/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
1
#ifndef __H_MAIN_LOOP__
2
#define __H_MAIN_LOOP__
3
4
/*
5
Copyright (c) 2013-2016 Gustav Hartvigsson
6
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:
13
14
The above copyright notice and this permission notice shall be included in
15
all copies or substantial portions of the Software.
16
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
23
THE SOFTWARE.
24
 */
25
26
#include "defs.h"
27
#include "Func.h"
28
#include "utils.h"
29
#include "utils.h"
30
31
S_BEGIN_DECLS
32
33
/**
34
 * @file
35
 * @defgroup SMainLoop SMainLoop
36
 * @addtogroup SMainLoop
37
 * @{
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.
41
 *
42
 * The sectond ring is put in the next state only after the first ring has done
43
 * what is required of it.
44
 *
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.
48
 *
49
 * Here is an overwiev of the main loop:
50
 * @code
51
 *  Ring 1
52
 *   [Do event handlers] -> [IO Push] -> [IO Pull] -> [Do Workers] ->
53
 *       ^                                                  |
54
 *       |                                                  |
55
 *       -------------- [Sleep] <---  [Ring 2 Next] <--------
56
 *
57
 *  Ring 2
58
 *   [Do event handlers] -> [IO Push] -> [IO PULL]
59
 *         ^                                |
60
 *         |                                |
61
 *         ---------  [Do Workers] <---------
62
 * @endcode
63
 */
64
65
/* -----------------------------------------------------------------------------
66
 * declarations
67
 * -----------------------------------------------------------------------------
68
 */
69
70
/**
71
 * The structure that represents the main loop. It is seldom required to
72
 * directly interact with objcets of this type.
73
 */
74
typedef struct SMainLoop SMainLoop;
75
76
/**
77
 * The stats that the main loop can be in.
78
 */
79
typedef enum {
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
88
} SMainLoopState;
89
90
/**
91
 * The namse of the states.
92
 */
93
S_UNUSED static schar *
94
SMainLoopStateName [] = {
95
  "NONE",
96
  "Event",
97
  "Push",
98
  "Pull",
99
  "Do Work",
100
  "Ring Two Next",
101
  "Sleep",
102
  "NONE",
103
  0x0,
104
  0x0
105
};
106
107
/**
108
 * Get the name of the state. For use in bindings.
109
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
110
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
111
schar *
112
s_main_loop_state_get_name (SMainLoopState state);
113
114
/**
115
 * The different rings.
116
 */
117
typedef enum {
118
  S_MAIN_LOOP_RING_NONE,
119
  S_MAIN_LOOP_RING_ONE,
120
  S_MAIN_LOOP_RING_TWO,
121
  S_MAIN_LOOP_RING_LAST
122
} SMainLoopRing;
123
124
/**
125
 * The names of the rings
126
 */
127
S_UNUSED static schar *
128
SMainLoopRingName [] = {
129
  "NONE",
130
  "Ring One",
131
  "Ring Two",
132
  "NONE",
133
  0x0,
134
  0x0
135
};
136
137
/**
138
 * Get name of the ring. For used in bindings.
139
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
140
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
141
schar *
142
s_main_loop_ring_get_name (SMainLoopRing ring);
143
144
/* -----------------------------------------------------------------------------
145
 * definition of the real functions and stuffs.
146
 * -----------------------------------------------------------------------------
147
 */
