Fix up path generation to use OPENSSL_MODULES
[openssl.git] / crypto / threads_win.c
1 /*
2  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #if defined(_WIN32)
11 # include <windows.h>
12 # if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
13 #  define USE_RWLOCK
14 # endif
15 #endif
16
17 #include <openssl/crypto.h>
18
19 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
20
21 # ifdef USE_RWLOCK
22 typedef struct {
23     SRWLOCK lock;
24     int exclusive;
25 } CRYPTO_win_rwlock;
26 # endif
27
28 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
29 {
30     CRYPTO_RWLOCK *lock;
31 # ifdef USE_RWLOCK
32     CRYPTO_win_rwlock *rwlock;
33
34     if ((lock = OPENSSL_zalloc(sizeof(CRYPTO_win_rwlock))) == NULL)
35         return NULL;
36     rwlock = lock;
37     InitializeSRWLock(&rwlock->lock);
38 # else
39
40     if ((lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION))) == NULL) {
41         /* Don't set error, to avoid recursion blowup. */
42         return NULL;
43     }
44
45 #  if !defined(_WIN32_WCE)
46     /* 0x400 is the spin count value suggested in the documentation */
47     if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) {
48         OPENSSL_free(lock);
49         return NULL;
50     }
51 #  else
52     InitializeCriticalSection(lock);
53 #  endif
54 # endif
55
56     return lock;
57 }
58
59 __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
60 {
61 # ifdef USE_RWLOCK
62     CRYPTO_win_rwlock *rwlock = lock;
63
64     AcquireSRWLockShared(&rwlock->lock);
65 # else
66     EnterCriticalSection(lock);
67 # endif
68     return 1;
69 }
70
71 __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
72 {
73 # ifdef USE_RWLOCK
74     CRYPTO_win_rwlock *rwlock = lock;
75
76     AcquireSRWLockExclusive(&rwlock->lock);
77     rwlock->exclusive = 1;
78 # else
79     EnterCriticalSection(lock);
80 # endif
81     return 1;
82 }
83
84 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
85 {
86 # ifdef USE_RWLOCK
87     CRYPTO_win_rwlock *rwlock = lock;
88
89     if (rwlock->exclusive) {
90         rwlock->exclusive = 0;
91         ReleaseSRWLockExclusive(&rwlock->lock);
92     } else {
93         ReleaseSRWLockShared(&rwlock->lock);
94     }
95 # else
96     LeaveCriticalSection(lock);
97 # endif
98     return 1;
99 }
100
101 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
102 {
103     if (lock == NULL)
104         return;
105
106 # ifndef USE_RWLOCK
107     DeleteCriticalSection(lock);
108 # endif
109     OPENSSL_free(lock);
110
111     return;
112 }
113
114 # define ONCE_UNINITED     0
115 # define ONCE_ININIT       1
116 # define ONCE_DONE         2
117
118 /*
119  * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
120  * we still have to support.
121  */
122 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
123 {
124     LONG volatile *lock = (LONG *)once;
125     LONG result;
126
127     if (*lock == ONCE_DONE)
128         return 1;
129
130     do {
131         result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
132         if (result == ONCE_UNINITED) {
133             init();
134             *lock = ONCE_DONE;
135             return 1;
136         }
137     } while (result == ONCE_ININIT);
138
139     return (*lock == ONCE_DONE);
140 }
141
142 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
143 {
144     *key = TlsAlloc();
145     if (*key == TLS_OUT_OF_INDEXES)
146         return 0;
147
148     return 1;
149 }
150
151 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
152 {
153     DWORD last_error;
154     void *ret;
155
156     /*
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.
161      *
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.
166      *
167      * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
168      */
169     last_error = GetLastError();
170     ret = TlsGetValue(*key);
171     SetLastError(last_error);
172     return ret;
173 }
174
175 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
176 {
177     if (TlsSetValue(*key, val) == 0)
178         return 0;
179
180     return 1;
181 }
182
183 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
184 {
185     if (TlsFree(*key) == 0)
186         return 0;
187
188     return 1;
189 }
190
191 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
192 {
193     return GetCurrentThreadId();
194 }
195
196 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
197 {
198     return (a == b);
199 }
200
201 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
202 {
203     *ret = (int)InterlockedExchangeAdd((long volatile *)val, (long)amount) + amount;
204     return 1;
205 }
206
207 int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
208                      CRYPTO_RWLOCK *lock)
209 {
210     *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, (LONG64)op) | op;
211     return 1;
212 }
213
214 int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
215 {
216     *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, 0);
217     return 1;
218 }
219
220 int openssl_init_fork_handlers(void)
221 {
222     return 0;
223 }
224
225 int openssl_get_fork_id(void)
226 {
227     return 0;
228 }
229 #endif