1
#ifndef __H_MAIN_LOOP__
2
#define __H_MAIN_LOOP__
5
Copyright (c) 2013-2016 Gustav Hartvigsson
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
14
The above copyright notice and this permission notice shall be included in
15
all copies or substantial portions of the Software.
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35
* @defgroup SMainLoop SMainLoop
36
* @addtogroup SMainLoop
38
* The main loop system consits of two rings. The first ring is for high
39
* priority stuffs that need to be run often. The second ring is run much less
40
* often than the first ring.
42
* The sectond ring is put in the next state only after the first ring has done
43
* what is required of it.
45
* Please note that if you are doing pull/push of data via streams, only use one
46
* or the other, do not mix ring 1 and ring 2 as that may lead to filling
47
* or depleating of buffers.
49
* Here is an overwiev of the main loop:
52
* [Do event handlers] -> [IO Push] -> [IO Pull] -> [Do Workers] ->
55
* -------------- [Sleep] <--- [Ring 2 Next] <--------
58
* [Do event handlers] -> [IO Push] -> [IO PULL]
61
* --------- [Do Workers] <---------
65
/* -----------------------------------------------------------------------------
67
* -----------------------------------------------------------------------------
71
* The structure that represents the main loop. It is seldom required to
72
* directly interact with objcets of this type.
74
typedef struct SMainLoop SMainLoop;
77
* The stats that the main loop can be in.
80
S_MAIN_LOOP_STATE_NONE,
81
S_MAIN_LOOP_STATE_EVENT,
82
S_MAIN_LOOP_STATE_IO_PUSH,
83
S_MAIN_LOOP_STATE_IO_PULL,
84
S_MAIN_LOOP_STATE_DO_WORKERS,
85
S_MAIN_LOOP_STATE_RING_TWO_NEXT,
86
S_MAIN_LOOP_STATE_SLEEP,
87
S_MAIN_LOOP_STATE_LAST
91
* The namse of the states.
93
S_UNUSED static schar *
94
SMainLoopStateName [] = {
108
* Get the name of the state. For use in bindings.
111
s_main_loop_state_get_name (SMainLoopState state);
114
* The different rings.
117
S_MAIN_LOOP_RING_NONE,
118
S_MAIN_LOOP_RING_ONE,
119
S_MAIN_LOOP_RING_TWO,
120
S_MAIN_LOOP_RING_LAST
124
* The names of the rings
126
S_UNUSED static schar *
127
SMainLoopRingName [] = {
137
* Get name of the ring. For used in bindings.
140
s_main_loop_ring_get_name (SMainLoopRing ring);
142
/* -----------------------------------------------------------------------------
143
* definition of the real functions and stuffs.
144
* -----------------------------------------------------------------------------
148
* Create a new MainLoop.
150
* This is something that usually is not needed, and unless you know exectly
151
* what you are doing, it is never recomeded to create your own mainloops,
152
* instead use @ref s_main_loop_get_default.
158
* Get the default mainloop.
160
* The default mainloop is the mainloop that it is recomeded to interact with.
163
s_main_loop_get_default ();
168
* This is usually the way that applications are started, after all pre-start
171
* @param loop The mainloop to be run.
174
s_main_loop_run (SMainLoop * loop);
178
* Quit the mainloop. This is usually the way that applications are quit.
180
* When this function is run, it is important that all data has been freed.
181
* To assure that this has happened you can add teardown handlers to the
182
* main loop using the @ref s_main_loop_add_teardown_handler function,
183
* these handlers will be run when s_main_loop_quit is run.
186
s_main_loop_quit (SMainLoop * loop);
189
* Add a reference to the main loop.
191
* This may or may not be useful. When the reference count reaches zero the
192
* main loop will be free using @ref s_main_loop_quit.
195
s_main_loop_ref (SMainLoop * loop);
198
* Remove a reference to the main loop.
200
* This may or may not be useful. When the reference count reaches zero the
201
* main loop will be free using @ref s_main_loop_quit.
204
s_main_loop_unref (SMainLoop * loop);
207
* Add an event handler to the main loop
209
* @param loop The main loop to add the event hadler to.
210
* @param ring To what ring to add the event handler to. (Ring 1 for high
211
* priority handlers, and ring 2 for low priority handlers.)
212
* @param event_handler The event handler to be added to the main loop.
213
* @param user_data The user data to be passed to the event handler. This
214
* could be data that is relevent to the componant where the
215
* event handler originates from.
218
s_main_loop_add_event_handler (SMainLoop * loop,
220
RunFunc event_handler,
224
* Add an I/O push event to the main loop
226
* @param loop the main loop to add the I/O push event to.
227
* @param ring The ring to add the I/O push event to.
228
* @param push_callback The callback that prepforms the I/O push.
229
* @param user_data A pointer to the data to be provided to the callback.
232
s_main_loop_add_io_push (SMainLoop * loop,
234
RunFunc push_callback,
238
* Add a I/O pull event to the main loop.
240
* @param loop The loop to add the I/O pull event to.
241
* @param ring The ring to add the evet to.
242
* @param pull_callback The callback to be performs the I/O pull.
243
* @param user_data The data to be provided to the callback.
246
s_main_loop_add_io_pull (SMainLoop * loop,
248
RunFunc pull_callback,
252
* Add a worker to the main loop.
254
* @param loop The loop to add the worker to.
255
* @param ring The ring to add the worker to.
256
* @param pull_callback The worker callback.
257
* @param user_data The data to be provided to the callback.
260
s_main_loop_add_worker (SMainLoop * loop,
262
RunFunc worker_callback,
266
* Add a teardown handler.
268
* A teardown handler is a function that
270
* @param loop The loop to add the teardown handler to.
271
* @param teardown_handler The teardown handler.
272
* @param self The self object that is to be torn down.
273
* @param user_data A pointer to be provided to the callback.
276
s_main_loop_add_teardown_handler (SMainLoop * loop,
277
Callback teardown_handler,
283
* @section THREAD_SAFTEY THREAD SAFTEY
285
inline void FOOL_DOXYGEN ();
288
* lock the main loop form tampering from other threads.
290
* This function locks the main loop's mutex so that no changes can
291
* be done to the loop whilst it is locked.
293
* @param loop The main loop to be locked.
296
s_main_loop_lock (SMainLoop * loop);
300
* lock the main loop form tampering from other threads.
302
* This function locks the main loop's mutex so that no changes can
303
* be done to the loop whilst it is locked.
305
* @param loop The loop to be unlocked.
308
s_main_loop_unlock (SMainLoop * loop);
318
#endif /* __H_MAIN_LOOP__ */