2 * Copyright 2016-2020 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
13 # include <synchapi.h>
18 #include <openssl/crypto.h>
20 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
29 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
33 CRYPTO_win_rwlock *rwlock;
35 if ((lock = OPENSSL_zalloc(sizeof(CRYPTO_win_rwlock))) == NULL)
38 InitializeSRWLock(&rwlock->lock);
41 if ((lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION))) == NULL) {
42 /* Don't set error, to avoid recursion blowup. */
46 # if !defined(_WIN32_WCE)
47 /* 0x400 is the spin count value suggested in the documentation */
48 if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) {
53 InitializeCriticalSection(lock);
60 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
63 CRYPTO_win_rwlock *rwlock = lock;
65 AcquireSRWLockShared(&rwlock->lock);
67 EnterCriticalSection(lock);
72 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
75 CRYPTO_win_rwlock *rwlock = lock;
77 AcquireSRWLockExclusive(&rwlock->lock);
78 rwlock->exclusive = 1;
80 EnterCriticalSection(lock);
85 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
88 CRYPTO_win_rwlock *rwlock = lock;
90 if (rwlock->exclusive) {
91 rwlock->exclusive = 0;
92 ReleaseSRWLockExclusive(&rwlock->lock);
94 ReleaseSRWLockShared(&rwlock->lock);
97 LeaveCriticalSection(lock);
102 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
108 DeleteCriticalSection(lock);
115 # define ONCE_UNINITED 0
116 # define ONCE_ININIT 1
120 * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
121 * we still have to support.
123 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
125 LONG volatile *lock = (LONG *)once;
128 if (*lock == ONCE_DONE)
132 result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
133 if (result == ONCE_UNINITED) {
138 } while (result == ONCE_ININIT);
140 return (*lock == ONCE_DONE);
143 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
146 if (*key == TLS_OUT_OF_INDEXES)
152 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
158 * TlsGetValue clears the last error even on success, so that callers may
159 * distinguish it successfully returning NULL or failing. It is documented
160 * to never fail if the argument is a valid index from TlsAlloc, so we do
161 * not need to handle this.
163 * However, this error-mangling behavior interferes with the caller's use of
164 * GetLastError. In particular SSL_get_error queries the error queue to
165 * determine whether the caller should look at the OS's errors. To avoid
166 * destroying state, save and restore the Windows error.
168 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
170 last_error = GetLastError();
171 ret = TlsGetValue(*key);
172 SetLastError(last_error);
176 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
178 if (TlsSetValue(*key, val) == 0)
184 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
186 if (TlsFree(*key) == 0)
192 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
194 return GetCurrentThreadId();
197 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
202 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
204 *ret = (int)InterlockedExchangeAdd((long volatile *)val, (long)amount) + amount;
208 int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
211 *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, (LONG64)op) | op;
215 int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
217 *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, 0);
221 int openssl_init_fork_handlers(void)
226 int openssl_get_fork_id(void)