threads.pod 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. =pod
  2. =head1 NAME
  3. CRYPTO_THREADID_set_callback, CRYPTO_THREADID_get_callback,
  4. CRYPTO_THREADID_current, CRYPTO_THREADID_cmp, CRYPTO_THREADID_cpy,
  5. CRYPTO_THREADID_hash, CRYPTO_set_locking_callback, CRYPTO_num_locks,
  6. CRYPTO_set_dynlock_create_callback, CRYPTO_set_dynlock_lock_callback,
  7. CRYPTO_set_dynlock_destroy_callback, CRYPTO_get_new_dynlockid,
  8. CRYPTO_destroy_dynlockid, CRYPTO_lock - OpenSSL thread support
  9. =head1 SYNOPSIS
  10. #include <openssl/crypto.h>
  11. /* Don't use this structure directly. */
  12. typedef struct crypto_threadid_st
  13. {
  14. void *ptr;
  15. unsigned long val;
  16. } CRYPTO_THREADID;
  17. /* Only use CRYPTO_THREADID_set_[numeric|pointer]() within callbacks */
  18. void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val);
  19. void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr);
  20. int CRYPTO_THREADID_set_callback(void (*threadid_func)(CRYPTO_THREADID *));
  21. void (*CRYPTO_THREADID_get_callback(void))(CRYPTO_THREADID *);
  22. void CRYPTO_THREADID_current(CRYPTO_THREADID *id);
  23. int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a,
  24. const CRYPTO_THREADID *b);
  25. void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest,
  26. const CRYPTO_THREADID *src);
  27. unsigned long CRYPTO_THREADID_hash(const CRYPTO_THREADID *id);
  28. int CRYPTO_num_locks(void);
  29. /* struct CRYPTO_dynlock_value needs to be defined by the user */
  30. struct CRYPTO_dynlock_value;
  31. void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *
  32. (*dyn_create_function)(char *file, int line));
  33. void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)
  34. (int mode, struct CRYPTO_dynlock_value *l,
  35. const char *file, int line));
  36. void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)
  37. (struct CRYPTO_dynlock_value *l, const char *file, int line));
  38. int CRYPTO_get_new_dynlockid(void);
  39. void CRYPTO_destroy_dynlockid(int i);
  40. void CRYPTO_lock(int mode, int n, const char *file, int line);
  41. #define CRYPTO_w_lock(type) \
  42. CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
  43. #define CRYPTO_w_unlock(type) \
  44. CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
  45. #define CRYPTO_r_lock(type) \
  46. CRYPTO_lock(CRYPTO_LOCK|CRYPTO_READ,type,__FILE__,__LINE__)
  47. #define CRYPTO_r_unlock(type) \
  48. CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_READ,type,__FILE__,__LINE__)
  49. #define CRYPTO_add(addr,amount,type) \
  50. CRYPTO_add_lock(addr,amount,type,__FILE__,__LINE__)
  51. =head1 DESCRIPTION
  52. OpenSSL can safely be used in multi-threaded applications provided
  53. that at least two callback functions are set, locking_function and
  54. threadid_func.
  55. locking_function(int mode, int n, const char *file, int line) is
  56. needed to perform locking on shared data structures.
  57. (Note that OpenSSL uses a number of global data structures that
  58. will be implicitly shared whenever multiple threads use OpenSSL.)
  59. Multi-threaded applications will crash at random if it is not set.
  60. locking_function() must be able to handle up to CRYPTO_num_locks()
  61. different mutex locks. It sets the B<n>-th lock if B<mode> &
  62. B<CRYPTO_LOCK>, and releases it otherwise.
  63. B<file> and B<line> are the file number of the function setting the
  64. lock. They can be useful for debugging.
  65. threadid_func(CRYPTO_THREADID *id) is needed to record the currently-executing
  66. thread's identifier into B<id>. The implementation of this callback should not
  67. fill in B<id> directly, but should use CRYPTO_THREADID_set_numeric() if thread
  68. IDs are numeric, or CRYPTO_THREADID_set_pointer() if they are pointer-based.
  69. If the application does not register such a callback using
  70. CRYPTO_THREADID_set_callback(), then a default implementation is used - on
  71. Windows and BeOS this uses the system's default thread identifying APIs, and on
  72. all other platforms it uses the address of B<errno>. The latter is satisfactory
  73. for thread-safety if and only if the platform has a thread-local error number
  74. facility.
  75. Once threadid_func() is registered, or if the built-in default implementation is
  76. to be used;
  77. =over 4
  78. =item *
  79. CRYPTO_THREADID_current() records the currently-executing thread ID into the
  80. given B<id> object.
  81. =item *
  82. CRYPTO_THREADID_cmp() compares two thread IDs (returning zero for equality, ie.
  83. the same semantics as memcmp()).
  84. =item *
  85. CRYPTO_THREADID_cpy() duplicates a thread ID value,
  86. =item *
  87. CRYPTO_THREADID_hash() returns a numeric value usable as a hash-table key. This
  88. is usually the exact numeric or pointer-based thread ID used internally, however
  89. this also handles the unusual case where pointers are larger than 'long'
  90. variables and the platform's thread IDs are pointer-based - in this case, mixing
  91. is done to attempt to produce a unique numeric value even though it is not as
  92. wide as the platform's true thread IDs.
  93. =back
  94. Additionally, OpenSSL supports dynamic locks, and sometimes, some parts
  95. of OpenSSL need it for better performance. To enable this, the following
  96. is required:
  97. =over 4
  98. =item *
  99. Three additional callback function, dyn_create_function, dyn_lock_function
  100. and dyn_destroy_function.
  101. =item *
  102. A structure defined with the data that each lock needs to handle.
  103. =back
  104. struct CRYPTO_dynlock_value has to be defined to contain whatever structure
  105. is needed to handle locks.
  106. dyn_create_function(const char *file, int line) is needed to create a
  107. lock. Multi-threaded applications might crash at random if it is not set.
  108. dyn_lock_function(int mode, CRYPTO_dynlock *l, const char *file, int line)
  109. is needed to perform locking off dynamic lock numbered n. Multi-threaded
  110. applications might crash at random if it is not set.
  111. dyn_destroy_function(CRYPTO_dynlock *l, const char *file, int line) is
  112. needed to destroy the lock l. Multi-threaded applications might crash at
  113. random if it is not set.
  114. CRYPTO_get_new_dynlockid() is used to create locks. It will call
  115. dyn_create_function for the actual creation.
  116. CRYPTO_destroy_dynlockid() is used to destroy locks. It will call
  117. dyn_destroy_function for the actual destruction.
  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. CRYPTO_LOCK 0x01
  125. CRYPTO_UNLOCK 0x02
  126. CRYPTO_READ 0x04
  127. CRYPTO_WRITE 0x08
  128. =head1 RETURN VALUES
  129. CRYPTO_num_locks() returns the required number of locks.
  130. CRYPTO_get_new_dynlockid() returns the index to the newly created lock.
  131. The other functions return no values.
  132. =head1 NOTES
  133. You can find out if OpenSSL was configured with thread support:
  134. #define OPENSSL_THREAD_DEFINES
  135. #include <openssl/opensslconf.h>
  136. #if defined(OPENSSL_THREADS)
  137. // thread support enabled
  138. #else
  139. // no thread support
  140. #endif
  141. Also, dynamic locks are currently not used internally by OpenSSL, but
  142. may do so in the future.
  143. =head1 EXAMPLES
  144. B<crypto/threads/mttest.c> shows examples of the callback functions on
  145. Solaris, Irix and Win32.
  146. =head1 HISTORY
  147. CRYPTO_set_locking_callback() is
  148. available in all versions of SSLeay and OpenSSL.
  149. CRYPTO_num_locks() was added in OpenSSL 0.9.4.
  150. All functions dealing with dynamic locks were added in OpenSSL 0.9.5b-dev.
  151. B<CRYPTO_THREADID> and associated functions were introduced in OpenSSL 0.9.9
  152. to replace (actually, deprecate) the previous CRYPTO_set_id_callback(),
  153. CRYPTO_get_id_callback(), and CRYPTO_thread_id() functions which assumed
  154. thread IDs to always be represented by 'unsigned long'.
  155. =head1 SEE ALSO
  156. L<crypto(3)|crypto(3)>
  157. =cut