]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/threads_win.c
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/crypto.h>
12 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
14 CRYPTO_RWLOCK
*CRYPTO_THREAD_lock_new(void)
16 CRYPTO_RWLOCK
*lock
= OPENSSL_zalloc(sizeof(CRITICAL_SECTION
));
20 /* 0x400 is the spin count value suggested in the documentation */
21 if (!InitializeCriticalSectionAndSpinCount(lock
, 0x400)) {
29 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK
*lock
)
31 EnterCriticalSection(lock
);
35 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK
*lock
)
37 EnterCriticalSection(lock
);
41 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK
*lock
)
43 LeaveCriticalSection(lock
);
47 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK
*lock
)
52 DeleteCriticalSection(lock
);
58 # if _WIN32_WINNT < 0x0600
60 # define ONCE_UNINITED 0
61 # define ONCE_ININIT 1
64 int CRYPTO_THREAD_run_once(CRYPTO_ONCE
*once
, void (*init
)(void))
66 LONG
volatile *lock
= (LONG
*)once
;
69 if (*lock
== ONCE_DONE
)
73 result
= InterlockedCompareExchange(lock
, ONCE_ININIT
, ONCE_UNINITED
);
74 if (result
== ONCE_UNINITED
) {
79 } while (result
== ONCE_ININIT
);
81 return (*lock
== ONCE_DONE
);
86 BOOL CALLBACK
once_cb(PINIT_ONCE once
, PVOID p
, PVOID
*pp
)
88 void (*init
)(void) = p
;
95 int CRYPTO_THREAD_run_once(CRYPTO_ONCE
*once
, void (*init
)(void))
97 if (InitOnceExecuteOnce(once
, once_cb
, init
, NULL
))
105 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL
*key
, void (*cleanup
)(void *))
108 if (*key
== TLS_OUT_OF_INDEXES
)
114 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL
*key
)
116 return TlsGetValue(*key
);
119 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL
*key
, void *val
)
121 if (TlsSetValue(*key
, val
) == 0)
127 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL
*key
)
129 if (TlsFree(*key
) == 0)
135 CRYPTO_THREAD_ID
CRYPTO_THREAD_get_current_id(void)
137 return GetCurrentThreadId();
140 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a
, CRYPTO_THREAD_ID b
)
145 int CRYPTO_atomic_add(int *val
, int amount
, int *ret
, CRYPTO_RWLOCK
*lock
)
147 *ret
= InterlockedExchangeAdd(val
, amount
) + amount
;