148
149
/** @brief
150
 * Create a new MainLoop.
151
 *
152
 * This is something that usually is not needed, and unless you know exectly
153
 * what you are doing, it is never recomeded to create your own mainloops,
154
 * instead use @ref s_main_loop_get_default.
155
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
156
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
157
SMainLoop *
158
s_main_loop_new ();
159
160
/** @brief
161
 * Get the default mainloop.
162
 *
163
 * The default mainloop is the mainloop that it is recomeded to interact with.
164
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
165
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
166
SMainLoop *
167
s_main_loop_get_default ();
168
169
/** @brief
170
 * Run a mainloop.
171
 *
172
 * This is usually the way that applications are started, after all pre-start
173
 * stuffs are done.
174
 *
175
 * @param loop The mainloop to be run.
176
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
177
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
178
void
179
s_main_loop_run (SMainLoop * loop);
180
181
182
/** @brief
183
 * Quit the mainloop. This is usually the way that applications are quit.
184
 *
185
 * When this function is run, it is important that all data has been freed.
186
 * To assure that this has happened you can add teardown handlers to the
187
 * main loop using the @ref s_main_loop_add_teardown_handler function,
188
 * these handlers will be run when s_main_loop_quit is run.
189
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
190
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
191
void
192
s_main_loop_quit (SMainLoop * loop);
193
194
/** @brief
195
 * Add a reference to the main loop.
196
 *
197
 * This may or may not be useful. When the reference count reaches zero the
198
 * main loop will be free using @ref s_main_loop_quit.
199
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
200
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
201
void
202
s_main_loop_ref (SMainLoop * loop);
203
204
/** @brief
205
 * Remove a reference to the main loop.
206
 *
207
 * This may or may not be useful. When the reference count reaches zero the
208
 * main loop will be free using @ref s_main_loop_quit.
209
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
210
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
211
void
212
s_main_loop_unref (SMainLoop * loop);
213
214
/** @brief.
215
 * Add an event handler to the main loop
216
 *
217
 * @param loop The main loop to add the event hadler to.
218
 * @param ring To what ring to add the event handler to. (Ring 1 for high
219
 *             priority handlers, and ring 2 for low priority handlers.)
220
 * @param event_handler The event handler to be added to the main loop.
221
 * @param user_data The user data to be passed to the event handler. This
222
 *                  could be data that is relevent to the componant where the
223
 *                  event handler originates from.
224
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
225
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
226
void
227
s_main_loop_add_event_handler (SMainLoop * loop,
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
228
                               SMainLoopRing ring,
229
                               RunFunc event_handler,
230
                               spointer user_data);
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
231
232
/** @brief
233
 * Add an I/O push event to the main loop
234
 *
235
 * @param loop the main loop to add the I/O push event to.
236
 * @param ring The ring to add the I/O push event to.
237
 * @param push_callback The callback that prepforms the I/O push.
238
 * @param user_data A pointer to the data to be provided to the callback.
239
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
240
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
241
void
242
s_main_loop_add_io_push (SMainLoop * loop,
243
			 SMainLoopRing ring,
244
			 RunFunc push_callback,
245
			 spointer user_data);
246
247
/** @brief
248
 * Add a I/O pull event to the main loop.
249
 *
250
 * @param loop The loop to add the I/O pull event to.
251
 * @param ring The ring to add the evet to.
252
 * @param pull_callback The callback to be performs the I/O pull.
253
 * @param user_data The data to be provided to the callback.
254
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
255
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
256
void
257
s_main_loop_add_io_pull (SMainLoop * loop,
258
			 SMainLoopRing ring,
259
			 RunFunc pull_callback,
260
			 spointer user_data);
261
262
/** @brief
263
 * Add a worker to the main loop.
264
 *
265
 * @param loop The loop to add the worker to.
266
 * @param ring The ring to add the worker to.
267
 * @param pull_callback The worker callback.
268
 * @param user_data The data to be provided to the callback.
269
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
270
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
271
void
272
s_main_loop_add_worker (SMainLoop * loop,
273
			SMainLoopRing ring,
274
			RunFunc worker_callback,
275
			spointer user_data);
276
277
/** @brief
278
 * Add a teardown handler.
279
 *
280
 * A teardown handler is a function that
281
 *
282
 * @param loop The loop to add the teardown handler to.
283
 * @param teardown_handler The teardown handler.
284
 * @param self The self object that is to be torn down.
285
 * @param user_data A pointer to be provided to the callback.
286
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
287
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
288
void
289
s_main_loop_add_teardown_handler (SMainLoop * loop,
290
				  Callback teardown_handler,
291
				  spointer self,
292
				  spointer user_data);
293
294
295
/**
296
 * @section THREAD_SAFTEY THREAD SAFTEY
297
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
298
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
299
inline void FOOL_DOXYGEN ();
300
301
/** @brief
302
 * lock the main loop form tampering from other threads.
303
 *
304
 * This function locks the main loop's mutex so that no changes can
305
 * be done to the loop whilst it is locked.
306
 *
307
 * @param loop The main loop to be locked.
308
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
309
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
310
void
311
s_main_loop_lock (SMainLoop * loop);
312
313
314
/** @brief
315
 * lock the main loop form tampering from other threads.
316
 *
317
 * This function locks the main loop's mutex so that no changes can
318
 * be done to the loop whilst it is locked.
319
 *
320
 * @param loop The loop to be unlocked.
321
 */
119 by Gustav Hartvigsson
* added S_EXPERTED to public functions.
322
S_EXPORTED
116.1.1 by Gustav Hartvigsson
* Implemented the SMainLoop
323
void
324
s_main_loop_unlock (SMainLoop * loop);
325
326
327
/**
328
 * @}
329
 */
330
331
S_END_DECLS
332
333
334
#endif /* __H_MAIN_LOOP__ */