libuev
uev.h
Go to the documentation of this file.
1 /* libuEv - Micro event loop library
2  *
3  * Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
4  * Copyright (c) 2013-2024 Joachim Wiberg <troglobit()gmail!com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
36 #ifndef LIBUEV_UEV_H_
37 #define LIBUEV_UEV_H_
38 
39 #include "private.h"
40 
41 #define UEV_MAX_EVENTS 10
43 /* I/O events, signal and timer revents are always UEV_READ */
44 #define UEV_NONE 0
45 #define UEV_ERROR EPOLLERR
46 #define UEV_READ EPOLLIN
47 #define UEV_WRITE EPOLLOUT
48 #define UEV_PRI EPOLLPRI
49 #define UEV_HUP EPOLLHUP
50 #define UEV_RDHUP EPOLLRDHUP
51 #define UEV_EDGE EPOLLET
52 #define UEV_ONESHOT EPOLLONESHOT
54 /* Run flags */
55 #define UEV_ONCE 1
56 #define UEV_NONBLOCK 2
59 #define uev_io_active(w) _uev_watcher_active(w)
60 
61 #define uev_signal_active(w) _uev_watcher_active(w)
62 
63 #define uev_timer_active(w) _uev_watcher_active(w)
64 
65 #define uev_cron_active(w) _uev_watcher_active(w)
66 
67 #define uev_event_active(w) _uev_watcher_active(w)
68 
70 typedef struct uev_ctx uev_ctx_t;
71 
73 typedef struct uev {
74  /* Private data for libuEv internal engine */
75  uev_private_t type;
76 
77  /* Public data for users to reference */
78  int signo;
79  int fd;
82  /* Extra data for certain watcher types */
83  struct signalfd_siginfo siginfo;
84 } uev_t;
85 
97 typedef void (uev_cb_t)(uev_t *w, void *arg, int events);
98 
99 /* Public interface */
100 
102 int uev_init (uev_ctx_t *ctx);
103 int uev_init1 (uev_ctx_t *ctx, int maxevents);
104 int uev_exit (uev_ctx_t *ctx);
105 int uev_run (uev_ctx_t *ctx, int flags);
106 
107 int uev_io_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int fd, int events);
108 int uev_io_set (uev_t *w, int fd, int events);
109 int uev_io_start (uev_t *w);
110 int uev_io_stop (uev_t *w);
111 
112 int uev_timer_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int timeout, int period);
113 int uev_timer_set (uev_t *w, int timeout, int period);
114 int uev_timer_start (uev_t *w);
115 int uev_timer_stop (uev_t *w);
116 
117 int uev_cron_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, time_t when, time_t interval);
118 int uev_cron_set (uev_t *w, time_t when, time_t interval);
119 int uev_cron_start (uev_t *w);
120 int uev_cron_stop (uev_t *w);
121 
122 int uev_signal_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int signo);
123 int uev_signal_set (uev_t *w, int signo);
124 int uev_signal_start (uev_t *w);
125 int uev_signal_stop (uev_t *w);
126 
127 int uev_event_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg);
128 int uev_event_post (uev_t *w);
129 int uev_event_stop (uev_t *w);
130 
131 #endif /* LIBUEV_UEV_H_ */
132 
int uev_signal_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int signo)
Create a signal watcher.
Definition: signal.c:52
int uev_init(uev_ctx_t *ctx)
Create an event loop context.
Definition: uev.c:191
int uev_exit(uev_ctx_t *ctx)
Terminate the event loop.
Definition: uev.c:240
int uev_init1(uev_ctx_t *ctx, int maxevents)
Create an event loop context.
Definition: uev.c:218
Event watcher.
Definition: uev.h:73
int uev_event_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg)
Create a generic event watcher.
Definition: event.c:46
int uev_io_stop(uev_t *w)
Stop an I/O watcher.
Definition: io.c:93
int uev_cron_start(uev_t *w)
Start a stopped at/cron job watcher.
Definition: cron.c:145
int uev_cron_stop(uev_t *w)
Stop and unregister an at/cron job watcher.
Definition: cron.c:156
int uev_io_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int fd, int events)
Create an I/O watcher.
Definition: io.c:44
int uev_run(uev_ctx_t *ctx, int flags)
Start the event loop.
Definition: uev.c:299
int uev_signal_stop(uev_t *w)
Stop a signal watcher.
Definition: signal.c:147
struct uev uev_t
Event watcher.
int fd
active descriptor
Definition: uev.h:79
int uev_signal_set(uev_t *w, int signo)
Reset a signal watcher.
Definition: signal.c:88
int uev_cron_set(uev_t *w, time_t when, time_t interval)
Reset an at/cron job watcher.
Definition: cron.c:100
int uev_timer_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int timeout, int period)
Create and start a timer watcher.
Definition: timer.c:77
int uev_cron_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, time_t when, time_t interval)
Create and start an at/cron job watcher.
Definition: cron.c:66
int uev_io_start(uev_t *w)
Start an I/O watcher.
Definition: io.c:82
int signo
configured signal
Definition: uev.h:78
int uev_io_set(uev_t *w, int fd, int events)
Reset an I/O watcher.
Definition: io.c:65
int uev_timer_start(uev_t *w)
Start a stopped timer watcher.
Definition: timer.c:160
int uev_timer_stop(uev_t *w)
Stop and unregister a timer watcher.
Definition: timer.c:179
struct signalfd_siginfo siginfo
received signal
Definition: uev.h:83
uev_ctx_t * ctx
watcher context
Definition: uev.h:80
int uev_event_post(uev_t *w)
Post a generic event.
Definition: event.c:70
int uev_event_stop(uev_t *w)
Stop a generic event watcher.
Definition: event.c:92
int uev_timer_set(uev_t *w, int timeout, int period)
Reset a timer.
Definition: timer.c:116
struct uev_ctx uev_ctx_t
Event loop context, need one per process and thread.
Definition: uev.h:70
int uev_signal_start(uev_t *w)
Start a stopped signal watcher.
Definition: signal.c:128
void() uev_cb_t(uev_t *w, void *arg, int events)
Generic callback for watchers, events holds UEV_READ and/or UEV_WRITE with optional UEV_PRI (priority...
Definition: uev.h:97