1=pod 2 3=head1 NAME 4 5CRYPTO_set_locking_callback, CRYPTO_set_id_callback, CRYPTO_num_locks, 6CRYPTO_set_dynlock_create_callback, CRYPTO_set_dynlock_lock_callback, 7CRYPTO_set_dynlock_destroy_callback, CRYPTO_get_new_dynlockid, 8CRYPTO_destroy_dynlockid, CRYPTO_lock - OpenSSL thread support 9 10=head1 SYNOPSIS 11 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 22 /* struct CRYPTO_dynlock_value needs to be defined by the user */ 23 struct CRYPTO_dynlock_value; 24 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)); 32 33 int CRYPTO_get_new_dynlockid(void); 34 35 void CRYPTO_destroy_dynlockid(int i); 36 37 void CRYPTO_lock(int mode, int n, const char *file, int line); 38 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__) 49 50=head1 DESCRIPTION 51 52OpenSSL can safely be used in multi-threaded applications provided 53that at least two callback functions are set. 54 55locking_function(int mode, int n, const char *file, int line) is 56needed to perform locking on shared data structures. 57(Note that OpenSSL uses a number of global data structures that 58will be implicitly shared whenever multiple threads use OpenSSL.) 59Multi-threaded applications will crash at random if it is not set. 60 61locking_function() must be able to handle up to CRYPTO_num_locks() 62different mutex locks. It sets the B<n>-th lock if B<mode> & 63B<CRYPTO_LOCK>, and releases it otherwise. 64 65B<file> and B<line> are the file number of the function setting the 66lock. They can be useful for debugging. 67 68id_function(void) is a function that returns a thread ID, for example 69pthread_self() if it returns an integer (see NOTES below). It isn't 70needed on Windows nor on platforms where getpid() returns a different 71ID for each thread (see NOTES below). 72 73Additionally, OpenSSL supports dynamic locks, and sometimes, some parts 74of OpenSSL need it for better performance. To enable this, the following 75is required: 76 77=over 4 78 79=item * 80Three additional callback function, dyn_create_function, dyn_lock_function 81and dyn_destroy_function. 82 83=item * 84A structure defined with the data that each lock needs to handle. 85 86=back 87 88struct CRYPTO_dynlock_value has to be defined to contain whatever structure 89is needed to handle locks. 90 91dyn_create_function(const char *file, int line) is needed to create a 92lock. Multi-threaded applications might crash at random if it is not set. 93 94dyn_lock_function(int mode, CRYPTO_dynlock *l, const char *file, int line) 95is needed to perform locking off dynamic lock numbered n. Multi-threaded 96applications might crash at random if it is not set. 97 98dyn_destroy_function(CRYPTO_dynlock *l, const char *file, int line) is 99needed to destroy the lock l. Multi-threaded applications might crash at 100random if it is not set. 101 102CRYPTO_get_new_dynlockid() is used to create locks. It will call 103dyn_create_function for the actual creation. 104 105CRYPTO_destroy_dynlockid() is used to destroy locks. It will call 106dyn_destroy_function for the actual destruction. 107 108CRYPTO_lock() is used to lock and unlock the locks. mode is a bitfield 109describing what should be done with the lock. n is the number of the 110lock as returned from CRYPTO_get_new_dynlockid(). mode can be combined 111from the following values. These values are pairwise exclusive, with 112undefined behaviour if misused (for example, CRYPTO_READ and CRYPTO_WRITE 113should not be used together): 114 115 CRYPTO_LOCK 0x01 116 CRYPTO_UNLOCK 0x02 117 CRYPTO_READ 0x04 118 CRYPTO_WRITE 0x08 119 120=head1 RETURN VALUES 121 122CRYPTO_num_locks() returns the required number of locks. 123 124CRYPTO_get_new_dynlockid() returns the index to the newly created lock. 125 126The other functions return no values. 127 128=head1 NOTES 129 130You can find out if OpenSSL was configured with thread support: 131 132 #define OPENSSL_THREAD_DEFINES 133 #include <openssl/opensslconf.h> 134 #if defined(OPENSSL_THREADS) 135 // thread support enabled 136 #else 137 // no thread support 138 #endif 139 140Also, dynamic locks are currently not used internally by OpenSSL, but 141may do so in the future. 142 143Defining id_function(void) has it's own issues. Generally speaking, 144pthread_self() should be used, even on platforms where getpid() gives 145different answers in each thread, since that may depend on the machine 146the program is run on, not the machine where the program is being 147compiled. For instance, Red Hat 8 Linux and earlier used 148LinuxThreads, whose getpid() returns a different value for each 149thread. Red Hat 9 Linux and later use NPTL, which is 150Posix-conformant, and has a getpid() that returns the same value for 151all threads in a process. A program compiled on Red Hat 8 and run on 152Red Hat 9 will therefore see getpid() returning the same value for 153all threads. 154 155There is still the issue of platforms where pthread_self() returns 156something other than an integer. This is a bit unusual, and this 157manual has no cookbook solution for that case. 158 159=head1 EXAMPLES 160 161B<crypto/threads/mttest.c> shows examples of the callback functions on 162Solaris, Irix and Win32. 163 164=head1 HISTORY 165 166CRYPTO_set_locking_callback() and CRYPTO_set_id_callback() are 167available in all versions of SSLeay and OpenSSL. 168CRYPTO_num_locks() was added in OpenSSL 0.9.4. 169All functions dealing with dynamic locks were added in OpenSSL 0.9.5b-dev. 170 171=head1 SEE ALSO 172 173L<crypto(3)|crypto(3)> 174 175=cut 176