diff options
author | bcook <> | 2018-11-11 06:41:28 +0000 |
---|---|---|
committer | bcook <> | 2018-11-11 06:41:28 +0000 |
commit | c3082ab78ba758ef51c552b6a91bdf1236de0cf8 (patch) | |
tree | 6d18ff101148258c403e820ff423d10a65fba89e /src/lib/libcrypto/crypto_lock.c | |
parent | 32ccc9aa9f3dd1c0efa02d4b21376521a91e4705 (diff) | |
download | openbsd-c3082ab78ba758ef51c552b6a91bdf1236de0cf8.tar.gz openbsd-c3082ab78ba758ef51c552b6a91bdf1236de0cf8.tar.bz2 openbsd-c3082ab78ba758ef51c552b6a91bdf1236de0cf8.zip |
Add automatic threading initialization for libcrypto.
This implements automatic thread support initialization in libcrypto.
This does not remove any functions from the ABI, but does turn them into
no-ops. Stub implementations of pthread_mutex_(init|lock|unlock) are
provided for ramdisks.
This does not implement the new OpenSSL 1.1 thread API internally,
keeping the original CRYTPO_lock / CRYPTO_add_lock functions for library
locking. For -portable, crypto_lock.c can be reimplemented with
OS-specific primitives as needed.
ok beck@, tb@, looks sane guenther@
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 | } | ||