diff --git a/starboard/shared/libevent/socket_waiter_internal.cc b/starboard/shared/libevent/socket_waiter_internal.cc index 056cfd9a114f..1a815513ccdc 100644 --- a/starboard/shared/libevent/socket_waiter_internal.cc +++ b/starboard/shared/libevent/socket_waiter_internal.cc @@ -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); @@ -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; } @@ -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)); diff --git a/starboard/shared/libevent/socket_waiter_internal.h b/starboard/shared/libevent/socket_waiter_internal.h index 5da590902564..f5c8c49deb95 100644 --- a/starboard/shared/libevent/socket_waiter_internal.h +++ b/starboard/shared/libevent/socket_waiter_internal.h @@ -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_; diff --git a/third_party/libevent/event.c b/third_party/libevent/event.c index b151ebca969a..22bbef67e03a 100644 --- a/third_party/libevent/event.c +++ b/third_party/libevent/event.c @@ -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. */ diff --git a/third_party/libevent/event.h b/third_party/libevent/event.h index b4af24e7a43a..caff09ec0538 100644 --- a/third_party/libevent/event.h +++ b/third_party/libevent/event.h @@ -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 */ #ifndef TAILQ_ENTRY #define _EVENT_DEFINED_TQENTRY @@ -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 */