]>
Commit | Line | Data |
---|---|---|
aa6bb135 RS |
1 | /* |
2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. | |
71a04cfc | 3 | * |
aa6bb135 RS |
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 | |
71a04cfc AG |
8 | */ |
9 | ||
f1f5ee17 AP |
10 | #if defined(_WIN32) |
11 | # include <windows.h> | |
12 | #endif | |
13 | ||
71a04cfc | 14 | #include <openssl/crypto.h> |
71a04cfc AG |
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 */ | |
0b2fc928 F |
25 | if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) { |
26 | OPENSSL_free(lock); | |
71a04cfc | 27 | return NULL; |
0b2fc928 | 28 | } |
71a04cfc AG |
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 | ||
71a04cfc AG |
62 | # define ONCE_UNINITED 0 |
63 | # define ONCE_ININIT 1 | |
64 | # define ONCE_DONE 2 | |
65 | ||
fcb318c6 MC |
66 | /* |
67 | * We don't use InitOnceExecuteOnce because that isn't available in WinXP which | |
68 | * we still have to support. | |
69 | */ | |
71a04cfc AG |
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) { | |
349d1cfd | 81 | init(); |
1fda5bc4 | 82 | *lock = ONCE_DONE; |
71a04cfc AG |
83 | return 1; |
84 | } | |
85 | } while (result == ONCE_ININIT); | |
86 | ||
87 | return (*lock == ONCE_DONE); | |
88 | } | |
89 | ||
71a04cfc AG |
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 | ||
94683b7a P |
136 | int CRYPTO_atomic_read(int *val, int *ret, CRYPTO_RWLOCK *lock) |
137 | { | |
f49452c2 | 138 | *ret = InterlockedCompareExchange(val, 0, 0); |
94683b7a P |
139 | return 1; |
140 | } | |
141 | ||
30ff41be P |
142 | int CRYPTO_atomic_write(int *val, int n, CRYPTO_RWLOCK *lock) |
143 | { | |
144 | InterlockedExchange(val, n); | |
145 | return 1; | |
146 | } | |
147 | ||
2915fe19 RS |
148 | int openssl_init_fork_handlers(void) |
149 | { | |
150 | return 0; | |
151 | } | |
152 | ||
71a04cfc | 153 | #endif |