2013-09-17 05:57:40 +08:00
|
|
|
/*
|
|
|
|
* BIRD -- I/O and event loop
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "proto/bfd/io.h"
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
#include "lib/buffer.h"
|
2013-09-17 05:57:40 +08:00
|
|
|
#include "lib/lists.h"
|
|
|
|
#include "lib/resource.h"
|
|
|
|
#include "lib/event.h"
|
2017-05-31 01:12:35 +08:00
|
|
|
#include "lib/timer.h"
|
2013-09-17 05:57:40 +08:00
|
|
|
#include "lib/socket.h"
|
|
|
|
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
struct birdloop
|
|
|
|
{
|
|
|
|
pool *pool;
|
2013-09-17 05:57:40 +08:00
|
|
|
pthread_t thread;
|
|
|
|
pthread_mutex_t mutex;
|
2013-09-10 18:09:36 +08:00
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
u8 stop_called;
|
2013-09-17 05:57:40 +08:00
|
|
|
u8 poll_active;
|
|
|
|
u8 wakeup_masked;
|
|
|
|
int wakeup_fds[2];
|
|
|
|
|
2017-05-31 01:12:35 +08:00
|
|
|
struct timeloop time;
|
2013-09-10 18:09:36 +08:00
|
|
|
list event_list;
|
|
|
|
list sock_list;
|
|
|
|
uint sock_num;
|
|
|
|
|
|
|
|
BUFFER(sock *) poll_sk;
|
|
|
|
BUFFER(struct pollfd) poll_fd;
|
|
|
|
u8 poll_changed;
|
|
|
|
u8 close_scheduled;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-11-20 05:33:48 +08:00
|
|
|
/*
|
|
|
|
* Current thread context
|
|
|
|
*/
|
2013-09-17 05:57:40 +08:00
|
|
|
|
|
|
|
static pthread_key_t current_loop_key;
|
2017-05-31 01:12:35 +08:00
|
|
|
extern pthread_key_t current_time_key;
|
2013-09-17 05:57:40 +08:00
|
|
|
|
|
|
|
static inline struct birdloop *
|
|
|
|
birdloop_current(void)
|
|
|
|
{
|
|
|
|
return pthread_getspecific(current_loop_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
birdloop_set_current(struct birdloop *loop)
|
|
|
|
{
|
|
|
|
pthread_setspecific(current_loop_key, loop);
|
2017-05-31 01:12:35 +08:00
|
|
|
pthread_setspecific(current_time_key, loop ? &loop->time : &main_timeloop);
|
2013-09-17 05:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
birdloop_init_current(void)
|
|
|
|
{
|
|
|
|
pthread_key_create(¤t_loop_key, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-20 05:33:48 +08:00
|
|
|
/*
|
|
|
|
* Wakeup code for birdloop
|
|
|
|
*/
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
pipe_new(int *pfds)
|
|
|
|
{
|
2013-09-17 05:57:40 +08:00
|
|
|
int rv = pipe(pfds);
|
2013-09-10 18:09:36 +08:00
|
|
|
if (rv < 0)
|
|
|
|
die("pipe: %m");
|
|
|
|
|
|
|
|
if (fcntl(pfds[0], F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
die("fcntl(O_NONBLOCK): %m");
|
|
|
|
|
|
|
|
if (fcntl(pfds[1], F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
die("fcntl(O_NONBLOCK): %m");
|
|
|
|
}
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
void
|
|
|
|
pipe_drain(int fd)
|
2013-09-10 18:09:36 +08:00
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
try:
|
2013-09-17 05:57:40 +08:00
|
|
|
rv = read(fd, buf, 64);
|
2013-09-10 18:09:36 +08:00
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto try;
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return;
|
|
|
|
die("wakeup read: %m");
|
|
|
|
}
|
|
|
|
if (rv == 64)
|
|
|
|
goto try;
|
|
|
|
}
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
void
|
|
|
|
pipe_kick(int fd)
|
2013-09-10 18:09:36 +08:00
|
|
|
{
|
|
|
|
u64 v = 1;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
try:
|
2013-09-17 05:57:40 +08:00
|
|
|
rv = write(fd, &v, sizeof(u64));
|
2013-09-10 18:09:36 +08:00
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto try;
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return;
|
|
|
|
die("wakeup write: %m");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
static inline void
|
|
|
|
wakeup_init(struct birdloop *loop)
|
|
|
|
{
|
|
|
|
pipe_new(loop->wakeup_fds);
|
|
|
|
}
|
2013-09-10 18:09:36 +08:00
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
static inline void
|
|
|
|
wakeup_drain(struct birdloop *loop)
|
|
|
|
{
|
|
|
|
pipe_drain(loop->wakeup_fds[0]);
|
|
|
|
}
|
2013-09-10 18:09:36 +08:00
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
static inline void
|
2017-05-31 01:12:35 +08:00
|
|
|
wakeup_do_kick(struct birdloop *loop)
|
2013-09-17 05:57:40 +08:00
|
|
|
{
|
|
|
|
pipe_kick(loop->wakeup_fds[1]);
|
|
|
|
}
|
2013-09-10 18:09:36 +08:00
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
static inline void
|
|
|
|
wakeup_kick(struct birdloop *loop)
|
|
|
|
{
|
|
|
|
if (!loop->wakeup_masked)
|
|
|
|
wakeup_do_kick(loop);
|
|
|
|
else
|
|
|
|
loop->wakeup_masked = 2;
|
|
|
|
}
|
2013-09-10 18:09:36 +08:00
|
|
|
|
2017-05-31 01:12:35 +08:00
|
|
|
/* For notifications from outside */
|
|
|
|
void
|
|
|
|
wakeup_kick_current(void)
|
|
|
|
{
|
|
|
|
struct birdloop *loop = birdloop_current();
|
|
|
|
|
|
|
|
if (loop && loop->poll_active)
|
|
|
|
wakeup_kick(loop);
|
|
|
|
}
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
|
2013-11-20 05:33:48 +08:00
|
|
|
/*
|
|
|
|
* Events
|
|
|
|
*/
|
2013-09-17 05:57:40 +08:00
|
|
|
|
|
|
|
static inline uint
|
|
|
|
events_waiting(struct birdloop *loop)
|
|
|
|
{
|
|
|
|
return !EMPTY_LIST(loop->event_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2013-09-10 18:09:36 +08:00
|
|
|
events_init(struct birdloop *loop)
|
|
|
|
{
|
2013-09-17 05:57:40 +08:00
|
|
|
init_list(&loop->event_list);
|
2013-09-10 18:09:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
events_fire(struct birdloop *loop)
|
|
|
|
{
|
2017-05-31 01:12:35 +08:00
|
|
|
times_update(&loop->time);
|
2013-09-10 18:09:36 +08:00
|
|
|
ev_run_list(&loop->event_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ev2_schedule(event *e)
|
|
|
|
{
|
2013-09-17 05:57:40 +08:00
|
|
|
struct birdloop *loop = birdloop_current();
|
|
|
|
|
2013-09-10 18:09:36 +08:00
|
|
|
if (loop->poll_active && EMPTY_LIST(loop->event_list))
|
|
|
|
wakeup_kick(loop);
|
|
|
|
|
|
|
|
if (e->n.next)
|
|
|
|
rem_node(&e->n);
|
|
|
|
|
|
|
|
add_tail(&loop->event_list, &e->n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-20 05:33:48 +08:00
|
|
|
/*
|
|
|
|
* Sockets
|
|
|
|
*/
|
2013-09-17 05:57:40 +08:00
|
|
|
|
2013-09-10 18:09:36 +08:00
|
|
|
static void
|
|
|
|
sockets_init(struct birdloop *loop)
|
|
|
|
{
|
2013-09-17 05:57:40 +08:00
|
|
|
init_list(&loop->sock_list);
|
|
|
|
loop->sock_num = 0;
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
BUFFER_INIT(loop->poll_sk, loop->pool, 4);
|
|
|
|
BUFFER_INIT(loop->poll_fd, loop->pool, 4);
|
2013-09-17 05:57:40 +08:00
|
|
|
loop->poll_changed = 1; /* add wakeup fd */
|
2013-09-10 18:09:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sockets_add(struct birdloop *loop, sock *s)
|
|
|
|
{
|
|
|
|
add_tail(&loop->sock_list, &s->n);
|
|
|
|
loop->sock_num++;
|
|
|
|
|
|
|
|
s->index = -1;
|
|
|
|
loop->poll_changed = 1;
|
|
|
|
|
|
|
|
if (loop->poll_active)
|
|
|
|
wakeup_kick(loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sk_start(sock *s)
|
|
|
|
{
|
2013-09-17 05:57:40 +08:00
|
|
|
struct birdloop *loop = birdloop_current();
|
|
|
|
|
|
|
|
sockets_add(loop, s);
|
2013-09-10 18:09:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sockets_remove(struct birdloop *loop, sock *s)
|
|
|
|
{
|
|
|
|
rem_node(&s->n);
|
|
|
|
loop->sock_num--;
|
|
|
|
|
|
|
|
if (s->index >= 0)
|
2013-09-17 05:57:40 +08:00
|
|
|
loop->poll_sk.data[s->index] = NULL;
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
s->index = -1;
|
|
|
|
loop->poll_changed = 1;
|
|
|
|
|
|
|
|
/* Wakeup moved to sk_stop() */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sk_stop(sock *s)
|
|
|
|
{
|
2013-09-17 05:57:40 +08:00
|
|
|
struct birdloop *loop = birdloop_current();
|
|
|
|
|
|
|
|
sockets_remove(loop, s);
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
if (loop->poll_active)
|
|
|
|
{
|
|
|
|
loop->close_scheduled = 1;
|
|
|
|
wakeup_kick(loop);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
close(s->fd);
|
|
|
|
|
|
|
|
s->fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint sk_want_events(sock *s)
|
|
|
|
{ return (s->rx_hook ? POLLIN : 0) | ((s->ttx != s->tpos) ? POLLOUT : 0); }
|
|
|
|
|
2013-11-20 05:33:48 +08:00
|
|
|
/*
|
|
|
|
FIXME: this should be called from sock code
|
|
|
|
|
2013-09-10 18:09:36 +08:00
|
|
|
static void
|
|
|
|
sockets_update(struct birdloop *loop, sock *s)
|
|
|
|
{
|
|
|
|
if (s->index >= 0)
|
2013-09-17 05:57:40 +08:00
|
|
|
loop->poll_fd.data[s->index].events = sk_want_events(s);
|
2013-09-10 18:09:36 +08:00
|
|
|
}
|
2013-11-20 05:33:48 +08:00
|
|
|
*/
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
sockets_prepare(struct birdloop *loop)
|
|
|
|
{
|
|
|
|
BUFFER_SET(loop->poll_sk, loop->sock_num + 1);
|
|
|
|
BUFFER_SET(loop->poll_fd, loop->sock_num + 1);
|
|
|
|
|
|
|
|
struct pollfd *pfd = loop->poll_fd.data;
|
|
|
|
sock **psk = loop->poll_sk.data;
|
|
|
|
int i = 0;
|
|
|
|
node *n;
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
WALK_LIST(n, loop->sock_list)
|
2013-09-10 18:09:36 +08:00
|
|
|
{
|
|
|
|
sock *s = SKIP_BACK(sock, n, n);
|
|
|
|
|
|
|
|
ASSERT(i < loop->sock_num);
|
|
|
|
|
|
|
|
s->index = i;
|
|
|
|
*psk = s;
|
|
|
|
pfd->fd = s->fd;
|
|
|
|
pfd->events = sk_want_events(s);
|
|
|
|
pfd->revents = 0;
|
|
|
|
|
|
|
|
pfd++;
|
|
|
|
psk++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(i == loop->sock_num);
|
|
|
|
|
|
|
|
/* Add internal wakeup fd */
|
|
|
|
*psk = NULL;
|
|
|
|
pfd->fd = loop->wakeup_fds[0];
|
|
|
|
pfd->events = POLLIN;
|
|
|
|
pfd->revents = 0;
|
|
|
|
|
|
|
|
loop->poll_changed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sockets_close_fds(struct birdloop *loop)
|
|
|
|
{
|
|
|
|
struct pollfd *pfd = loop->poll_fd.data;
|
|
|
|
sock **psk = loop->poll_sk.data;
|
|
|
|
int poll_num = loop->poll_fd.used - 1;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < poll_num; i++)
|
|
|
|
if (psk[i] == NULL)
|
|
|
|
close(pfd[i].fd);
|
|
|
|
|
|
|
|
loop->close_scheduled = 0;
|
|
|
|
}
|
|
|
|
|
2016-03-15 21:57:49 +08:00
|
|
|
int sk_read(sock *s, int revents);
|
2013-09-17 05:57:40 +08:00
|
|
|
int sk_write(sock *s);
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
sockets_fire(struct birdloop *loop)
|
|
|
|
{
|
|
|
|
struct pollfd *pfd = loop->poll_fd.data;
|
|
|
|
sock **psk = loop->poll_sk.data;
|
|
|
|
int poll_num = loop->poll_fd.used - 1;
|
|
|
|
|
2017-05-31 01:12:35 +08:00
|
|
|
times_update(&loop->time);
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
/* Last fd is internal wakeup fd */
|
2016-09-08 19:45:36 +08:00
|
|
|
if (pfd[poll_num].revents & POLLIN)
|
2013-09-10 18:09:36 +08:00
|
|
|
wakeup_drain(loop);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < poll_num; pfd++, psk++, i++)
|
|
|
|
{
|
|
|
|
int e = 1;
|
|
|
|
|
|
|
|
if (! pfd->revents)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pfd->revents & POLLNVAL)
|
|
|
|
die("poll: invalid fd %d", pfd->fd);
|
|
|
|
|
|
|
|
if (pfd->revents & POLLIN)
|
|
|
|
while (e && *psk && (*psk)->rx_hook)
|
2016-03-15 21:57:49 +08:00
|
|
|
e = sk_read(*psk, 0);
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
e = 1;
|
|
|
|
if (pfd->revents & POLLOUT)
|
|
|
|
while (e && *psk)
|
|
|
|
e = sk_write(*psk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-20 05:33:48 +08:00
|
|
|
/*
|
|
|
|
* Birdloop
|
|
|
|
*/
|
2013-09-17 05:57:40 +08:00
|
|
|
|
|
|
|
static void * birdloop_main(void *arg);
|
|
|
|
|
2013-09-10 18:09:36 +08:00
|
|
|
struct birdloop *
|
2013-11-20 05:33:48 +08:00
|
|
|
birdloop_new(void)
|
2013-09-10 18:09:36 +08:00
|
|
|
{
|
2013-09-17 05:57:40 +08:00
|
|
|
/* FIXME: this init should be elsewhere and thread-safe */
|
|
|
|
static int init = 0;
|
|
|
|
if (!init)
|
|
|
|
{ birdloop_init_current(); init = 1; }
|
|
|
|
|
2013-11-20 05:33:48 +08:00
|
|
|
pool *p = rp_new(NULL, "Birdloop root");
|
2013-09-10 18:09:36 +08:00
|
|
|
struct birdloop *loop = mb_allocz(p, sizeof(struct birdloop));
|
2013-09-17 05:57:40 +08:00
|
|
|
loop->pool = p;
|
|
|
|
pthread_mutex_init(&loop->mutex, NULL);
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
wakeup_init(loop);
|
|
|
|
|
|
|
|
events_init(loop);
|
2017-05-31 01:12:35 +08:00
|
|
|
timers_init(&loop->time, p);
|
2013-09-10 18:09:36 +08:00
|
|
|
sockets_init(loop);
|
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
return loop;
|
|
|
|
}
|
2013-09-17 05:57:40 +08:00
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
void
|
|
|
|
birdloop_start(struct birdloop *loop)
|
|
|
|
{
|
2013-09-17 05:57:40 +08:00
|
|
|
int rv = pthread_create(&loop->thread, NULL, birdloop_main, loop);
|
2013-10-06 02:12:28 +08:00
|
|
|
if (rv)
|
|
|
|
die("pthread_create(): %M", rv);
|
|
|
|
}
|
2013-09-17 05:57:40 +08:00
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
void
|
|
|
|
birdloop_stop(struct birdloop *loop)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&loop->mutex);
|
|
|
|
loop->stop_called = 1;
|
|
|
|
wakeup_do_kick(loop);
|
|
|
|
pthread_mutex_unlock(&loop->mutex);
|
|
|
|
|
|
|
|
int rv = pthread_join(loop->thread, NULL);
|
|
|
|
if (rv)
|
|
|
|
die("pthread_join(): %M", rv);
|
2013-09-10 18:09:36 +08:00
|
|
|
}
|
|
|
|
|
2013-11-20 05:33:48 +08:00
|
|
|
void
|
|
|
|
birdloop_free(struct birdloop *loop)
|
|
|
|
{
|
|
|
|
rfree(loop->pool);
|
|
|
|
}
|
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
|
2013-09-10 18:09:36 +08:00
|
|
|
void
|
|
|
|
birdloop_enter(struct birdloop *loop)
|
|
|
|
{
|
2013-09-17 05:57:40 +08:00
|
|
|
/* TODO: these functions could save and restore old context */
|
|
|
|
pthread_mutex_lock(&loop->mutex);
|
|
|
|
birdloop_set_current(loop);
|
2013-09-10 18:09:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
birdloop_leave(struct birdloop *loop)
|
|
|
|
{
|
2013-09-17 05:57:40 +08:00
|
|
|
/* TODO: these functions could save and restore old context */
|
|
|
|
birdloop_set_current(NULL);
|
|
|
|
pthread_mutex_unlock(&loop->mutex);
|
2013-09-10 18:09:36 +08:00
|
|
|
}
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
void
|
|
|
|
birdloop_mask_wakeups(struct birdloop *loop)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&loop->mutex);
|
|
|
|
loop->wakeup_masked = 1;
|
|
|
|
pthread_mutex_unlock(&loop->mutex);
|
|
|
|
}
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
void
|
2013-09-17 05:57:40 +08:00
|
|
|
birdloop_unmask_wakeups(struct birdloop *loop)
|
2013-09-10 18:09:36 +08:00
|
|
|
{
|
2013-09-17 05:57:40 +08:00
|
|
|
pthread_mutex_lock(&loop->mutex);
|
|
|
|
if (loop->wakeup_masked == 2)
|
|
|
|
wakeup_do_kick(loop);
|
|
|
|
loop->wakeup_masked = 0;
|
|
|
|
pthread_mutex_unlock(&loop->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
birdloop_main(void *arg)
|
|
|
|
{
|
|
|
|
struct birdloop *loop = arg;
|
2013-09-10 18:09:36 +08:00
|
|
|
timer2 *t;
|
2013-09-17 05:57:40 +08:00
|
|
|
int rv, timeout;
|
2013-09-10 18:09:36 +08:00
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
birdloop_set_current(loop);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&loop->mutex);
|
2013-09-10 18:09:36 +08:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
events_fire(loop);
|
2017-05-31 01:12:35 +08:00
|
|
|
timers_fire(&loop->time);
|
2013-09-10 18:09:36 +08:00
|
|
|
|
2017-05-31 01:12:35 +08:00
|
|
|
times_update(&loop->time);
|
2013-09-10 18:09:36 +08:00
|
|
|
if (events_waiting(loop))
|
|
|
|
timeout = 0;
|
2017-05-31 01:12:35 +08:00
|
|
|
else if (t = timers_first(&loop->time))
|
2013-09-10 18:09:36 +08:00
|
|
|
timeout = (tm2_remains(t) TO_MS) + 1;
|
|
|
|
else
|
|
|
|
timeout = -1;
|
|
|
|
|
|
|
|
if (loop->poll_changed)
|
|
|
|
sockets_prepare(loop);
|
|
|
|
|
|
|
|
loop->poll_active = 1;
|
2013-09-17 05:57:40 +08:00
|
|
|
pthread_mutex_unlock(&loop->mutex);
|
2013-09-10 18:09:36 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
rv = poll(loop->poll_fd.data, loop->poll_fd.used, timeout);
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR || errno == EAGAIN)
|
|
|
|
goto try;
|
|
|
|
die("poll: %m");
|
|
|
|
}
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
pthread_mutex_lock(&loop->mutex);
|
2013-09-10 18:09:36 +08:00
|
|
|
loop->poll_active = 0;
|
|
|
|
|
|
|
|
if (loop->close_scheduled)
|
|
|
|
sockets_close_fds(loop);
|
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
if (loop->stop_called)
|
|
|
|
break;
|
|
|
|
|
2013-09-10 18:09:36 +08:00
|
|
|
if (rv)
|
|
|
|
sockets_fire(loop);
|
|
|
|
|
2017-05-31 01:12:35 +08:00
|
|
|
timers_fire(&loop->time);
|
2013-09-10 18:09:36 +08:00
|
|
|
}
|
2013-09-17 05:57:40 +08:00
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
loop->stop_called = 0;
|
|
|
|
pthread_mutex_unlock(&loop->mutex);
|
|
|
|
|
2013-09-17 05:57:40 +08:00
|
|
|
return NULL;
|
2013-09-10 18:09:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|