Highly experimental boss-worker socket handler
[pazpar2-moved-to-github.git] / src / eventl.h
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #ifndef EVENTL_H
21 #define EVENTL_H
22
23 #include <time.h>
24
25 struct iochan;
26
27 typedef void (*IOC_CALLBACK)(struct iochan *i, int event);
28 typedef int (*IOC_SOCKETFUN)(struct iochan *i);
29 typedef int (*IOC_MASKFUN)(struct iochan *i);
30
31 typedef struct iochan_man_s *iochan_man_t;
32
33 typedef struct iochan
34 {
35     int fd;
36     int flags;
37 #define EVENT_INPUT     0x01
38 #define EVENT_OUTPUT    0x02
39 #define EVENT_EXCEPT    0x04
40 #define EVENT_TIMEOUT   0x08
41     int force_event;
42     IOC_CALLBACK fun;
43     IOC_SOCKETFUN socketfun;
44     IOC_MASKFUN maskfun;
45     void *data;
46     int destroyed;
47     time_t last_event;
48     time_t max_idle;
49     int this_event;
50     int thread_users;
51
52     iochan_man_t man;
53
54     struct iochan *next;
55 } *IOCHAN;
56
57
58 iochan_man_t iochan_man_create(int use_threads);
59 void iochan_add(iochan_man_t man, IOCHAN chan);
60 void iochan_man_events(iochan_man_t man);
61 void iochan_man_destroy(iochan_man_t *mp);
62
63 #define iochan_destroy(i) (void)((i)->destroyed = 1)
64 #define iochan_getfd(i) ((i)->fd)
65 #define iochan_setfd(i, f) ((i)->fd = (f))
66 #define iochan_getdata(i) ((i)->data)
67 #define iochan_setdata(i, d) ((i)->data = d)
68 #define iochan_getflags(i) ((i)->flags)
69 #define iochan_setflags(i, d) ((i)->flags = d)
70 #define iochan_setflag(i, d) ((i)->flags |= d)
71 #define iochan_clearflag(i, d) ((i)->flags &= ~(d))
72 #define iochan_getflag(i, d) ((i)->flags & d ? 1 : 0)
73 #define iochan_getfun(i) ((i)->fun)
74 #define iochan_setfun(i, d) ((i)->fun = d)
75 #define iochan_setevent(i, e) ((i)->force_event = (e))
76 #define iochan_settimeout(i, t) ((i)->max_idle = (t), (i)->last_event = time(0))
77 #define iochan_activity(i) ((i)->last_event = time(0))
78 #define iochan_setsocketfun(i, f) ((i)->socketfun = (f))
79 #define iochan_getsocketfun(i) ((i)->socketfun)
80 #define iochan_setmaskfun(i, f) ((i)->maskfun = (f))
81 #define iochan_getmaskfun(i) ((i)->maskfun)
82
83 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags);
84
85 #endif
86 /*
87  * Local variables:
88  * c-basic-offset: 4
89  * c-file-style: "Stroustrup"
90  * indent-tabs-mode: nil
91  * End:
92  * vim: shiftwidth=4 tabstop=8 expandtab
93  */
94