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

Avoid unnecessary libevent timers in the MessagePump #4258

Merged
merged 3 commits into from
Oct 15, 2024
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
39 changes: 20 additions & 19 deletions starboard/shared/libevent/socket_waiter_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ void GetSocketPipe(SbSocket* client_socket, SbSocket* server_socket) {
} // namespace

SbSocketWaiterPrivate::SbSocketWaiterPrivate()
: thread_(pthread_self()),
base_(event_base_new()),
waiting_(false),
woken_up_(false) {
: thread_(pthread_self()), base_(event_base_new()), woken_up_(false) {
#if USE_POSIX_PIPE
int fds[2];
int result = pipe(fds);
Expand Down Expand Up @@ -274,33 +271,38 @@ void SbSocketWaiterPrivate::Wait() {
SbSocketWaiterResult SbSocketWaiterPrivate::WaitTimed(int64_t duration_usec) {
SB_DCHECK(pthread_equal(pthread_self(), thread_));

if ((duration_usec == 0) || (duration_usec == kSbInt64Max)) {
// There is no need for a timeout event in these cases.
event_base_loop(base_,
(duration_usec == 0) ? EVLOOP_ONCE | EVLOOP_NONBLOCK : 0);

SbSocketWaiterResult result = woken_up_ ? kSbSocketWaiterResultWokenUp
: kSbSocketWaiterResultTimedOut;
woken_up_ = false;

return result;
}

// The way to do this is apparently to create a timeout event, call WakeUp
// inside that callback, and then just do a normal wait.
struct event event;
timeout_set(&event, &SbSocketWaiterPrivate::LibeventTimeoutCallback, this);
event_base_set(base_, &event);

if (duration_usec < kSbInt64Max) {
struct timeval tv;
tv.tv_sec = duration_usec / 1'000'000;
tv.tv_usec = duration_usec % 1'000'000;
timeout_add(&event, &tv);
}
struct timeval tv;
tv.tv_sec = duration_usec / 1'000'000;
tv.tv_usec = duration_usec % 1'000'000;
timeout_add(&event, &tv);

waiting_ = true;
event_base_loop(base_, 0);
waiting_ = false;

SbSocketWaiterResult result =
woken_up_ ? kSbSocketWaiterResultWokenUp : kSbSocketWaiterResultTimedOut;
woken_up_ = false;

if (duration_usec < kSbInt64Max) {
// We clean this up, in case we were awakened early, to prevent a spurious
// wake-up later.
timeout_del(&event);
}

// We clean this up, in case we were awakened early, to prevent a spurious
// wake-up later.
timeout_del(&event);
return result;
}

Expand Down Expand Up @@ -362,7 +364,6 @@ void SbSocketWaiterPrivate::HandleSignal(Waitee* waitee,
}

void SbSocketWaiterPrivate::HandleWakeUpRead() {
SB_DCHECK(waiting_);
// Remove and discard the wakeup byte.
char buf;
int bytes_read = HANDLE_EINTR(read(wakeup_read_fd_, &buf, 1));
Expand Down
3 changes: 0 additions & 3 deletions starboard/shared/libevent/socket_waiter_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,6 @@ struct SbSocketWaiterPrivate {
// The registry of currently registered Waitees.
WaiteesMap waitees_;

// Whether or not the waiter is actually waiting.
bool waiting_;

// Whether or not the waiter was woken up.
bool woken_up_;

Expand Down
2 changes: 1 addition & 1 deletion third_party/libevent/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ timeout_correct(struct event_base *base, struct timeval *tv)
pev = base->timeheap.p;
size = base->timeheap.n;
for (; size-- > 0; ++pev) {
struct evtimeval *ev_tv = &(**pev).ev_timeout;
struct timeval *ev_tv = &(**pev).ev_timeout;
evutil_timersub(ev_tv, &off, ev_tv);
}
/* Now remember what the new time turned out to be. */
Expand Down
18 changes: 0 additions & 18 deletions third_party/libevent/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,6 @@ typedef unsigned short u_short;
#define EV_SIGNAL 0x08
#define EV_PERSIST 0x10 /* Persistant event */

#ifdef STARBOARD
struct evtimeval {
#if SB_IS(64_BIT)
int64_t tv_sec; /* seconds */
int64_t tv_usec; /* and microseconds */
#else
int32_t tv_sec; /* seconds */
int32_t tv_usec; /* and microseconds */
#endif
};
#else
typedef struct timeval evtimeval;
#endif

/* Fix so that ppl dont have to run with <sys/queue.h> */
#ifndef TAILQ_ENTRY
#define _EVENT_DEFINED_TQENTRY
Expand All @@ -243,11 +229,7 @@ struct event {
short ev_ncalls;
short *ev_pncalls; /* Allows deletes in callback */

#if defined(STARBOARD)
struct evtimeval ev_timeout;
#else
struct timeval ev_timeout;
#endif

int ev_pri; /* smaller numbers are higher priority */

Expand Down
Loading