Update OpenSSL version number and regenerate the manual pages.
[dragonfly.git] / secure / lib / libcrypto / man / threads.3
1 .\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
12 ..
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
16 ..
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
21 ..
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
25 ..
26 .\" Set up some character translations and predefined strings.  \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
29 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
30 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
31 .\" nothing in troff, for use with C<>.
32 .tr \(*W-
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
37 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
51 .\" Escape single quotes in literal strings from groff's Unicode transform.
52 .ie \n(.g .ds Aq \(aq
53 .el       .ds Aq '
54 .\"
55 .\" If the F register is turned on, we'll generate index entries on stderr for
56 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
57 .\" entries marked with X<> in POD.  Of course, you'll have to process the
58 .\" output yourself in some meaningful fashion.
59 .ie \nF \{\
60 .    de IX
61 .    tm Index:\\$1\t\\n%\t"\\$2"
62 ..
63 .    nr % 0
64 .    rr F
65 .\}
66 .el \{\
67 .    de IX
68 ..
69 .\}
70 .\"
71 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
72 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
73 .    \" fudge factors for nroff and troff
74 .if n \{\
75 .    ds #H 0
76 .    ds #V .8m
77 .    ds #F .3m
78 .    ds #[ \f1
79 .    ds #] \fP
80 .\}
81 .if t \{\
82 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
83 .    ds #V .6m
84 .    ds #F 0
85 .    ds #[ \&
86 .    ds #] \&
87 .\}
88 .    \" simple accents for nroff and troff
89 .if n \{\
90 .    ds ' \&
91 .    ds ` \&
92 .    ds ^ \&
93 .    ds , \&
94 .    ds ~ ~
95 .    ds /
96 .\}
97 .if t \{\
98 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
99 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
100 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
101 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
102 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
103 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
104 .\}
105 .    \" troff and (daisy-wheel) nroff accents
106 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
107 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
108 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
109 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
110 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
111 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
112 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
113 .ds ae a\h'-(\w'a'u*4/10)'e
114 .ds Ae A\h'-(\w'A'u*4/10)'E
115 .    \" corrections for vroff
116 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
117 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
118 .    \" for low resolution devices (crt and lpr)
119 .if \n(.H>23 .if \n(.V>19 \
120 \{\
121 .    ds : e
122 .    ds 8 ss
123 .    ds o a
124 .    ds d- d\h'-1'\(ga
125 .    ds D- D\h'-1'\(hy
126 .    ds th \o'bp'
127 .    ds Th \o'LP'
128 .    ds ae ae
129 .    ds Ae AE
130 .\}
131 .rm #[ #] #H #V #F C
132 .\" ========================================================================
133 .\"
134 .IX Title "threads 3"
135 .TH threads 3 "2009-11-06" "0.9.8l" "OpenSSL"
136 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
137 .\" way too many mistakes in technical documents.
138 .if n .ad l
139 .nh
140 .SH "NAME"
141 CRYPTO_set_locking_callback, CRYPTO_set_id_callback, CRYPTO_num_locks,
142 CRYPTO_set_dynlock_create_callback, CRYPTO_set_dynlock_lock_callback,
143 CRYPTO_set_dynlock_destroy_callback, CRYPTO_get_new_dynlockid,
144 CRYPTO_destroy_dynlockid, CRYPTO_lock \- OpenSSL thread support
145 .SH "SYNOPSIS"
146 .IX Header "SYNOPSIS"
147 .Vb 1
148 \& #include <openssl/crypto.h>
149 \&
150 \& void CRYPTO_set_locking_callback(void (*locking_function)(int mode,
151 \&        int n, const char *file, int line));
152 \&
153 \& void CRYPTO_set_id_callback(unsigned long (*id_function)(void));
154 \&
155 \& int CRYPTO_num_locks(void);
156 \&
157 \&
158 \& /* struct CRYPTO_dynlock_value needs to be defined by the user */
159 \& struct CRYPTO_dynlock_value;
160 \&
161 \& void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *
162 \&        (*dyn_create_function)(char *file, int line));
163 \& void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)
164 \&        (int mode, struct CRYPTO_dynlock_value *l,
165 \&        const char *file, int line));
166 \& void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)
167 \&        (struct CRYPTO_dynlock_value *l, const char *file, int line));
168 \&
169 \& int CRYPTO_get_new_dynlockid(void);
170 \&
171 \& void CRYPTO_destroy_dynlockid(int i);
172 \&
173 \& void CRYPTO_lock(int mode, int n, const char *file, int line);
174 \&
175 \& #define CRYPTO_w_lock(type)    \e
176 \&        CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,_\|_FILE_\|_,_\|_LINE_\|_)
177 \& #define CRYPTO_w_unlock(type)  \e
178 \&        CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,_\|_FILE_\|_,_\|_LINE_\|_)
179 \& #define CRYPTO_r_lock(type)    \e
180 \&        CRYPTO_lock(CRYPTO_LOCK|CRYPTO_READ,type,_\|_FILE_\|_,_\|_LINE_\|_)
181 \& #define CRYPTO_r_unlock(type)  \e
182 \&        CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_READ,type,_\|_FILE_\|_,_\|_LINE_\|_)
183 \& #define CRYPTO_add(addr,amount,type)   \e
184 \&        CRYPTO_add_lock(addr,amount,type,_\|_FILE_\|_,_\|_LINE_\|_)
185 .Ve
186 .SH "DESCRIPTION"
187 .IX Header "DESCRIPTION"
188 OpenSSL can safely be used in multi-threaded applications provided
189 that at least two callback functions are set.
190 .PP
191 locking_function(int mode, int n, const char *file, int line) is
192 needed to perform locking on shared data structures. 
193 (Note that OpenSSL uses a number of global data structures that
194 will be implicitly shared whenever multiple threads use OpenSSL.)
195 Multi-threaded applications will crash at random if it is not set.
196 .PP
197 \&\fIlocking_function()\fR must be able to handle up to \fICRYPTO_num_locks()\fR
198 different mutex locks. It sets the \fBn\fR\-th lock if \fBmode\fR &
199 \&\fB\s-1CRYPTO_LOCK\s0\fR, and releases it otherwise.
200 .PP
201 \&\fBfile\fR and \fBline\fR are the file number of the function setting the
202 lock. They can be useful for debugging.
203 .PP
204 id_function(void) is a function that returns a thread \s-1ID\s0, for example
205 \&\fIpthread_self()\fR if it returns an integer (see \s-1NOTES\s0 below).  It isn't
206 needed on Windows nor on platforms where \fIgetpid()\fR returns a different
207 \&\s-1ID\s0 for each thread (see \s-1NOTES\s0 below).
208 .PP
209 Additionally, OpenSSL supports dynamic locks, and sometimes, some parts
210 of OpenSSL need it for better performance.  To enable this, the following
211 is required:
212 .IP "\(bu" 4
213 Three additional callback function, dyn_create_function, dyn_lock_function
214 and dyn_destroy_function.
215 .IP "\(bu" 4
216 A structure defined with the data that each lock needs to handle.
217 .PP
218 struct CRYPTO_dynlock_value has to be defined to contain whatever structure
219 is needed to handle locks.
220 .PP
221 dyn_create_function(const char *file, int line) is needed to create a
222 lock.  Multi-threaded applications might crash at random if it is not set.
223 .PP
224 dyn_lock_function(int mode, CRYPTO_dynlock *l, const char *file, int line)
225 is needed to perform locking off dynamic lock numbered n. Multi-threaded
226 applications might crash at random if it is not set.
227 .PP
228 dyn_destroy_function(CRYPTO_dynlock *l, const char *file, int line) is
229 needed to destroy the lock l. Multi-threaded applications might crash at
230 random if it is not set.
231 .PP
232 \&\fICRYPTO_get_new_dynlockid()\fR is used to create locks.  It will call
233 dyn_create_function for the actual creation.
234 .PP
235 \&\fICRYPTO_destroy_dynlockid()\fR is used to destroy locks.  It will call
236 dyn_destroy_function for the actual destruction.
237 .PP
238 \&\fICRYPTO_lock()\fR is used to lock and unlock the locks.  mode is a bitfield
239 describing what should be done with the lock.  n is the number of the
240 lock as returned from \fICRYPTO_get_new_dynlockid()\fR.  mode can be combined
241 from the following values.  These values are pairwise exclusive, with
242 undefined behaviour if misused (for example, \s-1CRYPTO_READ\s0 and \s-1CRYPTO_WRITE\s0
243 should not be used together):
244 .PP
245 .Vb 4
246 \&        CRYPTO_LOCK     0x01
247 \&        CRYPTO_UNLOCK   0x02
248 \&        CRYPTO_READ     0x04
249 \&        CRYPTO_WRITE    0x08
250 .Ve
251 .SH "RETURN VALUES"
252 .IX Header "RETURN VALUES"
253 \&\fICRYPTO_num_locks()\fR returns the required number of locks.
254 .PP
255 \&\fICRYPTO_get_new_dynlockid()\fR returns the index to the newly created lock.
256 .PP
257 The other functions return no values.
258 .SH "NOTES"
259 .IX Header "NOTES"
260 You can find out if OpenSSL was configured with thread support:
261 .PP
262 .Vb 7
263 \& #define OPENSSL_THREAD_DEFINES
264 \& #include <openssl/opensslconf.h>
265 \& #if defined(OPENSSL_THREADS)
266 \&   // thread support enabled
267 \& #else
268 \&   // no thread support
269 \& #endif
270 .Ve
271 .PP
272 Also, dynamic locks are currently not used internally by OpenSSL, but
273 may do so in the future.
274 .PP
275 Defining id_function(void) has it's own issues.  Generally speaking,
276 \&\fIpthread_self()\fR should be used, even on platforms where \fIgetpid()\fR gives
277 different answers in each thread, since that may depend on the machine
278 the program is run on, not the machine where the program is being
279 compiled.  For instance, Red Hat 8 Linux and earlier used
280 LinuxThreads, whose \fIgetpid()\fR returns a different value for each
281 thread.  Red Hat 9 Linux and later use \s-1NPTL\s0, which is
282 Posix-conformant, and has a \fIgetpid()\fR that returns the same value for
283 all threads in a process.  A program compiled on Red Hat 8 and run on
284 Red Hat 9 will therefore see \fIgetpid()\fR returning the same value for
285 all threads.
286 .PP
287 There is still the issue of platforms where \fIpthread_self()\fR returns
288 something other than an integer.  This is a bit unusual, and this
289 manual has no cookbook solution for that case.
290 .SH "EXAMPLES"
291 .IX Header "EXAMPLES"
292 \&\fBcrypto/threads/mttest.c\fR shows examples of the callback functions on
293 Solaris, Irix and Win32.
294 .SH "HISTORY"
295 .IX Header "HISTORY"
296 \&\fICRYPTO_set_locking_callback()\fR and \fICRYPTO_set_id_callback()\fR are
297 available in all versions of SSLeay and OpenSSL.
298 \&\fICRYPTO_num_locks()\fR was added in OpenSSL 0.9.4.
299 All functions dealing with dynamic locks were added in OpenSSL 0.9.5b\-dev.
300 .SH "SEE ALSO"
301 .IX Header "SEE ALSO"
302 \&\fIcrypto\fR\|(3)