threads.3 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. .\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35)
  2. .\"
  3. .\" Standard preamble:
  4. .\" ========================================================================
  5. .de Sp \" Vertical space (when we can't use .PP)
  6. .if t .sp .5v
  7. .if n .sp
  8. ..
  9. .de Vb \" Begin verbatim text
  10. .ft CW
  11. .nf
  12. .ne \\$1
  13. ..
  14. .de Ve \" End verbatim text
  15. .ft R
  16. .fi
  17. ..
  18. .\" Set up some character translations and predefined strings. \*(-- will
  19. .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
  20. .\" double quote, and \*(R" will give a right double quote. \*(C+ will
  21. .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
  22. .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
  23. .\" nothing in troff, for use with C<>.
  24. .tr \(*W-
  25. .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
  26. .ie n \{\
  27. . ds -- \(*W-
  28. . ds PI pi
  29. . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
  30. . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
  31. . ds L" ""
  32. . ds R" ""
  33. . ds C` ""
  34. . ds C' ""
  35. 'br\}
  36. .el\{\
  37. . ds -- \|\(em\|
  38. . ds PI \(*p
  39. . ds L" ``
  40. . ds R" ''
  41. . ds C`
  42. . ds C'
  43. 'br\}
  44. .\"
  45. .\" Escape single quotes in literal strings from groff's Unicode transform.
  46. .ie \n(.g .ds Aq \(aq
  47. .el .ds Aq '
  48. .\"
  49. .\" If the F register is >0, we'll generate index entries on stderr for
  50. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  51. .\" entries marked with X<> in POD. Of course, you'll have to process the
  52. .\" output yourself in some meaningful fashion.
  53. .\"
  54. .\" Avoid warning from groff about undefined register 'F'.
  55. .de IX
  56. ..
  57. .if !\nF .nr F 0
  58. .if \nF>0 \{\
  59. . de IX
  60. . tm Index:\\$1\t\\n%\t"\\$2"
  61. ..
  62. . if !\nF==2 \{\
  63. . nr % 0
  64. . nr F 2
  65. . \}
  66. .\}
  67. .\"
  68. .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
  69. .\" Fear. Run. Save yourself. No user-serviceable parts.
  70. . \" fudge factors for nroff and troff
  71. .if n \{\
  72. . ds #H 0
  73. . ds #V .8m
  74. . ds #F .3m
  75. . ds #[ \f1
  76. . ds #] \fP
  77. .\}
  78. .if t \{\
  79. . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
  80. . ds #V .6m
  81. . ds #F 0
  82. . ds #[ \&
  83. . ds #] \&
  84. .\}
  85. . \" simple accents for nroff and troff
  86. .if n \{\
  87. . ds ' \&
  88. . ds ` \&
  89. . ds ^ \&
  90. . ds , \&
  91. . ds ~ ~
  92. . ds /
  93. .\}
  94. .if t \{\
  95. . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
  96. . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
  97. . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
  98. . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
  99. . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
  100. . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
  101. .\}
  102. . \" troff and (daisy-wheel) nroff accents
  103. .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
  104. .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
  105. .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
  106. .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
  107. .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
  108. .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
  109. .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
  110. .ds ae a\h'-(\w'a'u*4/10)'e
  111. .ds Ae A\h'-(\w'A'u*4/10)'E
  112. . \" corrections for vroff
  113. .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
  114. .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
  115. . \" for low resolution devices (crt and lpr)
  116. .if \n(.H>23 .if \n(.V>19 \
  117. \{\
  118. . ds : e
  119. . ds 8 ss
  120. . ds o a
  121. . ds d- d\h'-1'\(ga
  122. . ds D- D\h'-1'\(hy
  123. . ds th \o'bp'
  124. . ds Th \o'LP'
  125. . ds ae ae
  126. . ds Ae AE
  127. .\}
  128. .rm #[ #] #H #V #F C
  129. .\" ========================================================================
  130. .\"
  131. .IX Title "threads 3"
  132. .TH threads 3 "2019-09-12" "1.0.2g" "OpenSSL"
  133. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  134. .\" way too many mistakes in technical documents.
  135. .if n .ad l
  136. .nh
  137. .SH "NAME"
  138. CRYPTO_THREADID_set_callback, CRYPTO_THREADID_get_callback,
  139. CRYPTO_THREADID_current, CRYPTO_THREADID_cmp, CRYPTO_THREADID_cpy,
  140. CRYPTO_THREADID_hash, CRYPTO_set_locking_callback, CRYPTO_num_locks,
  141. CRYPTO_set_dynlock_create_callback, CRYPTO_set_dynlock_lock_callback,
  142. CRYPTO_set_dynlock_destroy_callback, CRYPTO_get_new_dynlockid,
  143. CRYPTO_destroy_dynlockid, CRYPTO_lock \- OpenSSL thread support
  144. .SH "SYNOPSIS"
  145. .IX Header "SYNOPSIS"
  146. .Vb 1
  147. \& #include <openssl/crypto.h>
  148. \&
  149. \& /* Don\*(Aqt use this structure directly. */
  150. \& typedef struct crypto_threadid_st
  151. \& {
  152. \& void *ptr;
  153. \& unsigned long val;
  154. \& } CRYPTO_THREADID;
  155. \& /* Only use CRYPTO_THREADID_set_[numeric|pointer]() within callbacks */
  156. \& void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val);
  157. \& void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr);
  158. \& int CRYPTO_THREADID_set_callback(void (*threadid_func)(CRYPTO_THREADID *));
  159. \& void (*CRYPTO_THREADID_get_callback(void))(CRYPTO_THREADID *);
  160. \& void CRYPTO_THREADID_current(CRYPTO_THREADID *id);
  161. \& int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a,
  162. \& const CRYPTO_THREADID *b);
  163. \& void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest,
  164. \& const CRYPTO_THREADID *src);
  165. \& unsigned long CRYPTO_THREADID_hash(const CRYPTO_THREADID *id);
  166. \&
  167. \& int CRYPTO_num_locks(void);
  168. \&
  169. \& /* struct CRYPTO_dynlock_value needs to be defined by the user */
  170. \& struct CRYPTO_dynlock_value;
  171. \&
  172. \& void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *
  173. \& (*dyn_create_function)(char *file, int line));
  174. \& void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)
  175. \& (int mode, struct CRYPTO_dynlock_value *l,
  176. \& const char *file, int line));
  177. \& void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)
  178. \& (struct CRYPTO_dynlock_value *l, const char *file, int line));
  179. \&
  180. \& int CRYPTO_get_new_dynlockid(void);
  181. \&
  182. \& void CRYPTO_destroy_dynlockid(int i);
  183. \&
  184. \& void CRYPTO_lock(int mode, int n, const char *file, int line);
  185. \&
  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, locking_function and
  201. threadid_func.
  202. .PP
  203. locking_function(int mode, int n, const char *file, int line) is
  204. needed to perform locking on shared data structures.
  205. (Note that OpenSSL uses a number of global data structures that
  206. will be implicitly shared whenever multiple threads use OpenSSL.)
  207. Multi-threaded applications will crash at random if it is not set.
  208. .PP
  209. \&\fIlocking_function()\fR must be able to handle up to \fICRYPTO_num_locks()\fR
  210. different mutex locks. It sets the \fBn\fR\-th lock if \fBmode\fR &
  211. \&\fB\s-1CRYPTO_LOCK\s0\fR, and releases it otherwise.
  212. .PP
  213. \&\fBfile\fR and \fBline\fR are the file number of the function setting the
  214. lock. They can be useful for debugging.
  215. .PP
  216. threadid_func(\s-1CRYPTO_THREADID\s0 *id) is needed to record the currently-executing
  217. thread's identifier into \fBid\fR. The implementation of this callback should not
  218. fill in \fBid\fR directly, but should use \fICRYPTO_THREADID_set_numeric()\fR if thread
  219. IDs are numeric, or \fICRYPTO_THREADID_set_pointer()\fR if they are pointer-based.
  220. If the application does not register such a callback using
  221. \&\fICRYPTO_THREADID_set_callback()\fR, then a default implementation is used \- on
  222. Windows and BeOS this uses the system's default thread identifying APIs, and on
  223. all other platforms it uses the address of \fBerrno\fR. The latter is satisfactory
  224. for thread-safety if and only if the platform has a thread-local error number
  225. facility.
  226. .PP
  227. Once \fIthreadid_func()\fR is registered, or if the built-in default implementation is
  228. to be used;
  229. .IP "\(bu" 4
  230. \&\fICRYPTO_THREADID_current()\fR records the currently-executing thread \s-1ID\s0 into the
  231. given \fBid\fR object.
  232. .IP "\(bu" 4
  233. \&\fICRYPTO_THREADID_cmp()\fR compares two thread IDs (returning zero for equality, ie.
  234. the same semantics as \fImemcmp()\fR).
  235. .IP "\(bu" 4
  236. \&\fICRYPTO_THREADID_cpy()\fR duplicates a thread \s-1ID\s0 value,
  237. .IP "\(bu" 4
  238. \&\fICRYPTO_THREADID_hash()\fR returns a numeric value usable as a hash-table key. This
  239. is usually the exact numeric or pointer-based thread \s-1ID\s0 used internally, however
  240. this also handles the unusual case where pointers are larger than 'long'
  241. variables and the platform's thread IDs are pointer-based \- in this case, mixing
  242. is done to attempt to produce a unique numeric value even though it is not as
  243. wide as the platform's true thread IDs.
  244. .PP
  245. Additionally, OpenSSL supports dynamic locks, and sometimes, some parts
  246. of OpenSSL need it for better performance. To enable this, the following
  247. is required:
  248. .IP "\(bu" 4
  249. Three additional callback function, dyn_create_function, dyn_lock_function
  250. and dyn_destroy_function.
  251. .IP "\(bu" 4
  252. A structure defined with the data that each lock needs to handle.
  253. .PP
  254. struct CRYPTO_dynlock_value has to be defined to contain whatever structure
  255. is needed to handle locks.
  256. .PP
  257. dyn_create_function(const char *file, int line) is needed to create a
  258. lock. Multi-threaded applications might crash at random if it is not set.
  259. .PP
  260. dyn_lock_function(int mode, CRYPTO_dynlock *l, const char *file, int line)
  261. is needed to perform locking off dynamic lock numbered n. Multi-threaded
  262. applications might crash at random if it is not set.
  263. .PP
  264. dyn_destroy_function(CRYPTO_dynlock *l, const char *file, int line) is
  265. needed to destroy the lock l. Multi-threaded applications might crash at
  266. random if it is not set.
  267. .PP
  268. \&\fICRYPTO_get_new_dynlockid()\fR is used to create locks. It will call
  269. dyn_create_function for the actual creation.
  270. .PP
  271. \&\fICRYPTO_destroy_dynlockid()\fR is used to destroy locks. It will call
  272. dyn_destroy_function for the actual destruction.
  273. .PP
  274. \&\fICRYPTO_lock()\fR is used to lock and unlock the locks. mode is a bitfield
  275. describing what should be done with the lock. n is the number of the
  276. lock as returned from \fICRYPTO_get_new_dynlockid()\fR. mode can be combined
  277. from the following values. These values are pairwise exclusive, with
  278. undefined behaviour if misused (for example, \s-1CRYPTO_READ\s0 and \s-1CRYPTO_WRITE\s0
  279. should not be used together):
  280. .PP
  281. .Vb 4
  282. \& CRYPTO_LOCK 0x01
  283. \& CRYPTO_UNLOCK 0x02
  284. \& CRYPTO_READ 0x04
  285. \& CRYPTO_WRITE 0x08
  286. .Ve
  287. .SH "RETURN VALUES"
  288. .IX Header "RETURN VALUES"
  289. \&\fICRYPTO_num_locks()\fR returns the required number of locks.
  290. .PP
  291. \&\fICRYPTO_get_new_dynlockid()\fR returns the index to the newly created lock.
  292. .PP
  293. The other functions return no values.
  294. .SH "NOTES"
  295. .IX Header "NOTES"
  296. You can find out if OpenSSL was configured with thread support:
  297. .PP
  298. .Vb 7
  299. \& #define OPENSSL_THREAD_DEFINES
  300. \& #include <openssl/opensslconf.h>
  301. \& #if defined(OPENSSL_THREADS)
  302. \& // thread support enabled
  303. \& #else
  304. \& // no thread support
  305. \& #endif
  306. .Ve
  307. .PP
  308. Also, dynamic locks are currently not used internally by OpenSSL, but
  309. may do so in the future.
  310. .SH "EXAMPLES"
  311. .IX Header "EXAMPLES"
  312. \&\fBcrypto/threads/mttest.c\fR shows examples of the callback functions on
  313. Solaris, Irix and Win32.
  314. .SH "HISTORY"
  315. .IX Header "HISTORY"
  316. \&\fICRYPTO_set_locking_callback()\fR is
  317. available in all versions of SSLeay and OpenSSL.
  318. \&\fICRYPTO_num_locks()\fR was added in OpenSSL 0.9.4.
  319. All functions dealing with dynamic locks were added in OpenSSL 0.9.5b\-dev.
  320. \&\fB\s-1CRYPTO_THREADID\s0\fR and associated functions were introduced in OpenSSL 1.0.0
  321. to replace (actually, deprecate) the previous \fICRYPTO_set_id_callback()\fR,
  322. \&\fICRYPTO_get_id_callback()\fR, and \fICRYPTO_thread_id()\fR functions which assumed
  323. thread IDs to always be represented by 'unsigned long'.
  324. .SH "SEE ALSO"
  325. .IX Header "SEE ALSO"
  326. \&\fIcrypto\fR\|(3)