Build: change remaining $unified_info{install} checks to use attributes
[openssl.git] / crypto / threads_win.c
1 /*
2  * Copyright 2016-2018 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 #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;
21
22     if ((lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION))) == NULL) {
23         /* Don't set error, to avoid recursion blowup. */
24         return NULL;
25     }
26
27     /* 0x400 is the spin count value suggested in the documentation */
28     if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) {
29         OPENSSL_free(lock);
30         return NULL;
31     }
32
33     return lock;
34 }
35
36 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
37 {
38     EnterCriticalSection(lock);
39     return 1;
40 }
41
42 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
43 {
44     EnterCriticalSection(lock);
45     return 1;
46 }
47
48 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
49 {
50     LeaveCriticalSection(lock);
51     return 1;
52 }
53
54 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
55 {
56     if (lock == NULL)
57         return;
58
59     DeleteCriticalSection(lock);
60     OPENSSL_free(lock);
61
62     return;
63 }
64
65 #  define ONCE_UNINITED     0
66 #  define ONCE_ININIT       1
67 #  define ONCE_DONE         2
68
69 /*
70  * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
71  * we still have to support.
72  */
73 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
74 {
75     LONG volatile *lock = (LONG *)once;
76     LONG result;
77
78     if (*lock == ONCE_DONE)
79         return 1;
80
81     do {
82         result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
83         if (result == ONCE_UNINITED) {
84             init();
85             *lock = ONCE_DONE;
86             return 1;
87         }
88     } while (result == ONCE_ININIT);
89
90     return (*lock == ONCE_DONE);
91 }
92
93 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
94 {
95     *key = TlsAlloc();
96     if (*key == TLS_OUT_OF_INDEXES)
97         return 0;
98
99     return 1;
100 }
101
102 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
103 {
104     DWORD last_error;
105     void *ret;
106
107     /*
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.
112      *
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.
117      *
118      * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
119      */
120     last_error = GetLastError();
121     ret = TlsGetValue(*key);
122     SetLastError(last_error);
123     return ret;
124 }
125
126 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
127 {
128     if (TlsSetValue(*key, val) == 0)
129         return 0;
130
131     return 1;
132 }
133
134 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
135 {
136     if (TlsFree(*key) == 0)
137         return 0;
138
139     return 1;
140 }
141
142 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
143 {
144     return GetCurrentThreadId();
145 }
146
147 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
148 {
149     return (a == b);
150 }
151
152 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
153 {
154     *ret = InterlockedExchangeAdd(val, amount) + amount;
155     return 1;
156 }
157
158 int openssl_init_fork_handlers(void)
159 {
160     return 0;
161 }
162
163 #endif