2 * Copyright 2016-2018 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
14 #include <openssl/crypto.h>
16 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
18 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
22 if ((lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION))) == NULL) {
23 /* Don't set error, to avoid recursion blowup. */
27 #if !defined(_WIN32_WCE)
28 /* 0x400 is the spin count value suggested in the documentation */
29 if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) {
34 InitializeCriticalSection(lock);
40 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
42 EnterCriticalSection(lock);
46 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
48 EnterCriticalSection(lock);
52 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
54 LeaveCriticalSection(lock);
58 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
63 DeleteCriticalSection(lock);
69 # define ONCE_UNINITED 0
70 # define ONCE_ININIT 1
74 * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
75 * we still have to support.
77 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
79 LONG volatile *lock = (LONG *)once;
82 if (*lock == ONCE_DONE)
86 result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
87 if (result == ONCE_UNINITED) {
92 } while (result == ONCE_ININIT);
94 return (*lock == ONCE_DONE);
97 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
100 if (*key == TLS_OUT_OF_INDEXES)
106 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
112 * TlsGetValue clears the last error even on success, so that callers may
113 * distinguish it successfully returning NULL or failing. It is documented
114 * to never fail if the argument is a valid index from TlsAlloc, so we do
115 * not need to handle this.
117 * However, this error-mangling behavior interferes with the caller's use of
118 * GetLastError. In particular SSL_get_error queries the error queue to
119 * determine whether the caller should look at the OS's errors. To avoid
120 * destroying state, save and restore the Windows error.
122 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
124 last_error = GetLastError();
125 ret = TlsGetValue(*key);
126 SetLastError(last_error);
130 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
132 if (TlsSetValue(*key, val) == 0)
138 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
140 if (TlsFree(*key) == 0)
146 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
148 return GetCurrentThreadId();
151 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
156 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
158 *ret = InterlockedExchangeAdd(val, amount) + amount;
162 int openssl_init_fork_handlers(void)