]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/threads_win.c
2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 /* 0x400 is the spin count value suggested in the documentation */
28 if (!InitializeCriticalSectionAndSpinCount(lock
, 0x400)) {
36 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK
*lock
)
38 EnterCriticalSection(lock
);
42 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK
*lock
)
44 EnterCriticalSection(lock
);
48 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK
*lock
)
50 LeaveCriticalSection(lock
);
54 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK
*lock
)
59 DeleteCriticalSection(lock
);
65 # define ONCE_UNINITED 0
66 # define ONCE_ININIT 1
70 * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
71 * we still have to support.
73 int CRYPTO_THREAD_run_once(CRYPTO_ONCE
*once
, void (*init
)(void))
75 LONG
volatile *lock
= (LONG
*)once
;
78 if (*lock
== ONCE_DONE
)
82 result
= InterlockedCompareExchange(lock
, ONCE_ININIT
, ONCE_UNINITED
);
83 if (result
== ONCE_UNINITED
) {
88 } while (result
== ONCE_ININIT
);
90 return (*lock
== ONCE_DONE
);
93 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL
*key
, void (*cleanup
)(void *))
96 if (*key
== TLS_OUT_OF_INDEXES
)
102 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL
*key
)
108 * TlsGetValue clears the last error even on success, so that callers may
109 * distinguish it successfully returning NULL or failing. It is documented
110 * to never fail if the argument is a valid index from TlsAlloc, so we do
111 * not need to handle this.
113 * However, this error-mangling behavior interferes with the caller's use of
114 * GetLastError. In particular SSL_get_error queries the error queue to
115 * determine whether the caller should look at the OS's errors. To avoid
116 * destroying state, save and restore the Windows error.
118 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
120 last_error
= GetLastError();
121 ret
= TlsGetValue(*key
);
122 SetLastError(last_error
);
126 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL
*key
, void *val
)
128 if (TlsSetValue(*key
, val
) == 0)
134 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL
*key
)
136 if (TlsFree(*key
) == 0)
142 CRYPTO_THREAD_ID
CRYPTO_THREAD_get_current_id(void)
144 return GetCurrentThreadId();
147 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a
, CRYPTO_THREAD_ID b
)
152 int CRYPTO_atomic_add(int *val
, int amount
, int *ret
, CRYPTO_RWLOCK
*lock
)
154 *ret
= InterlockedExchangeAdd(val
, amount
) + amount
;
158 int openssl_init_fork_handlers(void)