Skip to content
Closed
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
14 changes: 13 additions & 1 deletion system/lib/libc/musl/src/thread/pthread_mutex_timedlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec
if ((m->_m_type&3) == PTHREAD_MUTEX_ERRORCHECK
&& (r&0x7fffffff) == __pthread_self()->tid)
return EDEADLK;

#ifdef __EMSCRIPTEN__
// If no time is given, do not sleep forever - there is a possible race
// condition here. We need to keep iterating in this loop, since _m_lock
// may be set to the value we want *just* before the __timedwait. __timedwait
// does not check if the value is what we want, it just checks for a wake on
// that address, so it would wait forever.
// Instead of picking some arbitrary time to wait, just keep busy-waiting.
if (!at) {
r = pthread_mutex_trylock(m);
if (r != EBUSY) return r;
}
continue;
#endif
a_inc(&m->_m_waiters);
t = r | 0x80000000;
a_cas(&m->_m_lock, r, t);
Expand Down