Add support for dynamically created and destroyed mutexes. This will
[openssl.git] / doc / crypto / threads.pod
1 =pod
2
3 =head1 NAME
4
5 CRYPTO_set_locking_callback, CRYPTO_set_id_callback - OpenSSL thread support
6
7 =head1 SYNOPSIS
8
9  #include <openssl/crypto.h>
10
11  void CRYPTO_set_locking_callback(void (*locking_function)(int mode,
12         int n, const char *file, int line));
13
14  void CRYPTO_set_id_callback(unsigned long (*id_function)(void));
15
16  int CRYPTO_num_locks(void);
17
18
19  /* struct CRYPTO_dynlock_value needs to be defined by the user */
20  typedef struct CRYPTO_dynlock_value CRYPTO_dynlock;
21
22  void CRYPTO_set_dynlock_create_callback(CRYPTO_dynlock *(*dyn_create_function)
23         (char *file, int line));
24  void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)
25         (int mode, CRYPTO_dynlock *l, const char *file, int line));
26  void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)
27         (CRYPTO_dynlock *l, const char *file, int line));
28
29  int CRYPTO_get_new_dynlockid(void);
30
31  void CRYPTO_destroy_dynlockid(int i);
32
33  void CRYPTO_lock(int mode, int n, const char *file, int line);
34
35 =head1 DESCRIPTION
36
37 OpenSSL can safely be used in multi-threaded applications provided
38 that at least two callback functions are set.
39
40 locking_function(int mode, int n, const char *file, int line) is
41 needed to perform locking on shared data stuctures. Multi-threaded
42 applications will crash at random if it is not set.
43
44 locking_function() must be able to handle up to CRYPTO_num_locks()
45 different mutex locks. It sets the B<n>-th lock if B<mode> &
46 B<CRYPTO_LOCK>, and releases it otherwise.
47
48 B<file> and B<line> are the file number of the function setting the
49 lock. They can be useful for debugging.
50
51 id_function(void) is a function that returns a thread ID. It is not
52 needed on Windows nor on platforms where getpid() returns a different
53 ID for each thread (most notably Linux).
54
55 Additionally, OpenSSL supports dynamic locks, and sometimes, some parts
56 of OpenSSL need it for better performance.  To enable this, the following
57 is required:
58
59 =item *
60 Three additional callback function, dyn_create_function, dyn_lock_function
61 and dyn_destroy_function.
62
63 =item *
64 A structure defined with the data that each lock needs to handle.
65
66 struct CRYPTO_dynlock_value has to be defined to contain whatever structure
67 is needed to handle locks.
68
69 dyn_create_function(const char *file, int line) is needed to create a
70 lock.  Multi-threaded applications might crash at random if it is not set.
71
72 dyn_lock_function(int mode, CRYPTO_dynlock *l, const char *file, int line)
73 is needed to perform locking off dynamic lock nunmbered n. Multi-threaded
74 applications might crash at random if it is not set.
75
76 dyn_destroy_function(CRYPTO_dynlock *l, const char *file, int line) is
77 needed to destroy the lock l. Multi-threaded applications might crash at
78 random if it is not set.
79
80 CRYPTO_get_new_dynlockid() is used to create locks.  It will call
81 dyn_create_function for the actual creation.
82
83 CRYPTO_destroy_dynlockid() is used to destroy locks.  It will call
84 dyn_destroy_function for the actual destruction.
85
86 CRYPTO_lock() is used to lock and unlock the locks.  mode is a bitfield
87 describing what should be done with the lock.  n is the number of the
88 lock as returned from CRYPTO_get_new_dynlockid().  mode can be combined
89 from the following values.  These values are pairwise exclusive, with
90 undefined behavior if misused (for example, CRYPTO_READ and CRYPTO_WRITE
91 should not be used together):
92
93         CRYPTO_LOCK     0x01
94         CRYPTO_UNLOCK   0x02
95         CRYPTO_READ     0x04
96         CRYPTO_WRITE    0x08
97
98 =head1 RETURN VALUES
99
100 CRYPTO_num_locks() returns the required number of locks.
101
102 CRYPTO_get_new_dynlockid() returns the index to the newly created lock.
103
104 The other functions return no values.
105
106 =head1 NOTE
107
108 You can find out if OpenSSL was configured with thread support:
109
110  #define OPENSSL_THREAD_DEFINES
111  #include <openssl/opensslconf.h>
112  #if defined(THREADS)
113    // thread support enabled
114  #else
115    // no thread support
116  #endif
117
118 =head1 EXAMPLES
119
120 B<crypto/threads/mttest.c> shows examples of the callback functions on
121 Solaris, Irix and Win32.
122
123 =head1 HISTORY
124
125 CRYPTO_set_locking_callback() and CRYPTO_set_id_callback() are
126 available in all versions of SSLeay and OpenSSL.
127 CRYPTO_num_locks() was added in OpenSSL 0.9.4.
128 All functions dealing with dynamic locks were added in OpenSSL 0.9.5b-dev.
129
130 =head1 SEE ALSO
131
132 L<crypto(3)|crypto(3)>
133
134 =cut