5 CRYPTO_set_locking_callback, CRYPTO_set_id_callback, CRYPTO_num_locks,
6 CRYPTO_set_dynlock_create_callback, CRYPTO_set_dynlock_lock_callback,
7 CRYPTO_set_dynlock_destroy_callback, CRYPTO_get_new_dynlockid,
8 CRYPTO_destroy_dynlockid, CRYPTO_lock - OpenSSL thread support
12 #include <openssl/crypto.h>
14 void CRYPTO_set_locking_callback(void (*locking_function)(int mode,
15 int n, const char *file, int line));
17 void CRYPTO_set_id_callback(unsigned long (*id_function)(void));
19 int CRYPTO_num_locks(void);
22 /* struct CRYPTO_dynlock_value needs to be defined by the user */
23 struct CRYPTO_dynlock_value;
25 void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *
26 (*dyn_create_function)(char *file, int line));
27 void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)
28 (int mode, struct CRYPTO_dynlock_value *l,
29 const char *file, int line));
30 void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)
31 (struct CRYPTO_dynlock_value *l, const char *file, int line));
33 int CRYPTO_get_new_dynlockid(void);
35 void CRYPTO_destroy_dynlockid(int i);
37 void CRYPTO_lock(int mode, int n, const char *file, int line);
39 #define CRYPTO_w_lock(type) \
40 CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
41 #define CRYPTO_w_unlock(type) \
42 CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
43 #define CRYPTO_r_lock(type) \
44 CRYPTO_lock(CRYPTO_LOCK|CRYPTO_READ,type,__FILE__,__LINE__)
45 #define CRYPTO_r_unlock(type) \
46 CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_READ,type,__FILE__,__LINE__)
47 #define CRYPTO_add(addr,amount,type) \
48 CRYPTO_add_lock(addr,amount,type,__FILE__,__LINE__)
52 OpenSSL can safely be used in multi-threaded applications provided
53 that at least two callback functions are set.
55 locking_function(int mode, int n, const char *file, int line) is
56 needed to perform locking on shared data structures.
57 (Note that OpenSSL uses a number of global data structures that
58 will be implicitly shared whenever multiple threads use OpenSSL.)
59 Multi-threaded applications will crash at random if it is not set.
61 locking_function() must be able to handle up to CRYPTO_num_locks()
62 different mutex locks. It sets the B<n>-th lock if B<mode> &
63 B<CRYPTO_LOCK>, and releases it otherwise.
65 B<file> and B<line> are the file number of the function setting the
66 lock. They can be useful for debugging.
68 id_function(void) is a function that returns a thread ID, for
69 instance, pthread_self(). It is not, needed on Windows nor on
70 platforms where getpid() returns a different ID for each thread.
71 However, even on those platforms, pthread_self() should be used, since
72 the behavior of getpid() may depend on the machine where the program
73 is being run, not the machine where the program is being compiled.
74 (For instance, Red Hat 8 Linux and earlier used LinuxThreads, whose
75 getpid() returns a different value for each thread; Red Hat 9 Linux
76 and later use NPTL, which is Posix-conformant, and thus whose getpid()
77 returns the same value for all threads in a process. But a program
78 compiled on Red Hat 8 and run on Red Hat 9 will by default see
79 getpid() returning the same value for all threads.)
81 Additionally, OpenSSL supports dynamic locks, and sometimes, some parts
82 of OpenSSL need it for better performance. To enable this, the following
88 Three additional callback function, dyn_create_function, dyn_lock_function
89 and dyn_destroy_function.
92 A structure defined with the data that each lock needs to handle.
96 struct CRYPTO_dynlock_value has to be defined to contain whatever structure
97 is needed to handle locks.
99 dyn_create_function(const char *file, int line) is needed to create a
100 lock. Multi-threaded applications might crash at random if it is not set.
102 dyn_lock_function(int mode, CRYPTO_dynlock *l, const char *file, int line)
103 is needed to perform locking off dynamic lock numbered n. Multi-threaded
104 applications might crash at random if it is not set.
106 dyn_destroy_function(CRYPTO_dynlock *l, const char *file, int line) is
107 needed to destroy the lock l. Multi-threaded applications might crash at
108 random if it is not set.
110 CRYPTO_get_new_dynlockid() is used to create locks. It will call
111 dyn_create_function for the actual creation.
113 CRYPTO_destroy_dynlockid() is used to destroy locks. It will call
114 dyn_destroy_function for the actual destruction.
116 CRYPTO_lock() is used to lock and unlock the locks. mode is a bitfield
117 describing what should be done with the lock. n is the number of the
118 lock as returned from CRYPTO_get_new_dynlockid(). mode can be combined
119 from the following values. These values are pairwise exclusive, with
120 undefined behaviour if misused (for example, CRYPTO_READ and CRYPTO_WRITE
121 should not be used together):
130 CRYPTO_num_locks() returns the required number of locks.
132 CRYPTO_get_new_dynlockid() returns the index to the newly created lock.
134 The other functions return no values.
138 You can find out if OpenSSL was configured with thread support:
140 #define OPENSSL_THREAD_DEFINES
141 #include <openssl/opensslconf.h>
142 #if defined(OPENSSL_THREADS)
143 // thread support enabled
148 Also, dynamic locks are currently not used internally by OpenSSL, but
149 may do so in the future.
153 B<crypto/threads/mttest.c> shows examples of the callback functions on
154 Solaris, Irix and Win32.
158 CRYPTO_set_locking_callback() and CRYPTO_set_id_callback() are
159 available in all versions of SSLeay and OpenSSL.
160 CRYPTO_num_locks() was added in OpenSSL 0.9.4.
161 All functions dealing with dynamic locks were added in OpenSSL 0.9.5b-dev.
165 L<crypto(3)|crypto(3)>