Skip to content

Commit ab9e35e

Browse files
committed
Revert ->tid simplification
1 parent 586b49f commit ab9e35e

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

system/lib/pthread/library_pthread.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void emscripten_main_thread_process_queued_calls() {
101101
}
102102

103103
int _emscripten_thread_is_valid(pthread_t thread) {
104-
return thread->tid;
104+
return thread->self == thread;
105105
}
106106

107107
static 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)))
112112
void _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.

system/lib/pthread/pthread_create.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)