diff options
Diffstat (limited to 'src/lib/libcrypto/crypto_lock.c')
-rw-r--r-- | src/lib/libcrypto/crypto_lock.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/lib/libcrypto/crypto_lock.c b/src/lib/libcrypto/crypto_lock.c new file mode 100644 index 0000000000..3d615cf485 --- /dev/null +++ b/src/lib/libcrypto/crypto_lock.c | |||
@@ -0,0 +1,55 @@ | |||
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 <pthread.h> | ||
19 | |||
20 | #include <openssl/crypto.h> | ||
21 | |||
22 | static pthread_mutex_t locks[CRYPTO_NUM_LOCKS]; | ||
23 | |||
24 | void | ||
25 | crypto_init_locks(void) | ||
26 | { | ||
27 | int i; | ||
28 | |||
29 | for (i = 0; i < CRYPTO_NUM_LOCKS; i++) | ||
30 | pthread_mutex_init(&locks[i], NULL); | ||
31 | } | ||
32 | |||
33 | void | ||
34 | CRYPTO_lock(int mode, int type, const char *file, int line) | ||
35 | { | ||
36 | if (type < 0 || type >= CRYPTO_NUM_LOCKS) | ||
37 | return; | ||
38 | |||
39 | if (mode & CRYPTO_LOCK) | ||
40 | pthread_mutex_lock(&locks[type]); | ||
41 | else | ||
42 | pthread_mutex_unlock(&locks[type]); | ||
43 | } | ||
44 | |||
45 | int | ||
46 | CRYPTO_add_lock(int *pointer, int amount, int type, const char *file, | ||
47 | int line) | ||
48 | { | ||
49 | int ret = 0; | ||
50 | CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE, type, file, line); | ||
51 | ret = *pointer + amount; | ||
52 | *pointer = ret; | ||
53 | CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE, type, file, line); | ||
54 | return (ret); | ||
55 | } | ||