// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. #pragma once #include #include #include #include "list.h" #include "scoped.h" #define TLSL_EVENT_READABLE (1 << 0) #define TLSL_EVENT_WRITABLE (1 << 1) enum tlsl_event_source { TLSL_EVENT_SOURCE_FD, TLSL_EVENT_SOURCE_TIMER, }; typedef struct { uintptr_t obj; enum tlsl_event_source source; } tlsl_event_id_t; struct tlsl_event { tlsl_event_id_t id; struct intrusive_list list; char _platform[16] __attribute__((aligned)); }; struct tlsl_eventloop { struct intrusive_list event_list; bool valid; char _platform[16] __attribute__((aligned)); }; static inline bool tlsl_event_id_eq(tlsl_event_id_t eid1, tlsl_event_id_t eid2) { return (eid1.obj == eid2.obj) && (eid1.source == eid2.source); } int tlsl_eventloop_init(struct tlsl_eventloop *el); void tlsl_eventloop_cleanup(struct tlsl_eventloop *el); int tlsl_eventloop_wait(struct tlsl_eventloop *el, tlsl_event_id_t *id_out, uint32_t *flags_out, int timeout_ms); int tlsl_eventloop_clear(struct tlsl_eventloop *el, struct tlsl_event *e); void tlsl_eventloop_remove(struct tlsl_eventloop *el, struct tlsl_event *e); int tlsl_eventloop_add_timer(struct tlsl_eventloop *el, struct tlsl_event *e, uint64_t millis, bool oneshot); int tlsl_eventloop_add_fd(struct tlsl_eventloop *el, struct tlsl_event *e, int fd, uint32_t flags); DEF_SCOPED_TYPE(eventloop_stack, struct tlsl_eventloop, (struct tlsl_eventloop) { .valid = false }, tlsl_eventloop_cleanup) #define EVENTLOOP_STACK_SCOPED(VAL) SCOPED(eventloop_stack, struct tlsl_eventloop, VAL) #define EVENTLOOP_STACK_CLOSE(EL) _scoped_eventloop_stack_cleanup(EL)