Refactor async.c
This commit is contained in:
32
src/async.c
32
src/async.c
@ -136,6 +136,9 @@ int async_init(void) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
listen1.n = 0;
|
||||||
|
listen2.n = 0;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,27 +149,38 @@ void async_free(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void async_thread(void) {
|
void async_thread(void) {
|
||||||
int num_fds;
|
listen_queue_t local_q;
|
||||||
struct pollfd fds[256]; // TODO dynamic
|
struct pollfd fds[256]; // TODO dynamic
|
||||||
|
|
||||||
thread = pthread_self();
|
thread = pthread_self();
|
||||||
|
local_q.n = 0;
|
||||||
|
|
||||||
// main event loop
|
// main event loop
|
||||||
while (alive) {
|
while (alive) {
|
||||||
// swap listen queue
|
// swap listen queue
|
||||||
listen_queue_t *l = listen_q;
|
listen_queue_t *l = listen_q;
|
||||||
listen_q = (listen_q == &listen1) ? &listen2 : &listen1;
|
listen_q = (listen_q == &listen1) ? &listen2 : &listen1;
|
||||||
|
int num_fds = 0;
|
||||||
|
|
||||||
|
// fill fds with previously added queue entries
|
||||||
|
for (int i = 0; i < l->n; i++, local_q.n++) {
|
||||||
|
memcpy(&local_q.q[local_q.n], &l->q[i], sizeof(evt_listen_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
// reset size of queue
|
||||||
|
l->n = 0;
|
||||||
|
|
||||||
// fill fds with newly added queue entries
|
// fill fds with newly added queue entries
|
||||||
for (num_fds = 0; num_fds < l->n; num_fds++) {
|
for (int i = 0; i < local_q.n; i++, num_fds++) {
|
||||||
fds[num_fds].fd = l->q[num_fds].fd;
|
fds[num_fds].fd = local_q.q[i].fd;
|
||||||
fds[num_fds].events = l->q[num_fds].events;
|
fds[num_fds].events = local_q.q[i].events;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (poll(fds, num_fds, -1) < 0) {
|
if (poll(fds, num_fds, -1) < 0) {
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
// interrupt
|
// interrupt
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
// other error
|
// other error
|
||||||
critical("Unable to poll for events");
|
critical("Unable to poll for events");
|
||||||
@ -174,14 +188,12 @@ void async_thread(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local_q.n = 0;
|
||||||
for (int i = 0; i < num_fds; i++) {
|
for (int i = 0; i < num_fds; i++) {
|
||||||
evt_listen_t *e = &l->q[i];
|
evt_listen_t *evt = &local_q.q[i];
|
||||||
if (async_exec(e, fds[i].revents) != 0)
|
if (async_exec(evt, fds[i].revents) != 0)
|
||||||
async_add_to_queue(e);
|
memcpy(&local_q.q[local_q.n++], evt, sizeof(*evt));
|
||||||
}
|
}
|
||||||
|
|
||||||
// reset size of queue
|
|
||||||
l->n = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user