@@ -933,14 +933,15 @@ static void thread_finalize(void *v) {
933
933
struct ThreadParam {
934
934
void * (*callback)(void *arg);
935
935
void *param;
936
- atomic_uintptr_t tid;
936
+ Tid tid;
937
+ Semaphore created;
938
+ Semaphore started;
937
939
};
938
940
939
941
extern " C" void *__tsan_thread_start_func (void *arg) {
940
942
ThreadParam *p = (ThreadParam*)arg;
941
943
void * (*callback)(void *arg) = p->callback ;
942
944
void *param = p->param ;
943
- int tid = 0 ;
944
945
{
945
946
cur_thread_init ();
946
947
ThreadState *thr = cur_thread ();
@@ -955,12 +956,11 @@ extern "C" void *__tsan_thread_start_func(void *arg) {
955
956
}
956
957
ThreadIgnoreEnd (thr);
957
958
#endif
958
- while ((tid = atomic_load (&p->tid , memory_order_acquire)) == 0 )
959
- internal_sched_yield ();
959
+ p->created .Wait ();
960
960
Processor *proc = ProcCreate ();
961
961
ProcWire (proc, thr);
962
- ThreadStart (thr, tid, GetTid (), ThreadType::Regular);
963
- atomic_store (& p->tid , 0 , memory_order_release );
962
+ ThreadStart (thr, p-> tid , GetTid (), ThreadType::Regular);
963
+ p->started . Post ( );
964
964
}
965
965
void *res = callback (param);
966
966
// Prevent the callback from being tail called,
@@ -999,7 +999,7 @@ TSAN_INTERCEPTOR(int, pthread_create,
999
999
ThreadParam p;
1000
1000
p.callback = callback;
1001
1001
p.param = param;
1002
- atomic_store (& p.tid , 0 , memory_order_relaxed) ;
1002
+ p.tid = kMainTid ;
1003
1003
int res = -1 ;
1004
1004
{
1005
1005
// Otherwise we see false positives in pthread stack manipulation.
@@ -1009,18 +1009,17 @@ TSAN_INTERCEPTOR(int, pthread_create,
1009
1009
ThreadIgnoreEnd (thr);
1010
1010
}
1011
1011
if (res == 0 ) {
1012
- Tid tid = ThreadCreate (thr, pc, *(uptr *)th, IsStateDetached (detached));
1013
- CHECK_NE (tid, kMainTid );
1012
+ p. tid = ThreadCreate (thr, pc, *(uptr *)th, IsStateDetached (detached));
1013
+ CHECK_NE (p. tid , kMainTid );
1014
1014
// Synchronization on p.tid serves two purposes:
1015
1015
// 1. ThreadCreate must finish before the new thread starts.
1016
1016
// Otherwise the new thread can call pthread_detach, but the pthread_t
1017
1017
// identifier is not yet registered in ThreadRegistry by ThreadCreate.
1018
1018
// 2. ThreadStart must finish before this thread continues.
1019
1019
// Otherwise, this thread can call pthread_detach and reset thr->sync
1020
1020
// before the new thread got a chance to acquire from it in ThreadStart.
1021
- atomic_store (&p.tid , tid, memory_order_release);
1022
- while (atomic_load (&p.tid , memory_order_acquire) != 0 )
1023
- internal_sched_yield ();
1021
+ p.created .Post ();
1022
+ p.started .Wait ();
1024
1023
}
1025
1024
if (attr == &myattr)
1026
1025
pthread_attr_destroy (&myattr);
0 commit comments