libubox
C utility functions for OpenWrt.
uloop-epoll.c File Reference

Go to the source code of this file.

Macros

#define EPOLLRDHUP   0x2000
 

Functions

static int uloop_init_pollfd (void)
 
static int register_poll (struct uloop_fd *fd, unsigned int flags)
 
static int __uloop_fd_delete (struct uloop_fd *sock)
 
static int uloop_fetch_events (int timeout)
 
static void dispatch_timer (struct uloop_fd *u, unsigned int events)
 
static int timer_register (struct uloop_interval *tm, unsigned int msecs)
 
static int timer_remove (struct uloop_interval *tm)
 
static int64_t timer_next (struct uloop_interval *tm)
 

Variables

static struct epoll_event events [ULOOP_MAX_EVENTS]
 

Macro Definition Documentation

◆ EPOLLRDHUP

#define EPOLLRDHUP   0x2000

FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17

Definition at line 23 of file uloop-epoll.c.

Function Documentation

◆ __uloop_fd_delete()

static int __uloop_fd_delete ( struct uloop_fd sock)
static

Definition at line 62 of file uloop-epoll.c.

63 {
64  sock->flags = 0;
65  return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
66 }
uint8_t flags
Definition: uloop.h:69
int fd
Definition: uloop.h:65
static int poll_fd
Definition: uloop.c:62
Here is the caller graph for this function:

◆ dispatch_timer()

static void dispatch_timer ( struct uloop_fd u,
unsigned int  events 
)
static

Definition at line 108 of file uloop-epoll.c.

109 {
110  if (!(events & ULOOP_READ))
111  return;
112 
113  uint64_t fired;
114 
115  if (read(u->fd, &fired, sizeof(fired)) != sizeof(fired))
116  return;
117 
118  struct uloop_interval *tm = container_of(u, struct uloop_interval, priv.ufd);
119 
120  tm->expirations += fired;
121  tm->cb(tm);
122 }
#define container_of(ptr, type, member)
Definition: list.h:38
uint64_t expirations
Definition: uloop.h:93
union uloop_interval::@15 priv
uloop_interval_handler cb
Definition: uloop.h:92
int64_t fired
Definition: uloop.h:98
static struct epoll_event events[ULOOP_MAX_EVENTS]
Definition: uloop-epoll.c:60
#define ULOOP_READ
Definition: uloop.h:47
Here is the caller graph for this function:

◆ register_poll()

static int register_poll ( struct uloop_fd fd,
unsigned int  flags 
)
static

Definition at line 39 of file uloop-epoll.c.

40 {
41  struct epoll_event ev;
42  int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
43 
44  memset(&ev, 0, sizeof(struct epoll_event));
45 
46  if (flags & ULOOP_READ)
47  ev.events |= EPOLLIN | EPOLLRDHUP;
48 
49  if (flags & ULOOP_WRITE)
50  ev.events |= EPOLLOUT;
51 
52  if (flags & ULOOP_EDGE_TRIGGER)
53  ev.events |= EPOLLET;
54 
55  ev.data.ptr = fd;
56 
57  return epoll_ctl(poll_fd, op, fd->fd, &ev);
58 }
int fd
Definition: udebug-priv.h:27
#define EPOLLRDHUP
Definition: uloop-epoll.c:23
#define ULOOP_WRITE
Definition: uloop.h:48
#define ULOOP_EDGE_TRIGGER
Definition: uloop.h:49
Here is the caller graph for this function:

◆ timer_next()

static int64_t timer_next ( struct uloop_interval tm)
static

Definition at line 175 of file uloop-epoll.c.

176 {
177  struct itimerspec spec;
178 
179  if (!tm->priv.ufd.registered)
180  return -1;
181 
182  if (timerfd_gettime(tm->priv.ufd.fd, &spec) == -1)
183  return -1;
184 
185  return spec.it_value.tv_sec * 1000 + spec.it_value.tv_nsec / 1000000;
186 }
bool registered
Definition: uloop.h:68
struct uloop_fd ufd
Definition: uloop.h:96
Here is the caller graph for this function:

◆ timer_register()

static int timer_register ( struct uloop_interval tm,
unsigned int  msecs 
)
static

