From eb7d5b69e97f8e7982f59a3ce64da15af4940dfd Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Sun, 20 May 2018 17:44:53 -0400 Subject: [PATCH] Replace the last CRITICAL_SECTION with SRWLOCK. We don't support Windows XP, so we can rely on SRWLOCK. Per https://crbug.com/592752, SRWLOCKs are more efficient and less of a hassle to use. We'd previously converted CRYPTO_MUTEX to SRWLOCK, but I missed this one. Not that this one lock matters much, may as well. It's less initialization code. Change-Id: I7ae435be5202b0a19f42015c9abff932dc04dbc7 Reviewed-on: https://boringssl-review.googlesource.com/c/33445 Commit-Queue: David Benjamin Commit-Queue: Adam Langley Reviewed-by: Adam Langley --- crypto/thread_win.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/crypto/thread_win.c b/crypto/thread_win.c index 45011650..c8e19f51 100644 --- a/crypto/thread_win.c +++ b/crypto/thread_win.c @@ -82,7 +82,7 @@ void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) { ReleaseSRWLockExclusive(&lock->lock); } -static CRITICAL_SECTION g_destructors_lock; +static SRWLOCK g_destructors_lock = SRWLOCK_INIT; static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS]; static CRYPTO_once_t g_thread_local_init_once = CRYPTO_ONCE_INIT; @@ -90,10 +90,6 @@ static DWORD g_thread_local_key; static int g_thread_local_failed; static void thread_local_init(void) { - if (!InitializeCriticalSectionAndSpinCount(&g_destructors_lock, 0x400)) { - g_thread_local_failed = 1; - return; - } g_thread_local_key = TlsAlloc(); g_thread_local_failed = (g_thread_local_key == TLS_OUT_OF_INDEXES); } @@ -121,12 +117,11 @@ static void NTAPI thread_local_destructor(PVOID module, DWORD reason, thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS]; - EnterCriticalSection(&g_destructors_lock); + AcquireSRWLockExclusive(&g_destructors_lock); OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors)); - LeaveCriticalSection(&g_destructors_lock); + ReleaseSRWLockExclusive(&g_destructors_lock); - unsigned i; - for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) { + for (unsigned i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) { if (destructors[i] != NULL) { destructors[i](pointers[i]); } @@ -250,9 +245,9 @@ int CRYPTO_set_thread_local(thread_local_data_t index, void *value, } } - EnterCriticalSection(&g_destructors_lock); + AcquireSRWLockExclusive(&g_destructors_lock); g_destructors[index] = destructor; - LeaveCriticalSection(&g_destructors_lock); + ReleaseSRWLockExclusive(&g_destructors_lock); pointers[index] = value; return 1;