提交 7865d569 编写于 作者: R Rich Felker

make thread list lock a recursive lock

this is a prerequisite for factoring the membarrier fallback code into
a function that can be called from a context with the thread list
already locked or independently.
上级 609dd57c
...@@ -17,28 +17,38 @@ weak_alias(dummy_0, __do_orphaned_stdio_locks); ...@@ -17,28 +17,38 @@ weak_alias(dummy_0, __do_orphaned_stdio_locks);
weak_alias(dummy_0, __dl_thread_cleanup); weak_alias(dummy_0, __dl_thread_cleanup);
weak_alias(dummy_0, __dl_prepare_for_threads); weak_alias(dummy_0, __dl_prepare_for_threads);
static int tl_lock_count;
static int tl_lock_waiters;
void __tl_lock(void) void __tl_lock(void)
{ {
if (!a_cas(&__thread_list_lock, 0, 1)) return; int tid = __pthread_self()->tid;
do { int val = __thread_list_lock;
a_cas(&__thread_list_lock, 1, 2); if (val == tid) {
__futexwait(&__thread_list_lock, 2, 0); tl_lock_count++;
} while (a_cas(&__thread_list_lock, 0, 2)); return;
}
while ((val = a_cas(&__thread_list_lock, 0, tid)))
__wait(&__thread_list_lock, &tl_lock_waiters, val, 0);
} }
void __tl_unlock(void) void __tl_unlock(void)
{ {
if (a_swap(&__thread_list_lock, 0)==2) if (tl_lock_count) {
__wake(&__thread_list_lock, 1, 0); tl_lock_count--;
return;
}
a_store(&__thread_list_lock, 0);
if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0);
} }
void __tl_sync(pthread_t td) void __tl_sync(pthread_t td)
{ {
a_barrier(); a_barrier();
if (!__thread_list_lock) return; int val = __thread_list_lock;
a_cas(&__thread_list_lock, 1, 2); if (!val) return;
__wait(&__thread_list_lock, 0, 2, 0); __wait(&__thread_list_lock, &tl_lock_waiters, val, 0);
__wake(&__thread_list_lock, 1, 0); if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0);
} }
_Noreturn void __pthread_exit(void *result) _Noreturn void __pthread_exit(void *result)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册