threads(3) OpenSL threads(3)
NAME
CRYPTOsetlockingcallback, CRYPTOsetidcallback, CRYPTOnumlocks,
CRYPTOsetdynlockcreatecallback, CRYPTOsetdynlocklockcallback,
CRYPTOsetdynlockdestroycallback, CRYPTOgetnewdynlockid,
CRYPTOdestroydynlockid, CRYPTOlock - OpenSL 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(CRYPTOLOCKCRYPTOWRITE,type,FILE,LINE)
#define CRYPTOwunlock(type) \
CRYPTOlock(CRYPTOUNLOCKCRYPTOWRITE,type,FILE,LINE)
#define CRYPTOrlock(type) \
CRYPTOlock(CRYPTOLOCKCRYPTOREAD,type,FILE,LINE)
#define CRYPTOrunlock(type) \
CRYPTOlock(CRYPTOUNLOCKCRYPTOREAD,type,FILE,LINE)
#define CRYPTOadd(addr,amount,type) \
CRYPTOaddlock(addr,amount,type,FILE,LINE)
DESCRIPTION
OpenSL 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 OpenSL uses
a number of global data structures that will be implicitly shared when-
ever multiple threads use OpenSL.) Multi-threaded applications will
crash at random if it is not set.
lockingfunction() must be able to handle up to CRYPTOnumlocks() dif-
ferent mutex locks. It sets the n-th lock if mode & CRYPTOLOCK, and
releases it otherwise.
file and line 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 example
pthreadself() if it returns an integer (see NOTES below). It isn't
needed on Windows nor on platforms where getpid() returns a different
ID for each thread (see NOTES below).
Additionally, OpenSL supports dynamic locks, and sometimes, some parts
of OpenSL need it for better performance. To enable this, the follow-
ing is required:
* Three additional callback function, dyncreatefunction,
dynlockfunction and dyndestroyfunction.
* 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 a
lock. Multi-threaded applications might crash at random if it is not
set.
dynlockfunction(int mode, CRYPTOdynlock *l, const char *file, int
line) 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) is
needed 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 0x08
RETURN VALUES
CRYPTOnumlocks() returns the required number of locks.
CRYPTOgetnewdynlockid() returns the index to the newly created lock.
The other functions return no values.
NOTES
You can find out if OpenSL was configured with thread support:
#define OPENSLTHREADEFINES
#include
#if defined(OPENSLTHREADS)
/ thread support enabled
#else
/ no thread support
#endif
Also, dynamic locks are currently not used internally by OpenSL, 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 com-
piled. For instance, Red Hat 8 Linux and earlier used LinuxThreads,
whose getpid() returns a different value for each thread. Red Hat 9
Linux 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 man-
ual has no cookbook solution for that case.
EXAMPLES
crypto/threads/mttest.c shows examples of the callback functions on
Solaris, Irix and Win32.
HISTORY
CRYPTOsetlockingcallback() and CRYPTOsetidcallback() are avail-
able in all versions of SLeay and OpenSL. CRYPTOnumlocks() was
added in OpenSL 0.9.4. All functions dealing with dynamic locks were
added in OpenSL 0.9.5b-dev.
SEE ALSO
crypto(3)
0.9.7l 2005-06-17 threads(3)
|