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.
92 /* process all work events */
98 /* process timer events and get timeout for next timer event */
100 timer = nearest_timer(&select_timer, &temp);
109 // printf("wait till infinity ..."); fflush(stdout);
115 /* prepare read and write fdsets */
118 if (lcr_fd->when & LCR_FD_READ)
119 FD_SET(lcr_fd->fd, &readset);
120 if (lcr_fd->when & LCR_FD_WRITE)
121 FD_SET(lcr_fd->fd, &writeset);
122 if (lcr_fd->when & LCR_FD_EXCEPT)
123 FD_SET(lcr_fd->fd, &exceptset);
124 lcr_fd = lcr_fd->next;
129 rc = select(maxfd+1, &readset, &writeset, &exceptset, timer);
134 // printf("interrupted.\n");
137 if (global_change && *global_change) {
147 /* call registered callback functions */
154 if (FD_ISSET(lcr_fd->fd, &readset)) {
155 flags |= LCR_FD_READ;
156 FD_CLR(lcr_fd->fd, &readset);
158 if (FD_ISSET(lcr_fd->fd, &writeset)) {
159 flags |= LCR_FD_WRITE;
160 FD_CLR(lcr_fd->fd, &writeset);
162 if (FD_ISSET(lcr_fd->fd, &exceptset)) {
163 flags |= LCR_FD_EXCEPT;
164 FD_CLR(lcr_fd->fd, &exceptset);
168 lcr_fd->cb(lcr_fd, flags, lcr_fd->cb_instance, lcr_fd->cb_index);
173 lcr_fd = lcr_fd->next;
179 static struct lcr_timer *timer_first = NULL;
181 int _add_timer(struct lcr_timer *timer, int (*cb)(struct lcr_timer *timer, void *instance, int index), void *instance, int index, const char *func)
184 FATAL("timer that is registered in function %s is already in use\n", func);
188 struct lcr_timer *test = timer_first;
191 FATAL("Timer already in list %s\n", func);
198 timer->timeout.tv_sec = 0;
199 timer->timeout.tv_usec = 0;
201 timer->cb_instance = instance;
202 timer->cb_index = index;
203 timer->next = timer_first;
209 void _del_timer(struct lcr_timer *timer, const char *func)
211 struct lcr_timer **lcr_timerp;
213 /* find pointer to timer */
214 lcr_timerp = &timer_first;
216 if (*lcr_timerp == timer)
218 lcr_timerp = &((*lcr_timerp)->next);
221 FATAL("timer deleted in function %s not in list\n", func);
224 /* remove timer from list */
226 *lcr_timerp = timer->next;
229 void schedule_timer(struct lcr_timer *timer, int seconds, int microseconds)
231 struct timeval current_time;
234 FATAL("Timer not added\n");
237 gettimeofday(¤t_time, NULL);
238 unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec;
239 currentTime += seconds * MICRO_SECONDS + microseconds;
240 timer->timeout.tv_sec = currentTime / MICRO_SECONDS;
241 timer->timeout.tv_usec = currentTime % MICRO_SECONDS;
245 void unsched_timer(struct lcr_timer *timer)
250 /* if a timeout is reached, process timer, if not, return timer value for select */
251 static struct timeval *nearest_timer(struct timeval *select_timer, int *work)
253 struct timeval current;
254 struct timeval *nearest = NULL;
255 struct lcr_timer *lcr_timer, *lcr_nearest = NULL;
257 /* find nearest timer, or NULL, if no timer active */
258 lcr_timer = timer_first;
260 if (lcr_timer->active && (!nearest || TIME_SMALLER(&lcr_timer->timeout, nearest))) {
261 nearest = &lcr_timer->timeout;
262 lcr_nearest = lcr_timer;
264 lcr_timer = lcr_timer->next;
267 select_timer->tv_sec = 0;
268 select_timer->tv_usec = 0;
271 return NULL; /* wait until infinity */
273 gettimeofday(¤t, NULL);
274 unsigned long long nearestTime = nearest->tv_sec * MICRO_SECONDS + nearest->tv_usec;
275 unsigned long long currentTime = current.tv_sec * MICRO_SECONDS + current.tv_usec;
277 if (nearestTime > currentTime) {
278 select_timer->tv_sec = (nearestTime - currentTime) / MICRO_SECONDS;
279 select_timer->tv_usec = (nearestTime - currentTime) % MICRO_SECONDS;
282 lcr_nearest->active = 0;
283 (*lcr_nearest->cb)(lcr_nearest, lcr_nearest->cb_instance, lcr_nearest->cb_index);
284 /* don't wait so we can process the queues, indicate "work=1" */
285 select_timer->tv_sec = 0;
286 select_timer->tv_usec = 0;
293 static struct lcr_work *work_first = NULL; /* chain of work */
294 static struct lcr_work *first_event = NULL, *last_event = NULL; /* chain of active events */
297 void show_chain(const char *func)
299 struct lcr_work *work = first_event;
300 printf("chain:%s\n", func);
302 printf("%p - %p - %p\n", work->prev_event, work, work->next_event);
303 work = work->next_event;
308 int _add_work(struct lcr_work *work, int (*cb)(struct lcr_work *work, void *instance, int index), void *instance, int index, const char *func)
311 FATAL("work that is registered in function %s is already in use\n", func);
315 printf("add work %p from function %s\n", work, func);
316 show_chain("before add");
321 work->cb_instance = instance;
322 work->cb_index = index;
323 work->next = work_first;
326 show_chain("after add");
332 void _del_work(struct lcr_work *work, const char *func)
334 struct lcr_work **lcr_workp;
337 show_chain("before detach");
340 /* first event removed */
341 if (!work->prev_event)
342 first_event = work->next_event;
344 work->prev_event->next_event = work->next_event;
345 /* last event removed */
346 if (!work->next_event)
347 last_event = work->prev_event;
349 work->next_event->prev_event = work->prev_event;
352 show_chain("after detach");
355 /* find pointer to work */
356 lcr_workp = &work_first;
358 if (*lcr_workp == work)
360 lcr_workp = &((*lcr_workp)->next);
363 FATAL("work deleted by '%s' not in list\n", func);
366 /* remove work from list */
368 *lcr_workp = work->next;
370 show_chain("after delete");
374 void _trigger_work(struct lcr_work *work, const char *func)
377 FATAL("Work not added, (called from func %s)\n", func);
380 /* event already triggered */
385 show_chain("before trigger");
387 /* append to tail of chain */
389 last_event->next_event = work;
390 work->prev_event = last_event;
391 work->next_event = NULL;
396 show_chain("after trigger");
402 /* get first work and remove from event chain */
403 static int next_work(void)
405 struct lcr_work *lcr_work;
411 show_chain("before next_work");
413 if (!first_event->inuse) {
414 FATAL("Work not added\n");
417 /* detach from event chain */
418 lcr_work = first_event;
419 first_event = lcr_work->next_event;
423 first_event->prev_event = NULL;
426 show_chain("after next_work");
428 lcr_work->active = 0;
430 (*lcr_work->cb)(lcr_work, lcr_work->cb_instance, lcr_work->cb_index);