1 | /* $Id: threads_iprt.cpp 116670 2017-07-03 16:39:00Z fmehnert $ */
|
---|
2 | /** @file
|
---|
3 | * Crypto thread locking functions which make use of the IPRT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <openssl/crypto.h>
|
---|
19 |
|
---|
20 | #if defined(OPENSSL_THREADS)
|
---|
21 |
|
---|
22 | #include <iprt/asm.h>
|
---|
23 | #include <iprt/assert.h>
|
---|
24 | #include <iprt/critsect.h>
|
---|
25 | #include <iprt/err.h>
|
---|
26 | #include <iprt/log.h>
|
---|
27 |
|
---|
28 | /*
|
---|
29 | * Of course it's wrong to use a critical section to implement a read/write
|
---|
30 | * lock. But as the OpenSSL interface is too simple (there is only read_lock()/
|
---|
31 | * write_lock() and only unspecified unlock() and the Windows implementatio
|
---|
32 | * (threads_win.c) uses {Enter,Leave}CriticalSection we do that here as well.
|
---|
33 | */
|
---|
34 | CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
|
---|
35 | {
|
---|
36 | RTCRITSECT *pCritSect = (RTCRITSECT*)OPENSSL_zalloc(sizeof(RTCRITSECT));
|
---|
37 | if (!pCritSect)
|
---|
38 | return NULL;
|
---|
39 |
|
---|
40 | int rc = RTCritSectInitEx(pCritSect, 0, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
|
---|
41 | if (RT_FAILURE(rc))
|
---|
42 | {
|
---|
43 | OPENSSL_free(pCritSect);
|
---|
44 | return NULL;
|
---|
45 | }
|
---|
46 |
|
---|
47 | return (CRYPTO_RWLOCK*)pCritSect;
|
---|
48 | }
|
---|
49 |
|
---|
50 | int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
|
---|
51 | {
|
---|
52 | PRTCRITSECT pCritSect = (PRTCRITSECT)lock;
|
---|
53 | int rc = RTCritSectEnter(pCritSect);
|
---|
54 | AssertRC(rc);
|
---|
55 | if (RT_FAILURE(rc))
|
---|
56 | return 0;
|
---|
57 |
|
---|
58 | return 1;
|
---|
59 | }
|
---|
60 |
|
---|
61 | int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
|
---|
62 | {
|
---|
63 | PRTCRITSECT pCritSect = (PRTCRITSECT)lock;
|
---|
64 | int rc = RTCritSectEnter(pCritSect);
|
---|
65 | AssertRC(rc);
|
---|
66 | if (RT_FAILURE(rc))
|
---|
67 | return 0;
|
---|
68 |
|
---|
69 | return 1;
|
---|
70 | }
|
---|
71 |
|
---|
72 | int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
|
---|
73 | {
|
---|
74 | PRTCRITSECT pCritSect = (PRTCRITSECT)lock;
|
---|
75 | int rc = RTCritSectLeave(pCritSect);
|
---|
76 | AssertRC(rc);
|
---|
77 | if (RT_FAILURE(rc))
|
---|
78 | return 0;
|
---|
79 |
|
---|
80 | return 1;
|
---|
81 | }
|
---|
82 |
|
---|
83 | void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
|
---|
84 | {
|
---|
85 | if (!lock)
|
---|
86 | return;
|
---|
87 |
|
---|
88 | PRTCRITSECT pCritSect = (PRTCRITSECT)lock;
|
---|
89 | int rc = RTCritSectDelete(pCritSect);
|
---|
90 | AssertRC(rc);
|
---|
91 | OPENSSL_free(lock);
|
---|
92 | }
|
---|
93 |
|
---|
94 | int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
|
---|
95 | {
|
---|
96 | *key = RTTlsAlloc();
|
---|
97 | if (*key == NIL_RTTLS)
|
---|
98 | return 0;
|
---|
99 |
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 |
|
---|
103 | void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
|
---|
104 | {
|
---|
105 | return RTTlsGet(*key);
|
---|
106 | }
|
---|
107 |
|
---|
108 | int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
|
---|
109 | {
|
---|
110 | int rc = RTTlsSet(*key, val);
|
---|
111 | if (RT_FAILURE(rc))
|
---|
112 | return 0;
|
---|
113 |
|
---|
114 | return 1;
|
---|
115 | }
|
---|
116 |
|
---|
117 | int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
|
---|
118 | {
|
---|
119 | int rc = RTTlsFree(*key);
|
---|
120 | if (RT_FAILURE(rc))
|
---|
121 | return 0;
|
---|
122 |
|
---|
123 | return 1;
|
---|
124 | }
|
---|
125 |
|
---|
126 | CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
|
---|
127 | {
|
---|
128 | return RTThreadSelf();
|
---|
129 | }
|
---|
130 |
|
---|
131 | int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
|
---|
132 | {
|
---|
133 | return (a == b);
|
---|
134 | }
|
---|
135 |
|
---|
136 | int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
|
---|
137 | {
|
---|
138 | *ret = ASMAtomicAddS32((int32_t volatile*)val, amount) + amount;
|
---|
139 | return 1;
|
---|
140 | }
|
---|
141 |
|
---|
142 | #endif
|
---|