In cases where we ask PEM_def_callback for minimum 0 length, accept 0 length
[openssl.git] / crypto / threads_win.c
1 /*
2  * Copyright 2016-2018 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;
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     return TlsGetValue(*key);
105 }
106
107 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
108 {
109     if (TlsSetValue(*key, val) == 0)
110         return 0;
111
112     return 1;
113 }
114
115 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
116 {
117     if (TlsFree(*key) == 0)
118         return 0;
119
120     return 1;
121 }
122
123 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
124 {
125     return GetCurrentThreadId();
126 }
127
128 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
129 {
130     return (a == b);
131 }
132
133 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
134 {
135     *ret = InterlockedExchangeAdd(val, amount) + amount;
136     return 1;
137 }
138
139 int CRYPTO_atomic_read(int *val, int *ret, CRYPTO_RWLOCK *lock)
140 {
141     *ret = InterlockedCompareExchange(val, 0, 0);
142     return 1;
143 }
144
145 int CRYPTO_atomic_write(int *val, int n, CRYPTO_RWLOCK *lock)
146 {
147     InterlockedExchange(val, n);
148     return 1;
149 }
150
151 int openssl_init_fork_handlers(void)
152 {
153     return 0;
154 }
155
156 #endif