aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrent Cook <bcook@openbsd.org>2018-11-11 09:06:13 -0600
committerBrent Cook <bcook@openbsd.org>2018-11-11 09:06:13 -0600
commitba2fe0e9499d718f14e4a1321008157d4e0a2a92 (patch)
tree0f7fe3cd77d25cce98432606afcb17ca9f630fa1
parent3172a42c45776e570d5157d19c175314a6948308 (diff)
downloadportable-ba2fe0e9499d718f14e4a1321008157d4e0a2a92.tar.gz
portable-ba2fe0e9499d718f14e4a1321008157d4e0a2a92.tar.bz2
portable-ba2fe0e9499d718f14e4a1321008157d4e0a2a92.zip
add simple Windows lock implementation
-rw-r--r--crypto/crypto_lock_win.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/crypto/crypto_lock_win.c b/crypto/crypto_lock_win.c
new file mode 100644
index 0000000..47c3dcc
--- /dev/null
+++ b/crypto/crypto_lock_win.c
@@ -0,0 +1,53 @@
1/* $OpenBSD: crypto_lock.c,v 1.1 2018/11/11 06:41:28 bcook Exp $ */
2/*
3 * Copyright (c) 2018 Brent Cook <bcook@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <windows.h>
19
20static HANDLE locks[CRYPTO_NUM_LOCKS];
21
22void
23crypto_init_locks(void)
24{
25 int i;
26
27 for (i = 0; i < CRYPTO_NUM_LOCKS; i++)
28 locks[i] = CreateMutex(NULL, FALSE, NULL);
29}
30
31void
32CRYPTO_lock(int mode, int type, const char *file, int line)
33{
34 if (type < 0 || type >= CRYPTO_NUM_LOCKS)
35 return;
36
37 if (mode & CRYPTO_LOCK)
38 WaitForSingleObject(locks[type], INFINITE);
39 else
40 ReleaseMutex(locks[type]);
41}
42
43int
44CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
45 int line)
46{
47 int ret = 0;
48 CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE, type, file, line);
49 ret = *pointer + amount;
50 *pointer = ret;
51 CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE, type, file, line);
52 return (ret);
53}