Skip to content

Commit

Permalink
evl: make edge triggering optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdenCullen committed Sep 29, 2023
1 parent cac568c commit 9474203
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
3 changes: 2 additions & 1 deletion include/tlb/event_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ struct tlb_event_loop *tlb_evl_new(struct tlb_allocator *alloc);
void tlb_evl_destroy(struct tlb_event_loop *loop);

/** Subscribe a file descriptor to the event loop. */
tlb_handle tlb_evl_add_fd(struct tlb_event_loop *loop, int fd, int events, tlb_on_event *on_event, void *userdata);
tlb_handle tlb_evl_add_fd(struct tlb_event_loop *loop, int fd, int events, bool edge_trigger, tlb_on_event *on_event,
void *userdata);

/** Add a timer to fire in timeout milliseconds */
tlb_handle tlb_evl_add_timer(struct tlb_event_loop *loop, int timeout, tlb_on_event *trigger, void *userdata);
Expand Down
25 changes: 12 additions & 13 deletions source/event_loop.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "tlb/private/event_loop.h"

#include "tlb/event_loop.h"

#include <errno.h>

/**********************************************************************************************************************
Expand Down Expand Up @@ -40,11 +42,14 @@ static struct tlb_subscription *s_sub_new(struct tlb_event_loop *loop, tlb_on_ev
* File descriptor *
**********************************************************************************************************************/

tlb_handle tlb_evl_add_fd(struct tlb_event_loop *loop, int fd, int events, tlb_on_event *on_event, void *userdata) {
tlb_handle tlb_evl_add_fd(struct tlb_event_loop *loop, int fd, int events, bool edge_trigger, tlb_on_event *on_event,
void *userdata) {
struct tlb_subscription *sub = TLB_CHECK(NULL !=, s_sub_new(loop, on_event, userdata, "fd"));
sub->ident.fd = fd;
sub->events = events;
sub->sub_mode |= TLB_SUB_EDGE;
if (edge_trigger) {
sub->sub_mode |= TLB_SUB_EDGE;
}

tlb_evl_impl_fd_init(sub);
TLB_CHECK_GOTO(0 ==, tlb_evl_impl_subscribe(loop, sub), sub_failed);
Expand Down Expand Up @@ -76,18 +81,12 @@ tlb_handle tlb_evl_add_timer(struct tlb_event_loop *loop, int timeout, tlb_on_ev
**********************************************************************************************************************/

tlb_handle tlb_evl_add_evl(struct tlb_event_loop *loop, struct tlb_event_loop *sub_loop) {
struct tlb_subscription *sub = TLB_CHECK(NULL !=, s_sub_new(loop, tlb_evl_sub_loop_on_event, sub_loop, "sub-loop"));
sub->ident.fd = sub_loop->fd;
sub->events = TLB_EV_READ;
sub->sub_mode |= 0;

tlb_evl_impl_fd_init(sub);
TLB_CHECK_GOTO(0 ==, tlb_evl_impl_subscribe(loop, sub), sub_failed);
struct tlb_subscription *sub =
tlb_evl_add_fd(loop, sub_loop->fd, TLB_EV_READ, false, tlb_evl_sub_loop_on_event, sub_loop);
if (sub) {
sub->name = "sub-loop";
}
return sub;

sub_failed:
tlb_free(loop->alloc, sub);
return NULL;
}

void tlb_evl_sub_loop_on_event(tlb_handle subscription, int events, void *userdata) {
Expand Down
8 changes: 4 additions & 4 deletions tests/pipe_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class PipeTest : public TlbTest {
TlbTest::TearDown();
}

tlb_handle SubscribeRead(tlb_on_event *on_event, void *userdata) {
const tlb_handle handle = tlb_evl_add_fd(loop(), pipe.fd_read, TLB_EV_READ, on_event, userdata);
tlb_handle SubscribeRead(tlb_on_event *on_event, void *userdata, bool edge_trigger = true) {
const tlb_handle handle = tlb_evl_add_fd(loop(), pipe.fd_read, TLB_EV_READ, edge_trigger, on_event, userdata);
open_subscriptions.emplace(handle);
return handle;
}

tlb_handle SubscribeWrite(tlb_on_event *on_event, void *userdata) {
const tlb_handle handle = tlb_evl_add_fd(loop(), pipe.fd_write, TLB_EV_WRITE, on_event, userdata);
tlb_handle SubscribeWrite(tlb_on_event *on_event, void *userdata, bool edge_trigger = true) {
const tlb_handle handle = tlb_evl_add_fd(loop(), pipe.fd_write, TLB_EV_WRITE, edge_trigger, on_event, userdata);
open_subscriptions.emplace(handle);
return handle;
}
Expand Down

0 comments on commit 9474203

Please sign in to comment.