From 31e72bc3289acdd85b0b745fbf64c5949ca33432 Mon Sep 17 00:00:00 2001 From: Jack Ren Date: Mon, 1 Aug 2011 18:44:55 +0800 Subject: [PATCH] bionic: fix __get_tls( ) crash issue When running the stress test of pthread create/destroy, a crash may oocur in __get_tls(). That is caused by the race condition with __set_tls( ): Author: Jack Ren Signed-off-by: Bruce Beare --- libc/arch-x86/bionic/__set_tls.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libc/arch-x86/bionic/__set_tls.c b/libc/arch-x86/bionic/__set_tls.c index 48b55f0e7..e5e43b591 100755 --- a/libc/arch-x86/bionic/__set_tls.c +++ b/libc/arch-x86/bionic/__set_tls.c @@ -60,6 +60,8 @@ static struct user_desc _tls_desc = 0 }; +static pthread_mutex_t _tls_desc_lock = PTHREAD_MUTEX_INITIALIZER; + struct _thread_area_head { void *self; }; @@ -71,6 +73,7 @@ int __set_tls(void *ptr) { int rc, segment; + pthread_mutex_lock(&_tls_desc_lock); _tls_desc.base_addr = (unsigned long)ptr; /* We also need to write the location of the tls to ptr[0] */ @@ -88,6 +91,8 @@ int __set_tls(void *ptr) asm __volatile__ ( " movw %w0, %%gs" :: "q"(segment) ); + pthread_mutex_unlock(&_tls_desc_lock); + return 0; }