File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -101,7 +101,7 @@ void emscripten_main_thread_process_queued_calls() {
101101}
102102
103103int _emscripten_thread_is_valid (pthread_t thread ) {
104- return thread -> tid ;
104+ return thread -> self == thread ;
105105}
106106
107107static void * dummy_tsd [1 ] = { 0 };
@@ -110,7 +110,8 @@ weak_alias(dummy_tsd, __pthread_tsd_main);
110110// See system/lib/README.md for static constructor ordering.
111111__attribute__((constructor (48 )))
112112void _emscripten_init_main_thread (void ) {
113- // The pthread struct has a field that points to itself.
113+ // The pthread struct has a field that points to itself - this is used as
114+ // a magic ID to detect whether the pthread_t structure is 'alive'.
114115 __main_pthread .self = & __main_pthread ;
115116 __main_pthread .detach_state = DT_JOINABLE ;
116117 // pthread struct robust_list head should point to itself.
Original file line number Diff line number Diff line change @@ -160,10 +160,9 @@ int __pthread_create(pthread_t* restrict res,
160160 new -> map_base = block ;
161161 new -> map_size = size ;
162162
163- // The pthread struct has a field that points to itself.
163+ // The pthread struct has a field that points to itself - this is used as a
164+ // magic ID to detect whether the pthread_t structure is 'alive'.
164165 new -> self = new ;
165-
166- // Thread ID, this becomes zero when the thread is no longer available.
167166 new -> tid = _emscripten_get_next_tid ();
168167
169168 // pthread struct robust_list head should point to itself.
@@ -280,10 +279,10 @@ void _emscripten_thread_free_data(pthread_t t) {
280279 emscripten_builtin_free (t -> profilerBlock );
281280 }
282281#endif
283- // The tid may be reused. Clear it to prevent inadvertent use
284- // and inform functions that would use it that it's no longer
285- // available.
286- t -> tid = 0 ;
282+ // Clear the self-reference to prevent inadvertent use and
283+ // inform functions that validate it that the thread is no
284+ // longer available.
285+ t -> self = NULL ;
287286
288287 // Free the entire thread block (called map_base because
289288 // musl normally allocates this using mmap). This region
You can’t perform that action at this time.
0 commit comments