@@ -65,54 +65,32 @@ int sched_get_priority_min(int policy) {
6565 return 0 ;
6666}
6767
68- int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t * restrict attr , int * restrict prioceiling )
69- {
68+ int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t * restrict attr , int * restrict prioceiling ) {
7069 // Not supported either in Emscripten or musl, return a faked value.
7170 if (prioceiling ) * prioceiling = 99 ;
7271 return 0 ;
7372}
7473
75- int pthread_mutexattr_setprioceiling (pthread_mutexattr_t * attr , int prioceiling )
76- {
74+ int pthread_mutexattr_setprioceiling (pthread_mutexattr_t * attr , int prioceiling ) {
7775 // Not supported either in Emscripten or musl, return an error.
7876 return EPERM ;
7977}
8078
81- static uint32_t dummyZeroAddress = 0 ;
82-
8379void emscripten_thread_sleep (double msecs ) {
84- double now = emscripten_get_now ();
85- double target = now + msecs ;
86-
87- // If we have less than this many msecs left to wait, busy spin that instead.
88- double min_ms_slice_to_sleep = 0.1 ;
89-
90- // Break up sleeping so that we process proxied work at regular intervals.
91- // TODO(sbc): This should be removed and/or moved down into
92- // `emscripten_futex_wait`.
93- double max_ms_slice_to_sleep = 100 ;
94-
95- emscripten_conditional_set_current_thread_status (
96- EM_THREAD_STATUS_RUNNING , EM_THREAD_STATUS_SLEEPING );
97-
98- do {
99- // Keep processing the main loop of the calling thread.
100- __pthread_testcancel (); // pthreads spec: sleep is a cancellation point, so must test if this
101- // thread is cancelled during the sleep.
102- emscripten_current_thread_process_queued_calls ();
103-
104- now = emscripten_get_now ();
105- double ms_to_sleep = target - now ;
106- if (ms_to_sleep < min_ms_slice_to_sleep )
107- continue ;
108- if (ms_to_sleep > max_ms_slice_to_sleep )
109- ms_to_sleep = max_ms_slice_to_sleep ;
110- emscripten_futex_wait (& dummyZeroAddress , 0 , ms_to_sleep );
111- now = emscripten_get_now ();
112- } while (now < target );
113-
114- emscripten_conditional_set_current_thread_status (
115- EM_THREAD_STATUS_SLEEPING , EM_THREAD_STATUS_RUNNING );
80+ // We include emscripten_current_thread_process_queued_calls before and
81+ // after sleeping since that is how we recieve "async" signals.
82+ // We include __pthread_testcancel here becuase clock_nanosleep is
83+ // a pthread cancelation point.
84+ emscripten_current_thread_process_queued_calls ();
85+ __pthread_testcancel ();
86+ emscripten_conditional_set_current_thread_status (EM_THREAD_STATUS_RUNNING ,
87+ EM_THREAD_STATUS_SLEEPING );
88+ uint32_t dummyZeroAddress = 0 ;
89+ emscripten_futex_wait (& dummyZeroAddress , 0 , msecs );
90+ emscripten_conditional_set_current_thread_status (EM_THREAD_STATUS_SLEEPING ,
91+ EM_THREAD_STATUS_RUNNING );
92+ emscripten_current_thread_process_queued_calls ();
93+ __pthread_testcancel ();
11694}
11795
11896static struct pthread __main_pthread ;
0 commit comments