th-lock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* crypto/threads/th-lock.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <errno.h>
  62. #ifdef LINUX
  63. # include <typedefs.h>
  64. #endif
  65. #ifdef OPENSSL_SYS_WIN32
  66. # include <windows.h>
  67. #endif
  68. #ifdef SOLARIS
  69. # include <synch.h>
  70. # include <thread.h>
  71. #endif
  72. #ifdef IRIX
  73. # include <ulocks.h>
  74. # include <sys/prctl.h>
  75. #endif
  76. #ifdef PTHREADS
  77. # include <pthread.h>
  78. #endif
  79. #include <openssl/lhash.h>
  80. #include <openssl/crypto.h>
  81. #include <openssl/buffer.h>
  82. #include "../../e_os.h"
  83. #include <openssl/x509.h>
  84. #include <openssl/ssl.h>
  85. #include <openssl/err.h>
  86. void CRYPTO_thread_setup(void);
  87. void CRYPTO_thread_cleanup(void);
  88. static void irix_locking_callback(int mode, int type, char *file, int line);
  89. static void solaris_locking_callback(int mode, int type, char *file,
  90. int line);
  91. static void win32_locking_callback(int mode, int type, char *file, int line);
  92. static void pthreads_locking_callback(int mode, int type, char *file,
  93. int line);
  94. static unsigned long irix_thread_id(void);
  95. static unsigned long solaris_thread_id(void);
  96. static unsigned long pthreads_thread_id(void);
  97. /*-
  98. * usage:
  99. * CRYPTO_thread_setup();
  100. * application code
  101. * CRYPTO_thread_cleanup();
  102. */
  103. #define THREAD_STACK_SIZE (16*1024)
  104. #ifdef OPENSSL_SYS_WIN32
  105. static HANDLE *lock_cs;
  106. void CRYPTO_thread_setup(void)
  107. {
  108. int i;
  109. lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
  110. if (!lock_cs) {
  111. /* Nothing we can do about this...void function! */
  112. return;
  113. }
  114. for (i = 0; i < CRYPTO_num_locks(); i++) {
  115. lock_cs[i] = CreateMutex(NULL, FALSE, NULL);
  116. }
  117. CRYPTO_set_locking_callback((void (*)(int, int, char *, int))
  118. win32_locking_callback);
  119. /* id callback defined */
  120. return (1);
  121. }
  122. static void CRYPTO_thread_cleanup(void)
  123. {
  124. int i;
  125. CRYPTO_set_locking_callback(NULL);
  126. for (i = 0; i < CRYPTO_num_locks(); i++)
  127. CloseHandle(lock_cs[i]);
  128. OPENSSL_free(lock_cs);
  129. }
  130. void win32_locking_callback(int mode, int type, char *file, int line)
  131. {
  132. if (mode & CRYPTO_LOCK) {
  133. WaitForSingleObject(lock_cs[type], INFINITE);
  134. } else {
  135. ReleaseMutex(lock_cs[type]);
  136. }
  137. }
  138. #endif /* OPENSSL_SYS_WIN32 */
  139. #ifdef SOLARIS
  140. # define USE_MUTEX
  141. # ifdef USE_MUTEX
  142. static mutex_t *lock_cs;
  143. # else
  144. static rwlock_t *lock_cs;
  145. # endif
  146. static long *lock_count;
  147. void CRYPTO_thread_setup(void)
  148. {
  149. int i;
  150. # ifdef USE_MUTEX
  151. lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t));
  152. # else
  153. lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(rwlock_t));
  154. # endif
  155. if (!lock_cs) {
  156. /* Nothing we can do about this...void function! */
  157. return;
  158. }
  159. lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
  160. for (i = 0; i < CRYPTO_num_locks(); i++) {
  161. lock_count[i] = 0;
  162. # ifdef USE_MUTEX
  163. mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL);
  164. # else
  165. rwlock_init(&(lock_cs[i]), USYNC_THREAD, NULL);
  166. # endif
  167. }
  168. CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
  169. CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
  170. }
  171. void CRYPTO_thread_cleanup(void)
  172. {
  173. int i;
  174. CRYPTO_set_locking_callback(NULL);
  175. for (i = 0; i < CRYPTO_num_locks(); i++) {
  176. # ifdef USE_MUTEX
  177. mutex_destroy(&(lock_cs[i]));
  178. # else
  179. rwlock_destroy(&(lock_cs[i]));
  180. # endif
  181. }
  182. OPENSSL_free(lock_cs);
  183. OPENSSL_free(lock_count);
  184. }
  185. void solaris_locking_callback(int mode, int type, char *file, int line)
  186. {
  187. # if 0
  188. fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n",
  189. CRYPTO_thread_id(),
  190. (mode & CRYPTO_LOCK) ? "l" : "u",
  191. (type & CRYPTO_READ) ? "r" : "w", file, line);
  192. # endif
  193. # if 0
  194. if (CRYPTO_LOCK_SSL_CERT == type)
  195. fprintf(stderr, "(t,m,f,l) %ld %d %s %d\n",
  196. CRYPTO_thread_id(), mode, file, line);
  197. # endif
  198. if (mode & CRYPTO_LOCK) {
  199. # ifdef USE_MUTEX
  200. mutex_lock(&(lock_cs[type]));
  201. # else
  202. if (mode & CRYPTO_READ)
  203. rw_rdlock(&(lock_cs[type]));
  204. else
  205. rw_wrlock(&(lock_cs[type]));
  206. # endif
  207. lock_count[type]++;
  208. } else {
  209. # ifdef USE_MUTEX
  210. mutex_unlock(&(lock_cs[type]));
  211. # else
  212. rw_unlock(&(lock_cs[type]));
  213. # endif
  214. }
  215. }
  216. unsigned long solaris_thread_id(void)
  217. {
  218. unsigned long ret;
  219. ret = (unsigned long)thr_self();
  220. return (ret);
  221. }
  222. #endif /* SOLARIS */
  223. #ifdef IRIX
  224. /* I don't think this works..... */
  225. static usptr_t *arena;
  226. static usema_t **lock_cs;
  227. void CRYPTO_thread_setup(void)
  228. {
  229. int i;
  230. char filename[20];
  231. lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
  232. if (!lock_cs) {
  233. /* Nothing we can do about this...void function! */
  234. return;
  235. }
  236. strcpy(filename, "/tmp/mttest.XXXXXX");
  237. mktemp(filename);
  238. usconfig(CONF_STHREADIOOFF);
  239. usconfig(CONF_STHREADMALLOCOFF);
  240. usconfig(CONF_INITUSERS, 100);
  241. usconfig(CONF_LOCKTYPE, US_DEBUGPLUS);
  242. arena = usinit(filename);
  243. unlink(filename);
  244. for (i = 0; i < CRYPTO_num_locks(); i++) {
  245. lock_cs[i] = usnewsema(arena, 1);
  246. }
  247. CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
  248. CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
  249. }
  250. void CRYPTO_thread_cleanup(void)
  251. {
  252. int i;
  253. CRYPTO_set_locking_callback(NULL);
  254. for (i = 0; i < CRYPTO_num_locks(); i++) {
  255. char buf[10];
  256. sprintf(buf, "%2d:", i);
  257. usdumpsema(lock_cs[i], stdout, buf);
  258. usfreesema(lock_cs[i], arena);
  259. }
  260. OPENSSL_free(lock_cs);
  261. }
  262. void irix_locking_callback(int mode, int type, char *file, int line)
  263. {
  264. if (mode & CRYPTO_LOCK) {
  265. uspsema(lock_cs[type]);
  266. } else {
  267. usvsema(lock_cs[type]);
  268. }
  269. }
  270. unsigned long irix_thread_id(void)
  271. {
  272. unsigned long ret;
  273. ret = (unsigned long)getpid();
  274. return (ret);
  275. }
  276. #endif /* IRIX */
  277. /* Linux and a few others */
  278. #ifdef PTHREADS
  279. static pthread_mutex_t *lock_cs;
  280. static long *lock_count;
  281. void CRYPTO_thread_setup(void)
  282. {
  283. int i;
  284. lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
  285. lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
  286. if (!lock_cs || !lock_count) {
  287. /* Nothing we can do about this...void function! */
  288. if (lock_cs)
  289. OPENSSL_free(lock_cs);
  290. if (lock_count)
  291. OPENSSL_free(lock_count);
  292. return;
  293. }
  294. for (i = 0; i < CRYPTO_num_locks(); i++) {
  295. lock_count[i] = 0;
  296. pthread_mutex_init(&(lock_cs[i]), NULL);
  297. }
  298. CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
  299. CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
  300. }
  301. void thread_cleanup(void)
  302. {
  303. int i;
  304. CRYPTO_set_locking_callback(NULL);
  305. for (i = 0; i < CRYPTO_num_locks(); i++) {
  306. pthread_mutex_destroy(&(lock_cs[i]));
  307. }
  308. OPENSSL_free(lock_cs);
  309. OPENSSL_free(lock_count);
  310. }
  311. void pthreads_locking_callback(int mode, int type, char *file, int line)
  312. {
  313. # if 0
  314. fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n",
  315. CRYPTO_thread_id(),
  316. (mode & CRYPTO_LOCK) ? "l" : "u",
  317. (type & CRYPTO_READ) ? "r" : "w", file, line);
  318. # endif
  319. # if 0
  320. if (CRYPTO_LOCK_SSL_CERT == type)
  321. fprintf(stderr, "(t,m,f,l) %ld %d %s %d\n",
  322. CRYPTO_thread_id(), mode, file, line);
  323. # endif
  324. if (mode & CRYPTO_LOCK) {
  325. pthread_mutex_lock(&(lock_cs[type]));
  326. lock_count[type]++;
  327. } else {
  328. pthread_mutex_unlock(&(lock_cs[type]));
  329. }
  330. }
  331. unsigned long pthreads_thread_id(void)
  332. {
  333. unsigned long ret;
  334. ret = (unsigned long)pthread_self();
  335. return (ret);
  336. }
  337. #endif /* PTHREADS */