230cbe890bc3869bdd57886881ab63ee14abe094
[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.  OpenSSL assumes that it is in the same
80 thread iff both the numerical and the pointer thread ID agree, so it
81 suffices to define one of these two callback functions appropriately.
82
83 Additionally, OpenSSL supports dynamic locks, and sometimes, some parts
84 of OpenSSL need it for better performance.  To enable this, the following
85 is required:
86
87 =over 4
88
89 =item *
90 Three additional callback function, dyn_create_function, dyn_lock_function
91 and dyn_destroy_function.
92
93 =item *
94 A structure defined with the data that each lock needs to handle.
95
96 =back
97
98 struct CRYPTO_dynlock_value has to be defined to contain whatever structure
99 is needed to handle locks.
100
101 dyn_create_function(const char *file, int line) is needed to create a
102 lock.  Multi-threaded applications might crash at random if it is not set.
103
104 dyn_lock_function(int mode, CRYPTO_dynlock *l, const char *file, int line)
105 is needed to perform locking off dynamic lock numbered n. Multi-threaded
106 applications might crash at random if it is not set.
107
108 dyn_destroy_function(CRYPTO_dynlock *l, const char *file, int line) is
109 needed to destroy the lock l. Multi-threaded applications might crash at
110 random if it is not set.
111
112 CRYPTO_get_new_dynlockid() is used to create locks.  It will call
113 dyn_create_function for the actual creation.
114
115 CRYPTO_destroy_dynlockid() is used to destroy locks.  It will call
116 dyn_destroy_function for the actual destruction.
117
118 CRYPTO_lock() is used to lock and unlock the locks.  mode is a bitfield
119 describing what should be done with the lock.  n is the number of the
120 lock as returned from CRYPTO_get_new_dynlockid().  mode can be combined
121 from the following values.  These values are pairwise exclusive, with
122 undefined behaviour if misused (for example, CRYPTO_READ and CRYPTO_WRITE
123 should not be used together):
124
125         CRYPTO_LOCK     0x01
126         CRYPTO_UNLOCK   0x02
127         CRYPTO_READ     0x04
128         CRYPTO_WRITE    0x08
129
130 =head1 RETURN VALUES
131
132 CRYPTO_num_locks() returns the required number of locks.
133
134 CRYPTO_get_new_dynlockid() returns the index to the newly created lock.
135
136 The other functions return no values.
137
138 =head1 NOTES
139
140 You can find out if OpenSSL was configured with thread support:
141
142  #define OPENSSL_THREAD_DEFINES
143  #include <openssl/opensslconf.h>
144  #if defined(OPENSSL_THREADS)
145    // thread support enabled
146  #else
147    // no thread support
148  #endif
149
150 Also, dynamic locks are currently not used internally by OpenSSL, but
151 may do so in the future.
152
153 Defining id_function(void) has it's own issues.  Generally speaking,
154 pthread_self() should be used, even on platforms where getpid() gives
155 different answers in each thread, since that may depend on the machine
156 the program is run on, not the machine where the program is being
157 compiled.  For instance, Red Hat 8 Linux and earlier used
158 LinuxThreads, whose getpid() returns a different value for each
159 thread.  Red Hat 9 Linux and later use NPTL, which is
160 Posix-conformant, and has a getpid() that returns the same value for
161 all threads in a process.  A program compiled on Red Hat 8 and run on
162 Red Hat 9 will therefore see getpid() returning the same value for
163 all threads.
164
165 There is still the issue of platforms where pthread_self() returns
166 something other than an integer.  It is for cases like this that
167 CRYPTO_set_idptr_callback() comes in handy.  (E.g., call malloc(1)
168 once in each thread, and have idptr_function() return a pointer to
169 this object.)
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