]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/threads_win.c
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
17 #include <openssl/crypto.h>
19 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
28 CRYPTO_RWLOCK
*CRYPTO_THREAD_lock_new(void)
32 CRYPTO_win_rwlock
*rwlock
;
34 if ((lock
= OPENSSL_zalloc(sizeof(CRYPTO_win_rwlock
))) == NULL
)
37 InitializeSRWLock(&rwlock
->lock
);
40 if ((lock
= OPENSSL_zalloc(sizeof(CRITICAL_SECTION
))) == NULL
) {
41 /* Don't set error, to avoid recursion blowup. */
45 # if !defined(_WIN32_WCE)
46 /* 0x400 is the spin count value suggested in the documentation */
47 if (!InitializeCriticalSectionAndSpinCount(lock
, 0x400)) {
52 InitializeCriticalSection(lock
);
59 __owur
int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK
*lock
)
62 CRYPTO_win_rwlock
*rwlock
= lock
;
64 AcquireSRWLockShared(&rwlock
->lock
);
66 EnterCriticalSection(lock
);
71 __owur
int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK
*lock
)
74 CRYPTO_win_rwlock
*rwlock
= lock
;
76 AcquireSRWLockExclusive(&rwlock
->lock
);
77 rwlock
->exclusive
= 1;
79 EnterCriticalSection(lock
);
84 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK
*lock
)
87 CRYPTO_win_rwlock
*rwlock
= lock
;
89 if (rwlock
->exclusive
) {
90 rwlock
->exclusive
= 0;
91 ReleaseSRWLockExclusive(&rwlock
->lock
);
93 ReleaseSRWLockShared(&rwlock
->lock
);
96 LeaveCriticalSection(lock
);
101 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK
*lock
)
107 DeleteCriticalSection(lock
);
114 # define ONCE_UNINITED 0
115 # define ONCE_ININIT 1
119 * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
120 * we still have to support.
122 int CRYPTO_THREAD_run_once(CRYPTO_ONCE
*once
, void (*init
)(void))
124 LONG
volatile *lock
= (LONG
*)once
;
127 if (*lock
== ONCE_DONE
)
131 result
= InterlockedCompareExchange(lock
, ONCE_ININIT
, ONCE_UNINITED
);
132 if (result
== ONCE_UNINITED
) {
137 } while (result
== ONCE_ININIT
);
139 return (*lock
== ONCE_DONE
);
142 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL
*key
, void (*cleanup
)(void *))
145 if (*key
== TLS_OUT_OF_INDEXES
)
151 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL
*key
)
157 * TlsGetValue clears the last error even on success, so that callers may
158 * distinguish it successfully returning NULL or failing. It is documented
159 * to never fail if the argument is a valid index from TlsAlloc, so we do
160 * not need to handle this.
162 * However, this error-mangling behavior interferes with the caller's use of
163 * GetLastError. In particular SSL_get_error queries the error queue to
164 * determine whether the caller should look at the OS's errors. To avoid
165 * destroying state, save and restore the Windows error.
167 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
169 last_error
= GetLastError();
170 ret
= TlsGetValue(*key
);
171 SetLastError(last_error
);
175 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL
*key
, void *val
)
177 if (TlsSetValue(*key
, val
) == 0)
183 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL
*key
)
185 if (TlsFree(*key
) == 0)
191 CRYPTO_THREAD_ID
CRYPTO_THREAD_get_current_id(void)
193 return GetCurrentThreadId();
196 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a
, CRYPTO_THREAD_ID b
)
201 int CRYPTO_atomic_add(int *val
, int amount
, int *ret
, CRYPTO_RWLOCK
*lock
)
203 *ret
= (int)InterlockedExchangeAdd((long volatile *)val
, (long)amount
) + amount
;
207 int CRYPTO_atomic_or(uint64_t *val
, uint64_t op
, uint64_t *ret
,
210 *ret
= (uint64_t)InterlockedOr64((LONG64
volatile *)val
, (LONG64
)op
) | op
;
214 int CRYPTO_atomic_load(uint64_t *val
, uint64_t *ret
, CRYPTO_RWLOCK
*lock
)
216 *ret
= (uint64_t)InterlockedOr64((LONG64
volatile *)val
, 0);
220 int openssl_init_fork_handlers(void)
225 int openssl_get_fork_id(void)