No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

thread.h 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #ifndef OPENSSL_HEADER_THREAD_H
  57. #define OPENSSL_HEADER_THREAD_H
  58. #include <openssl/base.h>
  59. #if defined(__cplusplus)
  60. extern "C" {
  61. #endif
  62. /* Functions to support multithreading.
  63. *
  64. * OpenSSL can safely be used in multi-threaded applications provided that at
  65. * least two callback functions are set with |CRYPTO_set_locking_callback| and
  66. * |CRYPTO_THREADID_set_callback|.
  67. *
  68. * The locking callback performs mutual exclusion. Rather than using a single
  69. * lock for all, shared data-structures, OpenSSL requires that the locking
  70. * callback support a fixed (at run-time) number of different locks, given by
  71. * |CRYPTO_num_locks|.
  72. *
  73. * The thread ID callback is called to record the currently executing thread's
  74. * identifier in a |CRYPTO_THREADID| structure. If this callback is not
  75. * provided then the address of |errno| is used as the thread identifier. This
  76. * is sufficient only if the system has a thread-local |errno| value. */
  77. /* CRYPTO_num_locks returns the number of static locks that the callback
  78. * function passed to |CRYPTO_set_locking_callback| must be able to handle. */
  79. OPENSSL_EXPORT int CRYPTO_num_locks(void);
  80. /* CRYPTO_set_locking_callback sets a callback function that implements locking
  81. * on behalf of OpenSSL. The callback is called whenever OpenSSL needs to lock
  82. * or unlock a lock, and locks are specified as a number between zero and
  83. * |CRYPTO_num_locks()-1|.
  84. *
  85. * The mode argument to the callback is a bitwise-OR of either CRYPTO_LOCK or
  86. * CRYPTO_UNLOCK, to denote the action, and CRYPTO_READ or CRYPTO_WRITE, to
  87. * indicate the type of lock. The |file| and |line| arguments give the location
  88. * in the OpenSSL source where the locking action originated. */
  89. OPENSSL_EXPORT void CRYPTO_set_locking_callback(
  90. void (*func)(int mode, int lock_num, const char *file, int line));
  91. /* CRYPTO_set_add_lock_callback sets an optional callback which is used when
  92. * OpenSSL needs to add a fixed amount to an integer. For example, this is used
  93. * when maintaining reference counts. Normally the reference counts are
  94. * maintained by performing the addition under a lock but, if this callback
  95. * has been set, the application is free to implement the operation using
  96. * faster methods (i.e. atomic operations).
  97. *
  98. * The callback is given a pointer to the integer to be altered (|num|), the
  99. * amount to add to the integer (|amount|, which may be negative), the number
  100. * of the lock which would have been taken to protect the operation and the
  101. * position in the OpenSSL code where the operation originated. */
  102. OPENSSL_EXPORT void CRYPTO_set_add_lock_callback(int (*func)(
  103. int *num, int amount, int lock_num, const char *file, int line));
  104. /* CRYPTO_get_lock_name returns the name of the lock given by |lock_num|. This
  105. * can be used in a locking callback for debugging purposes. */
  106. OPENSSL_EXPORT const char *CRYPTO_get_lock_name(int lock_num);
  107. /* CRYPTO_THREADID identifies a thread in a multithreaded program. This
  108. * structure should not be used directly. Rather applications should use
  109. * |CRYPTO_THREADID_set_numeric| and |CRYPTO_THREADID_set_pointer|. */
  110. typedef struct crypto_threadid_st {
  111. void *ptr;
  112. unsigned long val;
  113. } CRYPTO_THREADID;
  114. /* CRYPTO_THREADID_set_callback sets a callback function that stores an
  115. * identifier of the currently executing thread into |threadid|. The
  116. * CRYPTO_THREADID structure should not be accessed directly. Rather one of
  117. * |CRYPTO_THREADID_set_numeric| or |CRYPTO_THREADID_set_pointer| should be
  118. * used depending on whether thread IDs are numbers or pointers on the host
  119. * system. */
  120. OPENSSL_EXPORT int CRYPTO_THREADID_set_callback(
  121. void (*threadid_func)(CRYPTO_THREADID *threadid));
  122. OPENSSL_EXPORT void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id,
  123. unsigned long val);
  124. OPENSSL_EXPORT void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr);
  125. /* Private functions: */
  126. /* CRYPTO_get_locking_callback returns the callback, if any, that was most
  127. * recently set using |CRYPTO_set_locking_callback|. */
  128. void (*CRYPTO_get_locking_callback(void))(int mode, int lock_num,
  129. const char *file, int line);
  130. /* CRYPTO_get_add_lock_callback returns the callback, if any, that was most
  131. * recently set using |CRYPTO_set_add_lock_callback|. */
  132. int (*CRYPTO_get_add_lock_callback(void))(int *num, int amount, int lock_num,
  133. const char *file, int line);
  134. /* CRYPTO_lock locks or unlocks the lock specified by |lock_num| (one of
  135. * |CRYPTO_LOCK_*|). Don't call this directly, rather use one of the
  136. * CRYPTO_[rw]_(un)lock macros. */
  137. OPENSSL_EXPORT void CRYPTO_lock(int mode, int lock_num, const char *file,
  138. int line);
  139. /* CRYPTO_add_lock adds |amount| to |*pointer|, protected by the lock specified
  140. * by |lock_num|. It returns the new value of |*pointer|. Don't call this
  141. * function directly, rather use the |CRYPTO_add_lock| macro.
  142. *
  143. * TODO(fork): rename to CRYPTO_add_locked. */
  144. OPENSSL_EXPORT int CRYPTO_add_lock(int *pointer, int amount, int lock_num,
  145. const char *file, int line);
  146. /* CRYPTO_THREADID_current stores the current thread identifier in |id|. */
  147. OPENSSL_EXPORT void CRYPTO_THREADID_current(CRYPTO_THREADID *id);
  148. /* CRYPTO_THREADID_cmp returns < 0, 0 or > 0 if |a| is less than, equal to or
  149. * greater than |b|, respectively. */
  150. int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b);
  151. /* CRYPTO_THREADID_cpy sets |*dest| equal to |*src|. */
  152. void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src);
  153. /* CRYPTO_THREADID_hash returns a hash of the numeric value of |id|. */
  154. uint32_t CRYPTO_THREADID_hash(const CRYPTO_THREADID *id);
  155. /* These are the locks used by OpenSSL. These values should match up with the
  156. * table in thread.c. */
  157. #define CRYPTO_LOCK_ERR 1
  158. #define CRYPTO_LOCK_EX_DATA 2
  159. #define CRYPTO_LOCK_X509 3
  160. #define CRYPTO_LOCK_X509_INFO 4
  161. #define CRYPTO_LOCK_X509_PKEY 5
  162. #define CRYPTO_LOCK_X509_CRL 6
  163. #define CRYPTO_LOCK_X509_REQ 7
  164. #define CRYPTO_LOCK_DSA 8
  165. #define CRYPTO_LOCK_RSA 9
  166. #define CRYPTO_LOCK_EVP_PKEY 10
  167. #define CRYPTO_LOCK_X509_STORE 11
  168. #define CRYPTO_LOCK_SSL_CTX 12
  169. #define CRYPTO_LOCK_SSL_CERT 13
  170. #define CRYPTO_LOCK_SSL_SESSION 14
  171. #define CRYPTO_LOCK_SSL_SESS_CERT 15
  172. #define CRYPTO_LOCK_SSL 16
  173. #define CRYPTO_LOCK_SSL_METHOD 17
  174. #define CRYPTO_LOCK_RAND 18
  175. #define CRYPTO_LOCK_RAND2 19
  176. #define CRYPTO_LOCK_MALLOC 20
  177. #define CRYPTO_LOCK_BIO 21
  178. #define CRYPTO_LOCK_GETHOSTBYNAME 22
  179. #define CRYPTO_LOCK_GETSERVBYNAME 23
  180. #define CRYPTO_LOCK_READDIR 24
  181. #define CRYPTO_LOCK_RSA_BLINDING 25
  182. #define CRYPTO_LOCK_DH 26
  183. #define CRYPTO_LOCK_MALLOC2 27
  184. #define CRYPTO_LOCK_DSO 28
  185. #define CRYPTO_LOCK_DYNLOCK 29
  186. #define CRYPTO_LOCK_ENGINE 30
  187. #define CRYPTO_LOCK_UI 31
  188. #define CRYPTO_LOCK_ECDSA 32
  189. #define CRYPTO_LOCK_EC 33
  190. #define CRYPTO_LOCK_ECDH 34
  191. #define CRYPTO_LOCK_BN 35
  192. #define CRYPTO_LOCK_EC_PRE_COMP 36
  193. #define CRYPTO_LOCK_STORE 37
  194. #define CRYPTO_LOCK_COMP 38
  195. #define CRYPTO_LOCK_FIPS 39
  196. #define CRYPTO_LOCK_FIPS2 40
  197. #define CRYPTO_LOCK_OBJ 40
  198. #define CRYPTO_NUM_LOCKS 42
  199. #define CRYPTO_LOCK 1
  200. #define CRYPTO_UNLOCK 2
  201. #define CRYPTO_READ 4
  202. #define CRYPTO_WRITE 8
  203. #define CRYPTO_w_lock(lock_num) \
  204. CRYPTO_lock(CRYPTO_LOCK | CRYPTO_WRITE, lock_num, __FILE__, __LINE__)
  205. #define CRYPTO_w_unlock(lock_num) \
  206. CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_WRITE, lock_num, __FILE__, __LINE__)
  207. #define CRYPTO_r_lock(lock_num) \
  208. CRYPTO_lock(CRYPTO_LOCK | CRYPTO_READ, lock_num, __FILE__, __LINE__)
  209. #define CRYPTO_r_unlock(lock_num) \
  210. CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_READ, lock_num, __FILE__, __LINE__)
  211. #define CRYPTO_add(addr, amount, lock_num) \
  212. CRYPTO_add_lock(addr, amount, lock_num, __FILE__, __LINE__)
  213. /* Private functions.
  214. *
  215. * Some old code calls these functions and so no-op implementations are
  216. * provided.
  217. *
  218. * TODO(fork): cleanup callers and remove. */
  219. OPENSSL_EXPORT void CRYPTO_set_id_callback(unsigned long (*func)(void));
  220. typedef struct {
  221. int references;
  222. struct CRYPTO_dynlock_value *data;
  223. } CRYPTO_dynlock;
  224. OPENSSL_EXPORT void CRYPTO_set_dynlock_create_callback(
  225. struct CRYPTO_dynlock_value *(*dyn_create_function)(const char *file,
  226. int line));
  227. OPENSSL_EXPORT void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)(
  228. int mode, struct CRYPTO_dynlock_value *l, const char *file, int line));
  229. OPENSSL_EXPORT void CRYPTO_set_dynlock_destroy_callback(
  230. void (*dyn_destroy_function)(struct CRYPTO_dynlock_value *l,
  231. const char *file, int line));
  232. #if defined(__cplusplus)
  233. } /* extern C */
  234. #endif
  235. #endif /* OPENSSL_HEADER_THREAD_H */