@@ -312,10 +312,29 @@ void _emscripten_thread_exit(void* result) {
312312 // Call into the musl function that runs destructors of all thread-specific data.
313313 __pthread_tsd_run_dtors ();
314314
315- if (!-- libc .threads_minus_1 ) libc .need_locks = 0 ;
316-
317315 __tl_lock ();
318316
317+ /* Process robust list in userspace to handle non-pshared mutexes
318+ * and the detached thread case where the robust list head will
319+ * be invalid when the kernel would process it. */
320+ __vm_lock ();
321+ volatile void * volatile * rp ;
322+ while ((rp = self -> robust_list .head ) && rp != & self -> robust_list .head ) {
323+ pthread_mutex_t * m = (void * )((char * )rp
324+ - offsetof(pthread_mutex_t , _m_next ));
325+ int waiters = m -> _m_waiters ;
326+ int priv = (m -> _m_type & 128 ) ^ 128 ;
327+ self -> robust_list .pending = rp ;
328+ self -> robust_list .head = * rp ;
329+ int cont = a_swap (& m -> _m_lock , 0x40000000 );
330+ self -> robust_list .pending = 0 ;
331+ if (cont < 0 || waiters )
332+ __wake (& m -> _m_lock , 1 , priv );
333+ }
334+ __vm_unlock ();
335+
336+ if (!-- libc .threads_minus_1 ) libc .need_locks = 0 ;
337+
319338 self -> next -> prev = self -> prev ;
320339 self -> prev -> next = self -> next ;
321340 self -> prev = self -> next = self ;
0 commit comments