-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathes.h
228 lines (209 loc) · 7.14 KB
/
es.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#ifndef ES_H
#define ES_H
/****************************************************************************************
* ES is short for "Event Server". This is a per-thread event server
* for multiplexing sockets, regular file descriptors, Unix signals, and interval
* timers.
****************************************************************************************/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <pthread.h>
#include <stdint.h>
#include "ez.h"
#ifdef __cplusplus
extern "C" {
#endif
ez_hdr_proto (int, ES_registerFd,
int fd,
short events,
int (*callback_f)(void *ctxt, int fd, short events),
void *ctxt);
/**********************************************************************
* Register a function to be called when there is activity on the
* file descriptor (which may be a file, socket, pipe, etc. under Unix).
*
* fd: the file descriptor to be registered.
* events: event bits to monitor (see: man 2 poll).
* callback_f: callback function for when activity is detected.
* ctxt: Pointer which will be passed as the first argument to callback_f().
*
* RETURNS:
* If successful, a positive integer which can be used to unregister the callback.
* On failure, -1 is returned.
*/
#ifdef DEBUG
# define ez_ES_registerFd(...) \
_ez_ES_registerFd(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_registerFd(...) \
_ez_ES_registerFd(__VA_ARGS__)
#endif
ez_hdr_proto (int, ES_registerSignal,
int signum,
int (*callback_f)(void *ctxt, int signo),
void *ctxt);
/**********************************************************************
* Register a function to be called when a particular Unix signal is
* raised. Note: callback_f() is not called from a Unix signal handler,
* so it is safe to modify data within callback_f().
*
* signum: Unix signal number of interest (type "trap -l" on command line to see a list of signals)
* callback_f: callback function for when activity is detected.
* ctxt: Pointer which will be passed as the first argument to callback_f().
*
* RETURNS:
* If successful, a positive integer which can be used to unregister the callback.
* On failure, -1 is returned.
*/
#ifdef DEBUG
# define ez_ES_registerSignal(...) \
_ez_ES_registerSignal(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_registerSignal(...) \
_ez_ES_registerSignal(__VA_ARGS__)
#endif
ez_hdr_proto (int, ES_registerVSignal,
int signum,
int (*callback_f)(void *ctxt,int signo),
void *ctxt);
/**********************************************************************
* Register a function to be called when a particular virtual signal is
* raised. Virtual signals are implemented on top of the Unix signal, SIGUSR2.
*
* signum: Any integer number which is meaningful to your application.
* callback_f: callback function for when activity is detected.
* ctxt: Pointer which will be passed as the first argument to callback_f().
*
* RETURNS:
* If successful, a positive integer which can be used to unregister the callback.
* On failure, -1 is returned.
*/
#ifdef DEBUG
# define ez_ES_registerVSignal(...) \
_ez_ES_registerVSignal(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_registerVSignal(...) \
_ez_ES_registerVSignal(__VA_ARGS__)
#endif
ez_hdr_proto (int, ES_VSignal,
pthread_t tid,
int signum);
/**********************************************************************
* Send a virtual signal to tid, which is multiplexed on SIGUSR2.
*
* tid: Target thread identifier.
* signum: Any integer number which is meaningful to your application.
*
* RETURNS:
* 0: successful
* -1: failures.
*/
#ifdef DEBUG
# define ez_ES_VSignal(...) \
_ez_ES_VSignal(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_VSignal(...) \
_ez_ES_VSignal(__VA_ARGS__)
#endif
ez_hdr_proto (int, ES_registerTimer,
int64_t pause_ms,
int64_t interval_ms,
int (*callback_f)(void *ctxt),
void *ctxt
);
/**********************************************************************
* Register a function to be called when an interval or single-shot
* timer expires. If interval_ms == 0, the timer is single shot, and
* can only be ES_unregister()'d before it expires.
*
* pause_ms: How many milliseconds to wait before initially firing.
* interval_ms: How many milliseconds to wait between successive firings.
* if this is 0, the timer is single shot.
* callback_f: callback function for when timer expires.
* ctxt: Pointer which will be passed as the first argument to callback_f().
*
* RETURNS:
* If successful, a positive integer which can be used to unregister the callback.
* On failure, -1 is returned.
*/
#ifdef DEBUG
# define ez_ES_registerTimer(...) \
_ez_ES_registerTimer(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_registerTimer(...) \
_ez_ES_registerTimer(__VA_ARGS__)
#endif
ez_hdr_proto (int, ES_unregister,
int key);
/**********************************************************************
* Unegister a previously registered callback.
*
* key: value obtained from ES_registerXXX().
*
* RETURNS:
* 0 for success.
* -1 for failure (key not found)
*/
#ifdef DEBUG
# define ez_ES_unregister(...) \
_ez_ES_unregister(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_unregister(...) \
_ez_ES_unregister(__VA_ARGS__)
#endif
ez_hdr_proto (int, ES_run);
/**********************************************************************
* For this thread, use poll() to process socket activity until one
* of the registered callback_f() returns non-zero.
*
* RETURNS:
* Whatever nonzero value callback_f() returned.
*/
#ifdef DEBUG
# define ez_ES_run(...) \
_ez_ES_run(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_run(...) \
_ez_ES_run(__VA_ARGS__)
#endif
pthread_t
ES_spawn_thread_sched(
void *(*user_main) (void *),
void *arg,
int sched_policy, /* SCHED_NORMAL || SCHED_FIFO || SCHED_RR || SCHED_BATCH */
int priority
);
/**********************************************************************
* Spawn a thread which will begin executing user_main(arg).
* NOTE: the calling thread will be blocked until ES_release_parent()
* is called from user_main()!
*
* user_main: function pointer where thread will execute.
* arg: address passed to user_main(arg).
* sched_policy: Which pthreads scheduling policy to use.
* priority: pthreads priority to use.
*
* RETURNS:
* 0 for success, nonzero for error
*/
#define ES_spawn_thread(user_main, arg) \
ES_spawn_thread_sched(user_main, arg, -1, 0)
/**********************************************************************
* This is a convenience macro.
*/
void
ES_release_parent(void);
/**********************************************************************
* Called by a new thread created with ES_spawn_thread_sched(), so
* that the parent can continue execution.
*/
void
ES_cleanup(void);
/**********************************************************************
* Called by a thread when it exits, to clean up resources.
*/
#ifdef __cplusplus
}
#endif
#endif