Have a way to wait until a worker state changes.
[apps/madmutt.git] / lib-sys / evtloop.h
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  */
17 /*
18  *  Copyright © 2007 Pierre Habouzit
19  */
20
21 #ifndef MUTT_LIB_SYS_EVTLOOP_H
22 #define MUTT_LIB_SYS_EVTLOOP_H
23
24 #include <lib-lib/lib-lib.h>
25 #include <gnutls/gnutls.h>
26 #include <sys/socket.h>
27
28 typedef enum el_state {
29     EL_LLP_INIT,
30     EL_LLP_READY,
31     EL_LLP_FINI,
32 } el_state;
33
34 typedef enum el_mode {
35     EL_NEW     = 0,
36     EL_READING = 1,
37     EL_WRITING = 2,
38     EL_RDWR    = 3,
39     EL_IDLE    = 4,
40 } el_mode;
41
42 typedef enum el_status {
43     EL_SUCCESS,
44     EL_ERROR,
45     EL_KILLED,
46     EL_RDHUP,
47 } el_status;
48
49 typedef enum el_event {
50     EL_EVT_IN      = EL_READING,
51     EL_EVT_OUT     = EL_WRITING,
52     EL_EVT_INOUT   = EL_RDWR,
53     EL_EVT_RUNNING = 4,
54     EL_EVT_WAKEUP  = 5,
55 } el_event;
56
57 typedef struct job_t {
58     int fd;
59     int ssf;
60
61     unsigned cond  : 1;
62     el_state state : 2;
63     el_mode  mode  : 3;
64     el_mode  emode : 3;
65
66     gnutls_session_t session;
67     gnutls_certificate_credentials_t xcred;
68
69     struct timeval mru;
70     int (*llp)(struct job_t *);
71     const struct machine_t *m;
72     void *ptr;
73 } job_t;
74 DO_INIT(job_t, job);
75 void job_wipe(job_t *w);
76 DO_NEW(job_t, job);
77 DO_DELETE(job_t, job);
78
79 typedef struct machine_t {
80     const char *name;
81     __must_check__ int (*setup)(job_t *w, void *);
82     __must_check__ int (*on_event)(job_t *w, el_event);
83     void (*finalize)(job_t *w, el_status);
84 } machine_t;
85
86 #define EL_JOB_CHECK(expr) \
87     do { if ((expr) < 0) return -1; } while (0)
88
89 __must_check__ int el_job_setmode(job_t *w, el_mode);
90 __must_check__ job_t *el_job_start(const machine_t *m, void *cfg);
91 __must_check__ int el_job_release(job_t *j, el_status);
92
93 __must_check__ int el_job_connect(job_t *w, struct sockaddr *, socklen_t len,
94                                   int type, int proto, int ssl);
95 __must_check__ int el_job_starttls(job_t *w);
96 __must_check__ ssize_t el_job_read(job_t *w, buffer_t *buf);
97 __must_check__ ssize_t el_job_write(job_t *w, buffer_t *buf);
98
99 void el_lock(void);
100 void el_unlock(void);
101 void el_wait(job_t *w);
102
103 int el_dispatch(int timeout);
104
105 void el_initialize(void);
106 void el_shutdown(void);
107
108 #endif