1 /* based on code from OpenBSC */
14 static int unregistered;
15 static struct lcr_fd *fd_first = NULL;
16 static struct timeval *nearest_timer(struct timeval *select_timer, int *work);
17 static int next_work(void);
19 int _register_fd(struct lcr_fd *fd, int when, int (*cb)(struct lcr_fd *fd, unsigned int what, void *instance, int index), void *instance, int index, const char *func)
24 FATAL("FD that is registered in function %s is already in use\n", func);
25 // printf("registering fd %d %s\n", fd->fd, func);
27 /* make FD nonblocking */
28 flags = fcntl(fd->fd, F_GETFL);
30 FATAL("Failed to F_GETFL\n");
32 flags = fcntl(fd->fd, F_SETFL, flags);
34 FATAL("Failed to F_SETFL O_NONBLOCK\n");
44 fd->cb_instance = instance;
52 void _unregister_fd(struct lcr_fd *fd, const char *func)
54 struct lcr_fd **lcr_fdp;
56 /* find pointer to fd */
61 lcr_fdp = &((*lcr_fdp)->next);
64 FATAL("FD unregistered in function %s not in list\n", func);
67 /* remove fd from list */
74 int select_main(int polling, int *global_change, void (*lock)(void), void (*unlock)(void))
76 struct lcr_fd *lcr_fd;
77 fd_set readset, writeset, exceptset;
78 int work = 0, temp, rc;
79 struct timeval no_time = {0, 0};
80 struct timeval select_timer, *timer;
84 * this ensures that select is only called until:
85 * - no work event exists
86 * - and no timeout occurred
88 * if no future timeout exists, select will wait infinit.
91 printf("-"); fflush(stdout);
93 printf("1"); fflush(stdout);
94 /* process all work events */
96 printf("2"); fflush(stdout);
101 /* process timer events and get timeout for next timer event */
103 timer = nearest_timer(&select_timer, &temp);
104 printf("3"); fflush(stdout);
106 printf("4"); fflush(stdout);
114 // printf("wait till infinity ..."); fflush(stdout);
120 printf("5"); fflush(stdout);
121 /* prepare read and write fdsets */
124 if (lcr_fd->when & LCR_FD_READ)
125 FD_SET(lcr_fd->fd, &readset);
126 if (lcr_fd->when & LCR_FD_WRITE)
127 FD_SET(lcr_fd->fd, &writeset);
128 if (lcr_fd->when & LCR_FD_EXCEPT)
129 FD_SET(lcr_fd->fd, &exceptset);
130 lcr_fd = lcr_fd->next;
132 printf("6"); fflush(stdout);
136 rc = select(maxfd+1, &readset, &writeset, &exceptset, timer);
141 // printf("interrupted.\n");
144 if (global_change && *global_change) {
148 printf("7"); fflush(stdout);
155 /* call registered callback functions */
157 printf("8"); fflush(stdout);
163 if (FD_ISSET(lcr_fd->fd, &readset)) {
164 flags |= LCR_FD_READ;
165 FD_CLR(lcr_fd->fd, &readset);
167 if (FD_ISSET(lcr_fd->fd, &writeset)) {
168 flags |= LCR_FD_WRITE;
169 FD_CLR(lcr_fd->fd, &writeset);
171 if (FD_ISSET(lcr_fd->fd, &exceptset)) {
172 flags |= LCR_FD_EXCEPT;
173 FD_CLR(lcr_fd->fd, &exceptset);
176 printf("9"); fflush(stdout);
178 lcr_fd->cb(lcr_fd, flags, lcr_fd->cb_instance, lcr_fd->cb_index);
181 printf("-"); fflush(stdout);
184 lcr_fd = lcr_fd->next;
190 static struct lcr_timer *timer_first = NULL;
192 int _add_timer(struct lcr_timer *timer, int (*cb)(struct lcr_timer *timer, void *instance, int index), void *instance, int index, const char *func)
195 FATAL("timer that is registered in function %s is already in use\n", func);
199 struct lcr_timer *test = timer_first;
202 FATAL("Timer already in list %s\n", func);
209 timer->timeout.tv_sec = 0;
210 timer->timeout.tv_usec = 0;
212 timer->cb_instance = instance;
213 timer->cb_index = index;
214 timer->next = timer_first;
220 void _del_timer(struct lcr_timer *timer, const char *func)
222 struct lcr_timer **lcr_timerp;
224 /* find pointer to timer */
225 lcr_timerp = &timer_first;
227 if (*lcr_timerp == timer)
229 lcr_timerp = &((*lcr_timerp)->next);
232 FATAL("timer deleted in function %s not in list\n", func);
235 /* remove timer from list */
237 *lcr_timerp = timer->next;
240 void schedule_timer(struct lcr_timer *timer, int seconds, int microseconds)
242 struct timeval current_time;
245 FATAL("Timer not added\n");
248 gettimeofday(¤t_time, NULL);
249 unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec;
250 currentTime += seconds * MICRO_SECONDS + microseconds;
251 timer->timeout.tv_sec = currentTime / MICRO_SECONDS;
252 timer->timeout.tv_usec = currentTime % MICRO_SECONDS;
256 void unsched_timer(struct lcr_timer *timer)
261 /* if a timeout is reached, process timer, if not, return timer value for select */
262 static struct timeval *nearest_timer(struct timeval *select_timer, int *work)
264 struct timeval current;
265 struct timeval *nearest = NULL;
266 struct lcr_timer *lcr_timer, *lcr_nearest = NULL;
268 /* find nearest timer, or NULL, if no timer active */
269 lcr_timer = timer_first;
271 if (lcr_timer->active && (!nearest || TIME_SMALLER(&lcr_timer->timeout, nearest))) {
272 nearest = &lcr_timer->timeout;
273 lcr_nearest = lcr_timer;
275 lcr_timer = lcr_timer->next;
278 select_timer->tv_sec = 0;
279 select_timer->tv_usec = 0;
282 return NULL; /* wait until infinity */
284 gettimeofday(¤t, NULL);
285 unsigned long long nearestTime = nearest->tv_sec * MICRO_SECONDS + nearest->tv_usec;
286 unsigned long long currentTime = current.tv_sec * MICRO_SECONDS + current.tv_usec;
288 if (nearestTime > currentTime) {
289 select_timer->tv_sec = (nearestTime - currentTime) / MICRO_SECONDS;
290 select_timer->tv_usec = (nearestTime - currentTime) % MICRO_SECONDS;
293 lcr_nearest->active = 0;
294 (*lcr_nearest->cb)(lcr_nearest, lcr_nearest->cb_instance, lcr_nearest->cb_index);
295 /* don't wait so we can process the queues, indicate "work=1" */
296 select_timer->tv_sec = 0;
297 select_timer->tv_usec = 0;
304 static struct lcr_work *work_first = NULL; /* chain of work */
305 static struct lcr_work *first_event = NULL, *last_event = NULL; /* chain of active events */
308 void show_chain(const char *func)
310 struct lcr_work *work = first_event;
311 printf("chain:%s\n", func);
313 printf("%p - %p - %p\n", work->prev_event, work, work->next_event);
314 work = work->next_event;
319 int _add_work(struct lcr_work *work, int (*cb)(struct lcr_work *work, void *instance, int index), void *instance, int index, const char *func)
322 FATAL("work that is registered in function %s is already in use\n", func);
326 printf("add work %p from function %s\n", work, func);
327 show_chain("before add");
332 work->cb_instance = instance;
333 work->cb_index = index;
334 work->next = work_first;
337 show_chain("after add");
343 void _del_work(struct lcr_work *work, const char *func)
345 struct lcr_work **lcr_workp;
348 show_chain("before detach");
351 /* first event removed */
352 if (!work->prev_event)
353 first_event = work->next_event;
355 work->prev_event->next_event = work->next_event;
356 /* last event removed */
357 if (!work->next_event)
358 last_event = work->prev_event;
360 work->next_event->prev_event = work->prev_event;
363 show_chain("after detach");
366 /* find pointer to work */
367 lcr_workp = &work_first;
369 if (*lcr_workp == work)
371 lcr_workp = &((*lcr_workp)->next);
374 FATAL("work deleted by '%s' not in list\n", func);
377 /* remove work from list */
379 *lcr_workp = work->next;
381 show_chain("after delete");
385 void trigger_work(struct lcr_work *work)
388 FATAL("Work not added\n");
391 /* event already triggered */
396 show_chain("before trigger");
398 /* append to tail of chain */
400 last_event->next_event = work;
401 work->prev_event = last_event;
402 work->next_event = NULL;
407 show_chain("after trigger");
413 /* get first work and remove from event chain */
414 static int next_work(void)
416 struct lcr_work *lcr_work;
422 show_chain("before next_work");
424 if (!first_event->inuse) {
425 FATAL("Work not added\n");
428 /* detach from event chain */
429 lcr_work = first_event;
430 first_event = lcr_work->next_event;
434 first_event->prev_event = NULL;
437 show_chain("after next_work");
439 lcr_work->active = 0;
441 (*lcr_work->cb)(lcr_work, lcr_work->cb_instance, lcr_work->cb_index);