epoll event loop fd + timer
This commit is contained in:
1 parent
0e756354c9
commit
e76a0fe1e2
1 file changed
+33
@@ -2,9 +2,11 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
|
#include <sys/timerfd.h>
|
||||||
|
|
||||||
#include "eventloop.h"
|
#include "eventloop.h"
|
||||||
#include "eventloop_platform.h"
|
#include "eventloop_platform.h"
|
||||||
|
#include "io.h"
|
||||||
|
|
||||||
int owd_platform_el_init(struct owd_eventloop *el) {
|
int owd_platform_el_init(struct owd_eventloop *el) {
|
||||||
if ((el->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
|
if ((el->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
|
||||||
@@ -117,7 +119,30 @@ int owd_platform_el_add_timer(
|
|||||||
uint64_t millis,
|
uint64_t millis,
|
||||||
bool oneshot)
|
bool oneshot)
|
||||||
{
|
{
|
||||||
|
FD_SCOPED(tfd);
|
||||||
|
struct itimerspec spec;
|
||||||
|
struct timespec ts;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
ts.tv_sec = (long)(millis / 1000);
|
||||||
|
ts.tv_nsec = (long)((millis % 1000) * 1000000);
|
||||||
|
|
||||||
|
memset(&spec, 0, sizeof(spec));
|
||||||
|
spec.it_value = ts;
|
||||||
|
if (!oneshot)
|
||||||
|
spec.it_interval = ts;
|
||||||
|
|
||||||
|
if ((tfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC)) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (timerfd_settime(tfd, 0, &spec, NULL))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if ((res = el_epoll_add(el, e, tfd, OWD_EVENT_SOURCE_TIMER, EPOLLIN, true)))
|
||||||
|
return res;
|
||||||
|
|
||||||
|
FD_RELEASE(tfd);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int owd_platform_el_add_fd(
|
int owd_platform_el_add_fd(
|
||||||
@@ -126,5 +151,13 @@ int owd_platform_el_add_fd(
|
|||||||
int fd,
|
int fd,
|
||||||
uint32_t flags)
|
uint32_t flags)
|
||||||
{
|
{
|
||||||
|
uint32_t events;
|
||||||
|
|
||||||
|
events = 0;
|
||||||
|
if (flags & OWD_EVENT_READABLE)
|
||||||
|
events |= EPOLLIN;
|
||||||
|
if (flags & OWD_EVENT_WRITABLE)
|
||||||
|
events |= EPOLLOUT;
|
||||||
|
|
||||||
|
return el_epoll_add(el, e, fd, OWD_EVENT_SOURCE_FD, events, false);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user