]> git.mxchange.org Git - flightgear.git/blob - src/Time/event.hxx
Various tweaks for mingwin32.
[flightgear.git] / src / Time / event.hxx
1 // event.hxx -- Flight Gear periodic event scheduler
2 //
3 // Written by Curtis Olson, started December 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef _EVENT_HXX
25 #define _EVENT_HXX
26
27
28 #ifndef __cplusplus                                                          
29 # error This library requires C++
30 #endif                                   
31
32
33 #include <simgear/compiler.h>
34 #include <simgear/timing/timestamp.hxx>
35
36 #include <Include/fg_callback.hxx>
37
38 #include <deque>        // STL double ended queue
39 #include <list>         // STL list
40 #include STL_STRING
41
42
43 SG_USING_STD(deque);
44 SG_USING_STD(list);
45 SG_USING_STD(string);
46
47
48 class fgEVENT
49 {
50 public:
51     enum EventState
52     {
53         FG_EVENT_SUSP   = 0,
54         FG_EVENT_READY  = 1,
55         FG_EVENT_QUEUED = 2
56     };
57
58     friend class fgEVENT_MGR;
59
60     fgEVENT() {} // Required by deque<>.
61
62     fgEVENT( const string& desc,
63              const fgCallback& cb,
64              EventState evt_status,
65              int evt_interval );
66
67     ~fgEVENT();
68
69     void run();
70
71 //     void PrintStats() const;
72     int PrintStats() const;
73
74 private:
75     // not defined
76     fgEVENT( const fgEVENT& evt );
77     fgEVENT& operator= ( const fgEVENT& evt );
78
79 private:
80
81     string description;
82
83     // The callback object.
84     fgCallback* event_cb;
85
86     EventState status;       // status flag
87
88     long interval;    // interval in ms between each iteration of this event
89
90     SGTimeStamp last_run;
91     SGTimeStamp current;
92     SGTimeStamp next_run;
93
94     long cum_time;    // cumulative processor time of this event
95     long min_time;    // time of quickest execution
96     long max_time;    // time of slowest execution
97     long count;       // number of times executed
98 };
99
100
101 class fgEVENT_MGR {
102
103     // Event table
104     typedef deque < fgEVENT* >             EventContainer;
105     typedef EventContainer::iterator       EventIterator;
106     typedef EventContainer::const_iterator ConstEventIterator;
107
108     EventContainer event_table;
109
110     // Run Queue
111     typedef list < fgEVENT * > RunContainer;
112
113     RunContainer run_queue;
114
115 public:
116
117     // Constructor
118     fgEVENT_MGR ( void );
119
120     // Initialize the scheduling subsystem
121     void Init( void );
122
123     // Register an event with the scheduler
124     void Register( const string& desc, void (*event)( void ),
125                    fgEVENT::EventState status, int interval) {
126         Register( desc, fgFunctionCallback(event), status, interval );
127     }
128
129     void Register( const string& desc,
130                    const fgCallback& cb,
131                    fgEVENT::EventState status, 
132                    int interval );
133
134     // Update the scheduling parameters for an event
135     void Update( void );
136
137     // Delete a scheduled event
138     void Delete( void );
139
140     // Temporarily suspend scheduling of an event
141     void Suspend( void );
142
143     // Resume scheduling and event
144     void Resume( void );
145
146     // Dump scheduling stats
147     void PrintStats( void );
148
149     // Add pending jobs to the run queue and run the job at the front
150     // of the queue
151     void Process( void );
152
153     // Destructor
154     ~fgEVENT_MGR ( void );
155 };
156
157
158 // Wrapper to dump scheduling stats
159 void fgEventPrintStats( void );
160
161 extern fgEVENT_MGR global_events;
162
163
164 #endif // _EVENT_HXX
165
166