libuev 2.4.1
event.c
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
25#include <errno.h>
26#include <sys/eventfd.h>
27#include <unistd.h> /* close(), read() */
28
29#include "uev.h"
30
31
36
46int uev_event_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg)
47{
48 int fd;
49
50 if (!w || !ctx) {
51 errno = EINVAL;
52 return -1;
53 }
54 w->fd = -1;
55
56 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
57 if (fd < 0)
58 return -1;
59
60 return _uev_watcher_init(ctx, w, UEV_EVENT_TYPE, cb, arg, fd, UEV_READ)
61 || _uev_watcher_start(w);
62}
63
71{
72 uint64_t val = 1;
73
74 if (!w || -1 == w->fd) {
75 errno = EINVAL;
76 return -1;
77 }
78
79
80 if (write(w->fd, &val, sizeof(val)) != sizeof(val))
81 return -1;
82
83 return 0;
84}
85
93{
94 if (!_uev_watcher_active(w))
95 return 0;
96
97 if (_uev_watcher_stop(w))
98 return -1;
99
100 close(w->fd);
101 w->fd = -1;
102
103 return 0;
104}
105
int uev_event_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg)
Definition event.c:46
int uev_event_post(uev_t *w)
Definition event.c:70
int uev_event_stop(uev_t *w)
Definition event.c:92
int fd
Definition uev.h:79
struct uev_ctx uev_ctx_t
Definition uev.h:70
#define UEV_READ
Definition uev.h:46
void uev_cb_t(uev_t *w, void *arg, int events)
Definition uev.h:97
struct uev uev_t