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