2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 # if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
18 * VC++ 2008 or earlier x86 compilers do not have an inline implementation
19 * of InterlockedOr64 for 32bit and will fail to run on Windows XP 32bit.
20 * https://docs.microsoft.com/en-us/cpp/intrinsics/interlockedor-intrinsic-functions#requirements
21 * To work around this problem, we implement a manual locking mechanism for
22 * only VC++ 2008 or earlier x86 compilers.
25 #if (defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER <= 1500)
26 # define NO_INTERLOCKEDOR64
29 #include <openssl/crypto.h>
31 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
40 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
44 CRYPTO_win_rwlock *rwlock;
46 if ((lock = CRYPTO_zalloc(sizeof(CRYPTO_win_rwlock), NULL, 0)) == NULL)
47 /* Don't set error, to avoid recursion blowup. */
50 InitializeSRWLock(&rwlock->lock);
53 if ((lock = CRYPTO_zalloc(sizeof(CRITICAL_SECTION), NULL, 0)) == NULL)
54 /* Don't set error, to avoid recursion blowup. */
57 # if !defined(_WIN32_WCE)
58 /* 0x400 is the spin count value suggested in the documentation */
59 if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) {
64 InitializeCriticalSection(lock);
71 __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
74 CRYPTO_win_rwlock *rwlock = lock;
76 AcquireSRWLockShared(&rwlock->lock);
78 EnterCriticalSection(lock);
83 __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
86 CRYPTO_win_rwlock *rwlock = lock;
88 AcquireSRWLockExclusive(&rwlock->lock);
89 rwlock->exclusive = 1;
91 EnterCriticalSection(lock);
96 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
99 CRYPTO_win_rwlock *rwlock = lock;
101 if (rwlock->exclusive) {
102 rwlock->exclusive = 0;
103 ReleaseSRWLockExclusive(&rwlock->lock);
105 ReleaseSRWLockShared(&rwlock->lock);
108 LeaveCriticalSection(lock);
113 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
119 DeleteCriticalSection(lock);
126 # define ONCE_UNINITED 0
127 # define ONCE_ININIT 1
131 * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
132 * we still have to support.
134 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
136 LONG volatile *lock = (LONG *)once;
139 if (*lock == ONCE_DONE)
143 result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
144 if (result == ONCE_UNINITED) {
149 } while (result == ONCE_ININIT);
151 return (*lock == ONCE_DONE);
154 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
157 if (*key == TLS_OUT_OF_INDEXES)
163 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
169 * TlsGetValue clears the last error even on success, so that callers may
170 * distinguish it successfully returning NULL or failing. It is documented
171 * to never fail if the argument is a valid index from TlsAlloc, so we do
172 * not need to handle this.
174 * However, this error-mangling behavior interferes with the caller's use of
175 * GetLastError. In particular SSL_get_error queries the error queue to
176 * determine whether the caller should look at the OS's errors. To avoid
177 * destroying state, save and restore the Windows error.
179 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
181 last_error = GetLastError();
182 ret = TlsGetValue(*key);
183 SetLastError(last_error);
187 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
189 if (TlsSetValue(*key, val) == 0)
195 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
197 if (TlsFree(*key) == 0)
203 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
205 return GetCurrentThreadId();
208 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
213 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
215 *ret = (int)InterlockedExchangeAdd((long volatile *)val, (long)amount) + amount;
219 int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
222 #if (defined(NO_INTERLOCKEDOR64))
223 if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
228 if (!CRYPTO_THREAD_unlock(lock))
233 *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, (LONG64)op) | op;
238 int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
240 #if (defined(NO_INTERLOCKEDOR64))
241 if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
244 if (!CRYPTO_THREAD_unlock(lock))
249 *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, 0);
254 int openssl_init_fork_handlers(void)
259 int openssl_get_fork_id(void)