Definition at line 124 of file uloop-epoll.c.

125 {
126  if (!tm->priv.ufd.registered) {
127  int fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC|TFD_NONBLOCK);
128 
129  if (fd == -1)
130  return -1;
131 
132  tm->priv.ufd.fd = fd;
133  tm->priv.ufd.cb = dispatch_timer;
134  }
135 
136  struct itimerspec spec = {
137  .it_value = {
138  .tv_sec = msecs / 1000,
139  .tv_nsec = (msecs % 1000) * 1000000
140  },
141  .it_interval = {
142  .tv_sec = msecs / 1000,
143  .tv_nsec = (msecs % 1000) * 1000000
144  }
145  };
146 
147  if (timerfd_settime(tm->priv.ufd.fd, 0, &spec, NULL) == -1)
148  goto err;
149 
150  if (uloop_fd_add(&tm->priv.ufd, ULOOP_READ) == -1)
151  goto err;
152 
153  return 0;
154 
155 err:
156  uloop_fd_delete(&tm->priv.ufd);
157  close(tm->priv.ufd.fd);
158  memset(&tm->priv.ufd, 0, sizeof(tm->priv.ufd));
159 
160  return -1;
161 }
uloop_fd_handler cb
Definition: uloop.h:64
static void dispatch_timer(struct uloop_fd *u, unsigned int events)
Definition: uloop-epoll.c:108
int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
Definition: uloop.c:235
int uloop_fd_delete(struct uloop_fd *fd)
Definition: uloop.c:265
Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_remove()

static int timer_remove ( struct uloop_interval tm)
static

Definition at line 163 of file uloop-epoll.c.

164 {
165  int ret = __uloop_fd_delete(&tm->priv.ufd);
166 
167  if (ret == 0) {
168  close(tm->priv.ufd.fd);
169  memset(&tm->priv.ufd, 0, sizeof(tm->priv.ufd));
170  }
171 
172  return ret;
173 }
static int __uloop_fd_delete(struct uloop_fd *sock)
Definition: uloop-epoll.c:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uloop_fetch_events()

static int uloop_fetch_events ( int  timeout)
static

Definition at line 68 of file uloop-epoll.c.

69 {
70  int n, nfds;
71 
72  nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
73  for (n = 0; n < nfds; ++n) {
74  struct uloop_fd_event *cur = &cur_fds[n];
75  struct uloop_fd *u = events[n].data.ptr;
76  unsigned int ev = 0;
77 
78  cur->fd = u;
79  if (!u)
80  continue;
81 
82  if (events[n].events & (EPOLLERR|EPOLLHUP)) {
83  u->error = true;
84  if (!(u->flags & ULOOP_ERROR_CB))
85  uloop_fd_delete(u);
86  }
87 
88  if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP))) {
89  cur->fd = NULL;
90  continue;
91  }
92 
93  if(events[n].events & EPOLLRDHUP)
94  u->eof = true;
95 
96  if(events[n].events & EPOLLIN)
97  ev |= ULOOP_READ;
98 
99  if(events[n].events & EPOLLOUT)
100  ev |= ULOOP_WRITE;
101 
102  cur->events = ev;
103  }
104 
105  return nfds;
106 }
struct uloop_fd * fd
Definition: uloop.c:44
unsigned int events
Definition: uloop.c:45
Definition: uloop.h:63
bool eof
Definition: uloop.h:66
bool error
Definition: uloop.h:67
#define ARRAY_SIZE(x)
static struct uloop_fd_event cur_fds[10]
Definition: uloop.c:68
#define ULOOP_ERROR_CB
Definition: uloop.h:60
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uloop_init_pollfd()

static int uloop_init_pollfd ( void  )
static

Definition at line 26 of file uloop-epoll.c.

27 {
28  if (poll_fd >= 0)
29  return 0;
30 
31  poll_fd = epoll_create(32);
32  if (poll_fd < 0)
33  return -1;
34 
35  fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
36  return 0;
37 }
Here is the caller graph for this function:

Variable Documentation

◆ events

struct epoll_event events[ULOOP_MAX_EVENTS]
static

Definition at line 39 of file uloop-epoll.c.