Don't remove lock, if other LCR is using it
[lcr.git] / select.h
1
2 #define LCR_FD_READ     1
3 #define LCR_FD_WRITE    2
4 #define LCR_FD_EXCEPT   4
5
6 #define MICRO_SECONDS  1000000LL
7
8 #define TIME_SMALLER(left, right) \
9         (((left)->tv_sec*MICRO_SECONDS+(left)->tv_usec) <= ((right)->tv_sec*MICRO_SECONDS+(right)->tv_usec))
10
11 struct lcr_fd {
12         struct lcr_fd   *next;  /* pointer to next element in list */
13         int             inuse;  /* if in use */
14         int             fd;     /* file descriptior if in use */
15         int             when;   /* select on what event */
16         int             (*cb)(struct lcr_fd *fd, unsigned int what, void *instance, int index); /* callback */
17         void            *cb_instance;
18         int             cb_index;
19 };
20
21 #define register_fd(a, b, c, d, e) _register_fd(a, b, c, d, e, __func__);
22 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);
23 #define unregister_fd(a) _unregister_fd(a, __func__);
24 void _unregister_fd(struct lcr_fd *fd, const char *func);
25 int select_main(int polling, int *global_change, void (*lock)(void), void (*unlock)(void));
26
27
28 struct lcr_timer {
29         struct lcr_timer *next; /* pointer to next element in list */
30         int             inuse;  /* if in use */
31         int             active; /* if timer is currently active */
32         struct timeval  timeout; /* timestamp when to timeout */
33         int             (*cb)(struct lcr_timer *timer, void *instance, int index); /* callback */
34         void            *cb_instance;
35         int             cb_index;
36 };
37
38 #define add_timer(a, b, c, d) _add_timer(a, b, c, d, __func__);
39 int _add_timer(struct lcr_timer *timer, int (*cb)(struct lcr_timer *timer, void *instance, int index), void *instance, int index, const char *func);
40 #define del_timer(a) _del_timer(a, __func__);
41 void _del_timer(struct lcr_timer *timer, const char *func);
42 void schedule_timer(struct lcr_timer *timer, int seconds, int microseconds);
43 void unsched_timer(struct lcr_timer *timer);
44
45
46 struct lcr_work {
47         struct lcr_work *next;  /* pointer to next element in list */
48         struct lcr_work *prev_event, *next_event; /* pointer to previous/next event, if triggered */
49         int             inuse;  /* if in use */
50         int             active; /* if timer is currently active */
51         int             (*cb)(struct lcr_work *work, void *instance, int index); /* callback */
52         void            *cb_instance;
53         int             cb_index;
54 };
55
56 #define add_work(a, b, c, d) _add_work(a, b, c, d, __func__);
57 int _add_work(struct lcr_work *work, int (*cb)(struct lcr_work *work, void *instance, int index), void *instance, int index, const char *func);
58 #define del_work(a) _del_work(a, __func__);
59 void _del_work(struct lcr_work *work, const char *func);
60 #define trigger_work(a) _trigger_work(a, __func__);
61 void _trigger_work(struct lcr_work *work, const char *func);
62
63