@@ -148,20 +148,22 @@ int __pthread_create(pthread_t* restrict res,
148
148
//
149
149
// 1. pthread struct (sizeof struct pthread)
150
150
// 2. tls data (__builtin_wasm_tls_size())
151
- // 3. stack (_emscripten_default_pthread_stack_size() )
152
- // 4. tsd pointers (__pthread_tsd_size )
151
+ // 3. tsd pointers (__pthread_tsd_size )
152
+ // 4. stack (_emscripten_default_pthread_stack_size() )
153
153
size_t size = sizeof (struct pthread );
154
154
if (__builtin_wasm_tls_size ()) {
155
155
size += __builtin_wasm_tls_size () + __builtin_wasm_tls_align () - 1 ;
156
156
}
157
+ size += __pthread_tsd_size + TSD_ALIGN - 1 ;
158
+ size_t zero_size = size ;
157
159
if (!attr ._a_stackaddr ) {
158
160
size += attr ._a_stacksize + STACK_ALIGN - 1 ;
159
161
}
160
- size += __pthread_tsd_size + TSD_ALIGN - 1 ;
161
162
162
- // Allocate all the data for the new thread and zero-initialize.
163
+ // Allocate all the data for the new thread and zero-initialize all parts
164
+ // except for the stack.
163
165
unsigned char * block = emscripten_builtin_malloc (size );
164
- memset (block , 0 , size );
166
+ memset (block , 0 , zero_size );
165
167
166
168
uintptr_t offset = (uintptr_t )block ;
167
169
@@ -195,7 +197,14 @@ int __pthread_create(pthread_t* restrict res,
195
197
offset += __builtin_wasm_tls_size ();
196
198
}
197
199
198
- // 3. stack data
200
+ // 3. tsd slots
201
+ if (__pthread_tsd_size ) {
202
+ offset = ROUND_UP (offset , TSD_ALIGN );
203
+ new -> tsd = (void * )offset ;
204
+ offset += __pthread_tsd_size ;
205
+ }
206
+
207
+ // 4. stack data
199
208
// musl stores top of the stack in pthread_t->stack (i.e. the high
200
209
// end from which it grows down).
201
210
if (attr ._a_stackaddr ) {
@@ -205,13 +214,6 @@ int __pthread_create(pthread_t* restrict res,
205
214
new -> stack = (void * )offset ;
206
215
}
207
216
208
- // 4. tsd slots
209
- if (__pthread_tsd_size ) {
210
- offset = ROUND_UP (offset , TSD_ALIGN );
211
- new -> tsd = (void * )offset ;
212
- offset += __pthread_tsd_size ;
213
- }
214
-
215
217
// Check that we didn't use more data than we allocated.
216
218
assert (offset < (uintptr_t )block + size );
217
219
0 commit comments