There was a need to support thread ID types that couldn't be reliably cast
[openssl.git] / doc / crypto / threads.pod
1 =pod
2
3 =head1 NAME
4
5 CRYPTO_set_locking_callback, CRYPTO_set_id_callback,
6 CRYPTO_set_idptr_callback, CRYPTO_num_locks,
7 CRYPTO_set_dynlock_create_callback, CRYPTO_set_dynlock_lock_callback,
8 CRYPTO_set_dynlock_destroy_callback, CRYPTO_get_new_dynlockid,
9 CRYPTO_destroy_dynlockid, CRYPTO_lock - OpenSSL thread support
10
11 =head1 SYNOPSIS
12
13  #include <openssl/crypto.h>
14
15  void CRYPTO_set_locking_callback(void (*locking_function)(int mode,
16         int n, const char *file, int line));
17
18  void CRYPTO_set_id_callback(unsigned long (*id_function)(void));
19
20  void CRYPTO_set_idptr_callback(void *(*idptr_function)(void));
21
22  int CRYPTO_num_locks(void);
23
24
25  /* struct CRYPTO_dynlock_value needs to be defined by the user */
26  struct CRYPTO_dynlock_value;
27
28  void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *
29         (*dyn_create_function)(char *file, int line));
30  void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)
31         (int mode, struct CRYPTO_dynlock_value *l,
32         const char *file, int line));
33  void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)
34         (struct CRYPTO_dynlock_value *l, const char *file, int line));
35
36  int CRYPTO_get_new_dynlockid(void);
37
38  void CRYPTO_destroy_dynlockid(int i);
39
40  void CRYPTO_lock(int mode, int n, const char *file, int line);
41
42  #define CRYPTO_w_lock(type)    \
43         CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
44  #define CRYPTO_w_unlock(type)  \
45         CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
46  #define CRYPTO_r_lock(type)    \
47         CRYPTO_lock(CRYPTO_LOCK|CRYPTO_READ,type,__FILE__,__LINE__)
48  #define CRYPTO_r_unlock(type)  \
49         CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_READ,type,__FILE__,__LINE__)
50  #define CRYPTO_add(addr,amount,type)   \
51         CRYPTO_add_lock(addr,amount,type,__FILE__,__LINE__)
52
53 =head1 DESCRIPTION
54
55 OpenSSL can safely be used in multi-threaded applications provided
56 that at least two callback functions are set.
57
58 locking_function(int mode, int n, const char *file, int line) is
59 needed to perform locking on shared data structures. 
60 (Note that OpenSSL uses a number of global data structures that
61 will be implicitly shared whenever multiple threads use OpenSSL.)
62 Multi-threaded applications will crash at random if it is not set.
63
64 locking_function() must be able to handle up to CRYPTO_num_locks()
65 different mutex locks. It sets the B<n>-th lock if B<mode> &
66 B<CRYPTO_LOCK>, and releases it otherwise.
67
68 B<file> and B<line> are the file number of the function setting the
69 lock. They can be useful for debugging.
70
71 id_function(void) is a function that returns a numerical thread ID,
72 for example pthread_self() if it returns an integer (see NOTES below).
73 By OpenSSL's defaults, this is not needed on Windows nor on platforms
74 where getpid() returns a different ID for each thread (see NOTES
75 below).
76
77 idptr_function(void) is a function that similarly returns a thread ID,
78 but of type void *.  This is not needed on platforms where &errno is
79 different for each thread.
80
81 Additionally, OpenSSL supports dynamic locks, and sometimes, some parts
82 of OpenSSL need it for better performance.  To enable this, the following
83 is required:
84
85 =over 4
86
87 =item *
88 Three additional callback function, dyn_create_function, dyn_lock_function
89 and dyn_destroy_function.
90
91 =item *
92 A structure defined with the data that each lock needs to handle.
93
94 =back
95
96 struct CRYPTO_dynlock_value has to be defined to contain whatever structure
97 is needed to handle locks.
98
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.
101
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.
105
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.
109
110 CRYPTO_get_new_dynlockid() is used to create locks.  It will call
111 dyn_create_function for the actual creation.
112
113 CRYPTO_destroy_dynlockid() is used to destroy locks.  It will call
114 dyn_destroy_function for the actual destruction.
115
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):
122
123         CRYPTO_LOCK     0x01
124         CRYPTO_UNLOCK   0x02
125         CRYPTO_READ     0x04
126         CRYPTO_WRITE    0x08
127
128 =head1 RETURN VALUES
129
130 CRYPTO_num_locks() returns the required number of locks.
131
132 CRYPTO_get_new_dynlockid() returns the index to the newly created lock.
133
134 The other functions return no values.
135
136 =head1 NOTES
137
138 You can find out if OpenSSL was configured with thread support:
139
140  #define OPENSSL_THREAD_DEFINES
141  #include <openssl/opensslconf.h>
142  #if defined(OPENSSL_THREADS)
143    // thread support enabled
144  #else
145    // no thread support
146  #endif
147
148 Also, dynamic locks are currently not used internally by OpenSSL, but
149 may do so in the future.
150
151 Defining id_function(void) has it's own issues.  Generally speaking,
152 pthread_self() should be used, even on platforms where getpid() gives
153 different answers in each thread, since that may depend on the machine
154 the program is run on, not the machine where the program is being
155 compiled.  For instance, Red Hat 8 Linux and earlier used
156 LinuxThreads, whose getpid() returns a different value for each
157 thread.  Red Hat 9 Linux and later use NPTL, which is
158 Posix-conformant, and has a getpid() that returns the same value for
159 all threads in a process.  A program compiled on Red Hat 8 and run on
160 Red Hat 9 will therefore see getpid() returning the same value for
161 all threads.
162
163 There is still the issue of platforms where pthread_self() returns
164 something other than an integer.  It is for cases like this that
165 CRYPTO_set_idptr_callback() comes in handy.  (E.g., call malloc(1)
166 once in each thread, and have idptr_function() return a pointer to
167 this object.) Note that if neither id_function() or idptr_function()
168 are provided, OpenSSL will use (&errno) as a fallback (as this
169 usually returns a unique address for each thread).
170
171 =head1 EXAMPLES
172
173 B<crypto/threads/mttest.c> shows examples of the callback functions on
174 Solaris, Irix and Win32.
175
176 =head1 HISTORY
177
178 CRYPTO_set_locking_callback() and CRYPTO_set_id_callback() are
179 available in all versions of SSLeay and OpenSSL.
180 CRYPTO_num_locks() was added in OpenSSL 0.9.4.
181 All functions dealing with dynamic locks were added in OpenSSL 0.9.5b-dev.
182
183 CRYPTO_set_idptr_callback() was added in OpenSSL 0.9.9.
184
185 =head1 SEE ALSO
186
187 L<crypto(3)|crypto(3)>
188
189 =cut