]> git.mxchange.org Git - flightgear.git/blob - src/Time/event.hxx
Changes contributed by Tony Peden to put wind data "on the bus".
[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
35 #include <Include/fg_callback.hxx>
36
37 #include <deque>        // STL double ended queue
38 #include <list>         // STL list
39 #include STL_STRING
40
41 #include "fg_time.hxx"
42 #include "timestamp.hxx"
43
44 FG_USING_STD(deque);
45 FG_USING_STD(list);
46 FG_USING_STD(string);
47
48
49 class fgEVENT
50 {
51 public:
52     enum EventState
53     {
54         FG_EVENT_SUSP   = 0,
55         FG_EVENT_READY  = 1,
56         FG_EVENT_QUEUED = 2
57     };
58
59     friend class fgEVENT_MGR;
60
61     fgEVENT() {} // Required by deque<>.
62
63     fgEVENT( const string& desc,
64              const fgCallback& cb,
65              EventState evt_status,
66              int evt_interval );
67
68     ~fgEVENT();
69
70     void run();
71
72 //     void PrintStats() const;
73     int PrintStats() const;
74
75 private:
76     // not defined
77     fgEVENT( const fgEVENT& evt );
78     fgEVENT& operator= ( const fgEVENT& evt );
79
80 private:
81
82     string description;
83
84     // The callback object.
85     fgCallback* event_cb;
86
87     EventState status;       // status flag
88
89     long interval;    // interval in ms between each iteration of this event
90
91     FGTimeStamp last_run;
92     FGTimeStamp current;
93     FGTimeStamp next_run;
94
95     long cum_time;    // cumulative processor time of this event
96     long min_time;    // time of quickest execution
97     long max_time;    // time of slowest execution
98     long count;       // number of times executed
99 };
100
101
102 class fgEVENT_MGR {
103
104     // Event table
105     typedef deque < fgEVENT* >             EventContainer;
106     typedef EventContainer::iterator       EventIterator;
107     typedef EventContainer::const_iterator ConstEventIterator;
108
109     EventContainer event_table;
110
111     // Run Queue
112     typedef list < fgEVENT * > RunContainer;
113
114     RunContainer run_queue;
115
116 public:
117
118     // Constructor
119     fgEVENT_MGR ( void );
120
121     // Initialize the scheduling subsystem
122     void Init( void );
123
124     // Register an event with the scheduler
125     void Register( const string& desc, void (*event)( void ),
126                    fgEVENT::EventState status, int interval) {
127         Register( desc, fgFunctionCallback(event), status, interval );
128     }
129
130     void Register( const string& desc,
131                    const fgCallback& cb,
132                    fgEVENT::EventState status, 
133                    int interval );
134
135     // Update the scheduling parameters for an event
136     void Update( void );
137
138     // Delete a scheduled event
139     void Delete( void );
140
141     // Temporarily suspend scheduling of an event
142     void Suspend( void );
143
144     // Resume scheduling and event
145     void Resume( void );
146
147     // Dump scheduling stats
148     void PrintStats( void );
149
150     // Add pending jobs to the run queue and run the job at the front
151     // of the queue
152     void Process( void );
153
154     // Destructor
155     ~fgEVENT_MGR ( void );
156 };
157
158
159 // Wrapper to dump scheduling stats
160 void fgEventPrintStats( void );
161
162 extern fgEVENT_MGR global_events;
163
164
165 #endif // _EVENT_HXX
166
167