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