Fixed deadlock in CRYPTO_THREAD_run_once for Windows
[openssl.git] / crypto / threads_win.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #if defined(_WIN32)
11 # include <windows.h>
12 #endif
13
14 #include <openssl/crypto.h>
15
16 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
17
18 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
19 {
20     CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION));
21     if (lock == NULL)
22         return NULL;
23
24     /* 0x400 is the spin count value suggested in the documentation */
25     if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) {
26         OPENSSL_free(lock);
27         return NULL;
28     }
29
30     return lock;
31 }
32
33 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
34 {
35     EnterCriticalSection(lock);
36     return 1;
37 }
38
39 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
40 {
41     EnterCriticalSection(lock);
42     return 1;
43 }
44
45 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
46 {
47     LeaveCriticalSection(lock);
48     return 1;
49 }
50
51 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
52 {
53     if (lock == NULL)
54         return;
55
56     DeleteCriticalSection(lock);
57     OPENSSL_free(lock);
58
59     return;
60 }
61
62 #  define ONCE_UNINITED     0
63 #  define ONCE_ININIT       1
64 #  define ONCE_DONE         2
65
66 /*
67  * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
68  * we still have to support.
69  */
70 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
71 {
72     LONG volatile *lock = (LONG *)once;
73     LONG result;
74
75     if (*lock == ONCE_DONE)
76         return 1;
77
78     do {
79         result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
80         if (result == ONCE_UNINITED) {
81             *lock = ONCE_DONE;
82             init();
83             return 1;
84         }
85     } while (result == ONCE_ININIT);
86
87     return (*lock == ONCE_DONE);
88 }
89
90 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
91 {
92     *key = TlsAlloc();
93     if (*key == TLS_OUT_OF_INDEXES)
94         return 0;
95
96     return 1;
97 }
98
99 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
100 {
101     return TlsGetValue(*key);
102 }
103
104 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
105 {
106     if (TlsSetValue(*key, val) == 0)
107         return 0;
108
109     return 1;
110 }
111
112 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
113 {
114     if (TlsFree(*key) == 0)
115         return 0;
116
117     return 1;
118 }
119
120 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
121 {
122     return GetCurrentThreadId();
123 }
124
125 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
126 {
127     return (a == b);
128 }
129
130 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
131 {
132     *ret = InterlockedExchangeAdd(val, amount) + amount;
133     return 1;
134 }
135
136 #endif