Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evl: make edge triggering optional #7

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion source/linux/event_loop_epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void tlb_evl_cleanup(struct tlb_event_loop *loop) {
**********************************************************************************************************************/

void tlb_evl_impl_fd_init(struct tlb_subscription *sub) {
TLB_ASSERT(sub->sub_mode);
(void)sub;
}

/**********************************************************************************************************************
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
Loading