1threads(3) OpenSSL threads(3)
2
3
4
6 CRYPTO_set_locking_callback, CRYPTO_set_id_callback, CRYPTO_num_locks,
7 CRYPTO_set_dynlock_create_callback, CRYPTO_set_dynlock_lock_callback,
8 CRYPTO_set_dynlock_destroy_callback, CRYPTO_get_new_dynlockid,
9 CRYPTO_destroy_dynlockid, CRYPTO_lock - OpenSSL thread support
10
12 #include <openssl/crypto.h>
13
14 void CRYPTO_set_locking_callback(void (*locking_function)(int mode,
15 int n, const char *file, int line));
16
17 void CRYPTO_set_id_callback(unsigned long (*id_function)(void));
18
19 int CRYPTO_num_locks(void);
20
21 /* struct CRYPTO_dynlock_value needs to be defined by the user */
22 struct CRYPTO_dynlock_value;
23
24 void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *
25 (*dyn_create_function)(char *file, int line));
26 void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)
27 (int mode, struct CRYPTO_dynlock_value *l,
28 const char *file, int line));
29 void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)
30 (struct CRYPTO_dynlock_value *l, const char *file, int line));
31
32 int CRYPTO_get_new_dynlockid(void);
33
34 void CRYPTO_destroy_dynlockid(int i);
35
36 void CRYPTO_lock(int mode, int n, const char *file, int line);
37
38 #define CRYPTO_w_lock(type) \
39 CRYPTO_lock(CRYPTO_LOCK⎪CRYPTO_WRITE,type,__FILE__,__LINE__)
40 #define CRYPTO_w_unlock(type) \
41 CRYPTO_lock(CRYPTO_UNLOCK⎪CRYPTO_WRITE,type,__FILE__,__LINE__)
42 #define CRYPTO_r_lock(type) \
43 CRYPTO_lock(CRYPTO_LOCK⎪CRYPTO_READ,type,__FILE__,__LINE__)
44 #define CRYPTO_r_unlock(type) \
45 CRYPTO_lock(CRYPTO_UNLOCK⎪CRYPTO_READ,type,__FILE__,__LINE__)
46 #define CRYPTO_add(addr,amount,type) \
47 CRYPTO_add_lock(addr,amount,type,__FILE__,__LINE__)
48
50 OpenSSL can safely be used in multi-threaded applications provided that
51 at least two callback functions are set.
52
53 locking_function(int mode, int n, const char *file, int line) is needed
54 to perform locking on shared data structures. (Note that OpenSSL uses
55 a number of global data structures that will be implicitly shared when‐
56 ever multiple threads use OpenSSL.) Multi-threaded applications will
57 crash at random if it is not set.
58
59 locking_function() must be able to handle up to CRYPTO_num_locks() dif‐
60 ferent mutex locks. It sets the n-th lock if mode & CRYPTO_LOCK, and
61 releases it otherwise.
62
63 file and line are the file number of the function setting the lock.
64 They can be useful for debugging.
65
66 id_function(void) is a function that returns a thread ID, for example
67 pthread_self() if it returns an integer (see NOTES below). It isn't
68 needed on Windows nor on platforms where getpid() returns a different
69 ID for each thread (see NOTES below).
70
71 Additionally, OpenSSL supports dynamic locks, and sometimes, some parts
72 of OpenSSL need it for better performance. To enable this, the follow‐
73 ing is required:
74
75 * Three additional callback function, dyn_create_function,
76 dyn_lock_function and dyn_destroy_function.
77 * A structure defined with the data that each lock needs to handle.
78
79 struct CRYPTO_dynlock_value has to be defined to contain whatever
80 structure is needed to handle locks.
81
82 dyn_create_function(const char *file, int line) is needed to create a
83 lock. Multi-threaded applications might crash at random if it is not
84 set.
85
86 dyn_lock_function(int mode, CRYPTO_dynlock *l, const char *file, int
87 line) is needed to perform locking off dynamic lock numbered n. Multi-
88 threaded applications might crash at random if it is not set.
89
90 dyn_destroy_function(CRYPTO_dynlock *l, const char *file, int line) is
91 needed to destroy the lock l. Multi-threaded applications might crash
92 at random if it is not set.
93
94 CRYPTO_get_new_dynlockid() is used to create locks. It will call
95 dyn_create_function for the actual creation.
96
97 CRYPTO_destroy_dynlockid() is used to destroy locks. It will call
98 dyn_destroy_function for the actual destruction.
99
100 CRYPTO_lock() is used to lock and unlock the locks. mode is a bitfield
101 describing what should be done with the lock. n is the number of the
102 lock as returned from CRYPTO_get_new_dynlockid(). mode can be combined
103 from the following values. These values are pairwise exclusive, with
104 undefined behaviour if misused (for example, CRYPTO_READ and
105 CRYPTO_WRITE should not be used together):
106
107 CRYPTO_LOCK 0x01
108 CRYPTO_UNLOCK 0x02
109 CRYPTO_READ 0x04
110 CRYPTO_WRITE 0x08
111
113 CRYPTO_num_locks() returns the required number of locks.
114
115 CRYPTO_get_new_dynlockid() returns the index to the newly created lock.
116
117 The other functions return no values.
118
120 You can find out if OpenSSL was configured with thread support:
121
122 #define OPENSSL_THREAD_DEFINES
123 #include <openssl/opensslconf.h>
124 #if defined(OPENSSL_THREADS)
125 // thread support enabled
126 #else
127 // no thread support
128 #endif
129
130 Also, dynamic locks are currently not used internally by OpenSSL, but
131 may do so in the future.
132
133 Defining id_function(void) has it's own issues. Generally speaking,
134 pthread_self() should be used, even on platforms where getpid() gives
135 different answers in each thread, since that may depend on the machine
136 the program is run on, not the machine where the program is being com‐
137 piled. For instance, Red Hat 8 Linux and earlier used LinuxThreads,
138 whose getpid() returns a different value for each thread. Red Hat 9
139 Linux and later use NPTL, which is Posix-conformant, and has a getpid()
140 that returns the same value for all threads in a process. A program
141 compiled on Red Hat 8 and run on Red Hat 9 will therefore see getpid()
142 returning the same value for all threads.
143
144 There is still the issue of platforms where pthread_self() returns
145 something other than an integer. This is a bit unusual, and this man‐
146 ual has no cookbook solution for that case.
147
149 crypto/threads/mttest.c shows examples of the callback functions on
150 Solaris, Irix and Win32.
151
153 CRYPTO_set_locking_callback() and CRYPTO_set_id_callback() are avail‐
154 able in all versions of SSLeay and OpenSSL. CRYPTO_num_locks() was
155 added in OpenSSL 0.9.4. All functions dealing with dynamic locks were
156 added in OpenSSL 0.9.5b-dev.
157
159 crypto(3)
160
161
162
1630.9.8b 2005-06-18 threads(3)