NAME
CRYPTOsetlockingcallback, CRYPTOsetidcallback, CRYPTOnumlocks, CRYPTOsetdynlockcreatecallback, CRYPTOsetdynlocklockcallback, CRYPTOsetdynlockdestroycallback, CRYPTOgetnewdynlockid,CRYPTOdestroydynlockid, CRYPTOlock - OpenSSL thread support
SYNOPSIS
#include
void CRYPTOsetlockingcallback(void (*lockingfunction)(int mode, int n, const char *file, int line)); void CRYPTOsetidcallback(unsigned long (*idfunction)(void)); int CRYPTOnumlocks(void); /* struct CRYPTOdynlockvalue needs to be defined by the user */ struct CRYPTOdynlockvalue; void CRYPTOsetdynlockcreatecallback(struct CRYPTOdynlockvalue * (*dyncreatefunction)(char *file, int line)); void CRYPTOsetdynlocklockcallback(void (*dynlockfunction) (int mode, struct CRYPTOdynlockvalue *l, const char *file, int line)); void CRYPTOsetdynlockdestroycallback(void (*dyndestroyfunction) (struct CRYPTOdynlockvalue *l, const char *file, int line)); int CRYPTOgetnewdynlockid(void); void CRYPTOdestroydynlockid(int i); void CRYPTOlock(int mode, int n, const char *file, int line);#define CRYPTOwlock(type) \
CRYPTOlock(CRYPTOLOCK|CRYPTOWRITE,type,FILE,LINE)#define CRYPTOwunlock(type) \
CRYPTOlock(CRYPTOUNLOCK|CRYPTOWRITE,type,FILE,LINE)#define CRYPTOrlock(type) \
CRYPTOlock(CRYPTOLOCK|CRYPTOREAD,type,FILE,LINE)#define CRYPTOrunlock(type) \
CRYPTOlock(CRYPTOUNLOCK|CRYPTOREAD,type,FILE,LINE)#define CRYPTOadd(addr,amount,type) \
CRYPTOaddlock(addr,amount,type,FILE,LINE)DESCRIPTION
OpenSSL can safely be used in multi-threaded applications provided that
at least two callback functions are set. lockingfunction(int mode, int n, const char *file, int line) is needed to perform locking on shared data structures. (Note that OpenSSL uses a number of global data structures that will be implicitly sharedwhenever multiple threads use OpenSSL.) Multi-threaded applications
will crash at random if it is not set. lockingfunction() must be able to handle up to CRYPTOnumlocks()different mutex locks. It sets the nn-th lock if mmooddee & CCRRYYPPTTOOLLOOCCKK, and
releases it otherwise. ffiillee and lliinnee are the file number of the function setting the lock. They can be useful for debugging. idfunction(void) is a function that returns a thread ID, for examplepthreadself() if it returns an integer (see NOTES below). It isn't
needed on Windows nor on platforms where getpid() returns a differentID for each thread (see NOTES below).
Additionally, OpenSSL supports dynamic locks, and sometimes, some parts of OpenSSL need it for better performance. To enable this, the following is required: +o Three additional callback function, dyncreatefunction, dynlockfunction and dyndestroyfunction. +o A structure defined with the data that each lock needs to handle. struct CRYPTOdynlockvalue has to be defined to contain whatever structure is needed to handle locks. dyncreatefunction(const char *file, int line) is needed to create alock. Multi-threaded applications might crash at random if it is not
set. dynlockfunction(int mode, CRYPTOdynlock *l, const char *file, intline) is needed to perform locking off dynamic lock numbered n. Multi-
threaded applications might crash at random if it is not set. dyndestroyfunction(CRYPTOdynlock *l, const char *file, int line) isneeded to destroy the lock l. Multi-threaded applications might crash
at random if it is not set. CRYPTOgetnewdynlockid() is used to create locks. It will call dyncreatefunction for the actual creation. CRYPTOdestroydynlockid() is used to destroy locks. It will call dyndestroyfunction for the actual destruction. CRYPTOlock() is used to lock and unlock the locks. mode is a bitfield describing what should be done with the lock. n is the number of the lock as returned from CRYPTOgetnewdynlockid(). mode can be combined from the following values. These values are pairwise exclusive, with undefined behaviour if misused (for example, CRYPTOREAD and CRYPTOWRITE should not be used together): CRYPTOLOCK 0x01 CRYPTOUNLOCK 0x02 CRYPTOREAD 0x04 CRYPTOWRITE 0x08RETURN VALUES
CRYPTOnumlocks() returns the required number of locks. CRYPTOgetnewdynlockid() returns the index to the newly created lock. The other functions return no values. NNOOTTEESS You can find out if OpenSSL was configured with thread support:#define OPENSSLTHREADDEFINES
#include
#if defined(OPENSSLTHREADS)
// thread support enabled#else
// no thread support#endif
Also, dynamic locks are currently not used internally by OpenSSL, but may do so in the future. Defining idfunction(void) has it's own issues. Generally speaking,pthreadself() should be used, even on platforms where getpid() gives
different answers in each thread, since that may depend on the machine the program is run on, not the machine where the program is being compiled. For instance, Red Hat 8 Linux and earlier used LinuxThreads, whose getpid() returns a different value for each thread. Red Hat 9Linux and later use NPTL, which is Posix-conformant, and has a getpid()
that returns the same value for all threads in a process. A program
compiled on Red Hat 8 and run on Red Hat 9 will therefore see getpid()returning the same value for all threads.
There is still the issue of platforms where pthreadself() returns
something other than an integer. This is a bit unusual, and this manual has no cookbook solution for that case. EEXXAAMMPPLLEESS ccrryyppttoo//tthhrreeaaddss//mmtttteesstt..cc shows examples of the callback functions on Solaris, Irix and Win32. HISTORY CRYPTOsetlockingcallback() and CRYPTOsetidcallback() are available in all versions of SSLeay and OpenSSL. CRYPTOnumlocks() was added in OpenSSL 0.9.4. All functions dealing with dynamic lockswere added in OpenSSL 0.9.5b-dev.
SEE ALSO
crypto(3)0.9.7l 2005-06-17 threads(3)