bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
130.1.3
by Gustav Hartvigsson
* pragma once gawd damn it! |
1 |
#pragma once
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
2 |
|
3 |
/*
|
|
4 |
Copyright (c) 2013-2016 Gustav Hartvigsson
|
|
5 |
||
6 |
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7 |
of this software and associated documentation files (the "Software"), to deal
|
|
8 |
in the Software without restriction, including without limitation the rights
|
|
9 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10 |
copies of the Software, and to permit persons to whom the Software is
|
|
11 |
furnished to do so, subject to the following conditions:
|
|
12 |
||
13 |
The above copyright notice and this permission notice shall be included in
|
|
14 |
all copies or substantial portions of the Software.
|
|
15 |
||
16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22 |
THE SOFTWARE.
|
|
23 |
*/
|
|
24 |
||
25 |
#include "defs.h" |
|
26 |
#include "Func.h" |
|
27 |
#include "utils.h" |
|
28 |
#include "utils.h" |
|
29 |
||
30 |
S_BEGIN_DECLS
|
|
31 |
||
32 |
/**
|
|
33 |
* @file
|
|
34 |
* @defgroup SMainLoop SMainLoop
|
|
35 |
* @addtogroup SMainLoop
|
|
36 |
* @{
|
|
37 |
* The main loop system consits of two rings. The first ring is for high
|
|
38 |
* priority stuffs that need to be run often. The second ring is run much less
|
|
39 |
* often than the first ring.
|
|
40 |
*
|
|
41 |
* The sectond ring is put in the next state only after the first ring has done
|
|
42 |
* what is required of it.
|
|
43 |
*
|
|
44 |
* Please note that if you are doing pull/push of data via streams, only use one
|
|
45 |
* or the other, do not mix ring 1 and ring 2 as that may lead to filling
|
|
46 |
* or depleating of buffers.
|
|
47 |
*
|
|
48 |
* Here is an overwiev of the main loop:
|
|
49 |
* @code
|
|
50 |
* Ring 1
|
|
51 |
* [Do event handlers] -> [IO Push] -> [IO Pull] -> [Do Workers] ->
|
|
52 |
* ^ |
|
|
53 |
* | |
|
|
54 |
* -------------- [Sleep] <--- [Ring 2 Next] <--------
|
|
55 |
*
|
|
56 |
* Ring 2
|
|
57 |
* [Do event handlers] -> [IO Push] -> [IO PULL]
|
|
58 |
* ^ |
|
|
59 |
* | |
|
|
60 |
* --------- [Do Workers] <---------
|
|
61 |
* @endcode
|
|
62 |
*/
|
|
63 |
||
64 |
/* -----------------------------------------------------------------------------
|
|
65 |
* declarations
|
|
66 |
* -----------------------------------------------------------------------------
|
|
67 |
*/
|
|
68 |
||
69 |
/**
|
|
70 |
* The structure that represents the main loop. It is seldom required to
|
|
71 |
* directly interact with objcets of this type.
|
|
72 |
*/
|
|
73 |
typedef struct SMainLoop SMainLoop; |
|
74 |
||
75 |
/**
|
|
76 |
* The stats that the main loop can be in.
|
|
77 |
*/
|
|
78 |
typedef enum { |
|
79 |
S_MAIN_LOOP_STATE_NONE, |
|
80 |
S_MAIN_LOOP_STATE_EVENT, |
|
81 |
S_MAIN_LOOP_STATE_IO_PUSH, |
|
82 |
S_MAIN_LOOP_STATE_IO_PULL, |
|
83 |
S_MAIN_LOOP_STATE_DO_WORKERS, |
|
84 |
S_MAIN_LOOP_STATE_RING_TWO_NEXT, |
|
85 |
S_MAIN_LOOP_STATE_SLEEP, |
|
86 |
S_MAIN_LOOP_STATE_LAST |
|
87 |
} SMainLoopState; |
|
88 |
||
89 |
/**
|
|
90 |
* The namse of the states.
|
|
91 |
*/
|
|
92 |
S_UNUSED static schar * |
|
93 |
SMainLoopStateName [] = { |
|
94 |
"NONE", |
|
95 |
"Event", |
|
96 |
"Push", |
|
97 |
"Pull", |
|
98 |
"Do Work", |
|
99 |
"Ring Two Next", |
|
100 |
"Sleep", |
|
101 |
"NONE", |
|
102 |
0x0, |
|
103 |
0x0 |
|
104 |
};
|
|
105 |
||
106 |
/**
|
|
107 |
* Get the name of the state. For use in bindings.
|
|
108 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
109 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
110 |
schar * |
111 |
s_main_loop_state_get_name (SMainLoopState state); |
|
112 |
||
113 |
/**
|
|
114 |
* The different rings.
|
|
115 |
*/
|
|
116 |
typedef enum { |
|
117 |
S_MAIN_LOOP_RING_NONE, |
|
118 |
S_MAIN_LOOP_RING_ONE, |
|
119 |
S_MAIN_LOOP_RING_TWO, |
|
120 |
S_MAIN_LOOP_RING_LAST |
|
121 |
} SMainLoopRing; |
|
122 |
||
123 |
/**
|
|
124 |
* The names of the rings
|
|
125 |
*/
|
|
126 |
S_UNUSED static schar * |
|
127 |
SMainLoopRingName [] = { |
|
128 |
"NONE", |
|
129 |
"Ring One", |
|
130 |
"Ring Two", |
|
131 |
"NONE", |
|
132 |
0x0, |
|
133 |
0x0 |
|
134 |
};
|
|
135 |
||
136 |
/**
|
|
137 |
* Get name of the ring. For used in bindings.
|
|
138 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
139 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
140 |
schar * |
141 |
s_main_loop_ring_get_name (SMainLoopRing ring); |
|
142 |
||
143 |
/* -----------------------------------------------------------------------------
|
|
144 |
* definition of the real functions and stuffs.
|
|
145 |
* -----------------------------------------------------------------------------
|
|
146 |
*/
|
|
147 |
||
148 |
/** @brief
|
|
149 |
* Create a new MainLoop.
|
|
150 |
*
|
|
151 |
* This is something that usually is not needed, and unless you know exectly
|
|
152 |
* what you are doing, it is never recomeded to create your own mainloops,
|
|
153 |
* instead use @ref s_main_loop_get_default.
|
|
154 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
155 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
156 |
SMainLoop * |
157 |
s_main_loop_new (); |
|
158 |
||
159 |
/** @brief
|
|
160 |
* Get the default mainloop.
|
|
161 |
*
|
|
162 |
* The default mainloop is the mainloop that it is recomeded to interact with.
|
|
163 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
164 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
165 |
SMainLoop * |
166 |
s_main_loop_get_default (); |
|
167 |
||
168 |
/** @brief
|
|
169 |
* Run a mainloop.
|
|
170 |
*
|
|
171 |
* This is usually the way that applications are started, after all pre-start
|
|
172 |
* stuffs are done.
|
|
173 |
*
|
|
174 |
* @param loop The mainloop to be run.
|
|
175 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
176 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
177 |
void
|
178 |
s_main_loop_run (SMainLoop * loop); |
|
179 |
||
180 |
||
181 |
/** @brief
|
|
182 |
* Quit the mainloop. This is usually the way that applications are quit.
|
|
183 |
*
|
|
184 |
* When this function is run, it is important that all data has been freed.
|
|
185 |
* To assure that this has happened you can add teardown handlers to the
|
|
186 |
* main loop using the @ref s_main_loop_add_teardown_handler function,
|
|
187 |
* these handlers will be run when s_main_loop_quit is run.
|
|
188 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
189 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
190 |
void
|
191 |
s_main_loop_quit (SMainLoop * loop); |
|
192 |
||
193 |
/** @brief
|
|
194 |
* Add a reference to the main loop.
|
|
195 |
*
|
|
196 |
* This may or may not be useful. When the reference count reaches zero the
|
|
197 |
* main loop will be free using @ref s_main_loop_quit.
|
|
198 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
199 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
200 |
void
|
201 |
s_main_loop_ref (SMainLoop * loop); |
|
202 |
||
203 |
/** @brief
|
|
204 |
* Remove a reference to the main loop.
|
|
205 |
*
|
|
206 |
* This may or may not be useful. When the reference count reaches zero the
|
|
207 |
* main loop will be free using @ref s_main_loop_quit.
|
|
208 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
209 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
210 |
void
|
211 |
s_main_loop_unref (SMainLoop * loop); |
|
212 |
||
213 |
/** @brief.
|
|
214 |
* Add an event handler to the main loop
|
|
215 |
*
|
|
216 |
* @param loop The main loop to add the event hadler to.
|
|
217 |
* @param ring To what ring to add the event handler to. (Ring 1 for high
|
|
218 |
* priority handlers, and ring 2 for low priority handlers.)
|
|
219 |
* @param event_handler The event handler to be added to the main loop.
|
|
220 |
* @param user_data The user data to be passed to the event handler. This
|
|
221 |
* could be data that is relevent to the componant where the
|
|
222 |
* event handler originates from.
|
|
223 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
224 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
225 |
void
|
226 |
s_main_loop_add_event_handler (SMainLoop * loop, |
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
227 |
SMainLoopRing ring, |
228 |
RunFunc event_handler, |
|
229 |
spointer user_data); |
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
230 |
|
231 |
/** @brief
|
|
232 |
* Add an I/O push event to the main loop
|
|
233 |
*
|
|
234 |
* @param loop the main loop to add the I/O push event to.
|
|
235 |
* @param ring The ring to add the I/O push event to.
|
|
236 |
* @param push_callback The callback that prepforms the I/O push.
|
|
237 |
* @param user_data A pointer to the data to be provided to the callback.
|
|
238 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
239 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
240 |
void
|
241 |
s_main_loop_add_io_push (SMainLoop * loop, |
|
140
by Gustav Hartvigsson
* Added Better comments to SRingBuffer |
242 |
SMainLoopRing ring, |
243 |
RunFunc push_callback, |
|
244 |
spointer user_data); |
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
245 |
|
246 |
/** @brief
|
|
247 |
* Add a I/O pull event to the main loop.
|
|
248 |
*
|
|
249 |
* @param loop The loop to add the I/O pull event to.
|
|
250 |
* @param ring The ring to add the evet to.
|
|
251 |
* @param pull_callback The callback to be performs the I/O pull.
|
|
252 |
* @param user_data The data to be provided to the callback.
|
|
253 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
254 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
255 |
void
|
256 |
s_main_loop_add_io_pull (SMainLoop * loop, |
|
257 |
SMainLoopRing ring, |
|
258 |
RunFunc pull_callback, |
|
259 |
spointer user_data); |
|
260 |
||
261 |
/** @brief
|
|
262 |
* Add a worker to the main loop.
|
|
263 |
*
|
|
264 |
* @param loop The loop to add the worker to.
|
|
265 |
* @param ring The ring to add the worker to.
|
|
266 |
* @param pull_callback The worker callback.
|
|
267 |
* @param user_data The data to be provided to the callback.
|
|
268 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
269 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
270 |
void
|
271 |
s_main_loop_add_worker (SMainLoop * loop, |
|
140
by Gustav Hartvigsson
* Added Better comments to SRingBuffer |
272 |
SMainLoopRing ring, |
273 |
RunFunc worker_callback, |
|
274 |
spointer user_data); |
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
275 |
|
276 |
/** @brief
|
|
277 |
* Add a teardown handler.
|
|
278 |
*
|
|
279 |
* A teardown handler is a function that
|
|
280 |
*
|
|
281 |
* @param loop The loop to add the teardown handler to.
|
|
282 |
* @param teardown_handler The teardown handler.
|
|
283 |
* @param self The self object that is to be torn down.
|
|
284 |
* @param user_data A pointer to be provided to the callback.
|
|
285 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
286 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
287 |
void
|
288 |
s_main_loop_add_teardown_handler (SMainLoop * loop, |
|
140
by Gustav Hartvigsson
* Added Better comments to SRingBuffer |
289 |
Callback teardown_handler, |
290 |
spointer self, |
|
291 |
spointer user_data); |
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
292 |
|
293 |
||
151
by Gustav Hartvigsson
* Fixed all warnings (exept the depricated warning...) |
294 |
/** @brief
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
295 |
* @section THREAD_SAFTEY THREAD SAFTEY
|
296 |
*/
|
|
297 |
inline void FOOL_DOXYGEN (); |
|
298 |
||
151
by Gustav Hartvigsson
* Fixed all warnings (exept the depricated warning...) |
299 |
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
300 |
/** @brief
|
301 |
* lock the main loop form tampering from other threads.
|
|
302 |
*
|
|
303 |
* This function locks the main loop's mutex so that no changes can
|
|
304 |
* be done to the loop whilst it is locked.
|
|
305 |
*
|
|
306 |
* @param loop The main loop to be locked.
|
|
307 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
308 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
309 |
void
|
310 |
s_main_loop_lock (SMainLoop * loop); |
|
311 |
||
312 |
||
313 |
/** @brief
|
|
314 |
* lock the main loop form tampering from other threads.
|
|
315 |
*
|
|
316 |
* This function locks the main loop's mutex so that no changes can
|
|
317 |
* be done to the loop whilst it is locked.
|
|
318 |
*
|
|
319 |
* @param loop The loop to be unlocked.
|
|
320 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
321 |
S_EXPORTED
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
322 |
void
|
323 |
s_main_loop_unlock (SMainLoop * loop); |
|
324 |
||
151
by Gustav Hartvigsson
* Fixed all warnings (exept the depricated warning...) |
325 |
/* This has to be put last to remove undefined warning */
|
326 |
inline void FOOL_DOXYGEN () {}; |
|
116.1.1
by Gustav Hartvigsson
* Implemented the SMainLoop |
327 |
|
328 |
/**
|
|
329 |
* @}
|
|
330 |
*/
|
|
331 |
||
332 |
S_END_DECLS
|