th-lock.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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,int line);
  90. static void win32_locking_callback(int mode,int type,char *file,int line);
  91. static void pthreads_locking_callback(int mode,int type,char *file,int line);
  92. static unsigned long irix_thread_id(void );
  93. static unsigned long solaris_thread_id(void );
  94. static unsigned long pthreads_thread_id(void );
  95. /* usage:
  96. * CRYPTO_thread_setup();
  97. * application code
  98. * CRYPTO_thread_cleanup();
  99. */
  100. #define THREAD_STACK_SIZE (16*1024)
  101. #ifdef OPENSSL_SYS_WIN32
  102. static HANDLE *lock_cs;
  103. void CRYPTO_thread_setup(void)
  104. {
  105. int i;
  106. lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
  107. for (i=0; i<CRYPTO_num_locks(); i++)
  108. {
  109. lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
  110. }
  111. CRYPTO_set_locking_callback((void (*)(int,int,char *,int))win32_locking_callback);
  112. /* id callback defined */
  113. return(1);
  114. }
  115. static void CRYPTO_thread_cleanup(void)
  116. {
  117. int i;
  118. CRYPTO_set_locking_callback(NULL);
  119. for (i=0; i<CRYPTO_num_locks(); i++)
  120. CloseHandle(lock_cs[i]);
  121. OPENSSL_free(lock_cs);
  122. }
  123. void win32_locking_callback(int mode, int type, char *file, int line)
  124. {
  125. if (mode & CRYPTO_LOCK)
  126. {
  127. WaitForSingleObject(lock_cs[type],INFINITE);
  128. }
  129. else
  130. {
  131. ReleaseMutex(lock_cs[type]);
  132. }
  133. }
  134. #endif /* OPENSSL_SYS_WIN32 */
  135. #ifdef SOLARIS
  136. #define USE_MUTEX
  137. #ifdef USE_MUTEX
  138. static mutex_t *lock_cs;
  139. #else
  140. static rwlock_t *lock_cs;
  141. #endif
  142. static long *lock_count;
  143. void CRYPTO_thread_setup(void)
  144. {
  145. int i;
  146. #ifdef USE_MUTEX
  147. lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t));
  148. #else
  149. lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(rwlock_t));
  150. #endif
  151. lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
  152. for (i=0; i<CRYPTO_num_locks(); i++)
  153. {
  154. lock_count[i]=0;
  155. #ifdef USE_MUTEX
  156. mutex_init(&(lock_cs[i]),USYNC_THREAD,NULL);
  157. #else
  158. rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL);
  159. #endif
  160. }
  161. CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id);
  162. CRYPTO_set_locking_callback((void (*)())solaris_locking_callback);
  163. }
  164. void CRYPTO_thread_cleanup(void)
  165. {
  166. int i;
  167. CRYPTO_set_locking_callback(NULL);
  168. for (i=0; i<CRYPTO_num_locks(); i++)
  169. {
  170. #ifdef USE_MUTEX
  171. mutex_destroy(&(lock_cs[i]));
  172. #else
  173. rwlock_destroy(&(lock_cs[i]));
  174. #endif
  175. }
  176. OPENSSL_free(lock_cs);
  177. OPENSSL_free(lock_count);
  178. }
  179. void solaris_locking_callback(int mode, int type, char *file, int line)
  180. {
  181. #if 0
  182. fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
  183. CRYPTO_thread_id(),
  184. (mode&CRYPTO_LOCK)?"l":"u",
  185. (type&CRYPTO_READ)?"r":"w",file,line);
  186. #endif
  187. #if 0
  188. if (CRYPTO_LOCK_SSL_CERT == type)
  189. fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
  190. CRYPTO_thread_id(),
  191. mode,file,line);
  192. #endif
  193. if (mode & CRYPTO_LOCK)
  194. {
  195. #ifdef USE_MUTEX
  196. mutex_lock(&(lock_cs[type]));
  197. #else
  198. if (mode & CRYPTO_READ)
  199. rw_rdlock(&(lock_cs[type]));
  200. else
  201. rw_wrlock(&(lock_cs[type]));
  202. #endif
  203. lock_count[type]++;
  204. }
  205. else
  206. {
  207. #ifdef USE_MUTEX
  208. mutex_unlock(&(lock_cs[type]));
  209. #else
  210. rw_unlock(&(lock_cs[type]));
  211. #endif
  212. }
  213. }
  214. unsigned long solaris_thread_id(void)
  215. {
  216. unsigned long ret;
  217. ret=(unsigned long)thr_self();
  218. return(ret);
  219. }
  220. #endif /* SOLARIS */
  221. #ifdef IRIX
  222. /* I don't think this works..... */
  223. static usptr_t *arena;
  224. static usema_t **lock_cs;
  225. void CRYPTO_thread_setup(void)
  226. {
  227. int i;
  228. char filename[20];
  229. strcpy(filename,"/tmp/mttest.XXXXXX");
  230. mktemp(filename);
  231. usconfig(CONF_STHREADIOOFF);
  232. usconfig(CONF_STHREADMALLOCOFF);
  233. usconfig(CONF_INITUSERS,100);
  234. usconfig(CONF_LOCKTYPE,US_DEBUGPLUS);
  235. arena=usinit(filename);
  236. unlink(filename);
  237. lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
  238. for (i=0; i<CRYPTO_num_locks(); i++)
  239. {
  240. lock_cs[i]=usnewsema(arena,1);
  241. }
  242. CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id);
  243. CRYPTO_set_locking_callback((void (*)())irix_locking_callback);
  244. }
  245. void CRYPTO_thread_cleanup(void)
  246. {
  247. int i;
  248. CRYPTO_set_locking_callback(NULL);
  249. for (i=0; i<CRYPTO_num_locks(); i++)
  250. {
  251. char buf[10];
  252. sprintf(buf,"%2d:",i);
  253. usdumpsema(lock_cs[i],stdout,buf);
  254. usfreesema(lock_cs[i],arena);
  255. }
  256. OPENSSL_free(lock_cs);
  257. }
  258. void irix_locking_callback(int mode, int type, char *file, int line)
  259. {
  260. if (mode & CRYPTO_LOCK)
  261. {
  262. uspsema(lock_cs[type]);
  263. }
  264. else
  265. {
  266. usvsema(lock_cs[type]);
  267. }
  268. }
  269. unsigned long irix_thread_id(void)
  270. {
  271. unsigned long ret;
  272. ret=(unsigned long)getpid();
  273. return(ret);
  274. }
  275. #endif /* IRIX */
  276. /* Linux and a few others */
  277. #ifdef PTHREADS
  278. static pthread_mutex_t *lock_cs;
  279. static long *lock_count;
  280. void CRYPTO_thread_setup(void)
  281. {
  282. int i;
  283. lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
  284. lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
  285. for (i=0; i<CRYPTO_num_locks(); i++)
  286. {
  287. lock_count[i]=0;
  288. pthread_mutex_init(&(lock_cs[i]),NULL);
  289. }
  290. CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
  291. CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback);
  292. }
  293. void thread_cleanup(void)
  294. {
  295. int i;
  296. CRYPTO_set_locking_callback(NULL);
  297. for (i=0; i<CRYPTO_num_locks(); i++)
  298. {
  299. pthread_mutex_destroy(&(lock_cs[i]));
  300. }
  301. OPENSSL_free(lock_cs);
  302. OPENSSL_free(lock_count);
  303. }
  304. void pthreads_locking_callback(int mode, int type, char *file,
  305. int line)
  306. {
  307. #if 0
  308. fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
  309. CRYPTO_thread_id(),
  310. (mode&CRYPTO_LOCK)?"l":"u",
  311. (type&CRYPTO_READ)?"r":"w",file,line);
  312. #endif
  313. #if 0
  314. if (CRYPTO_LOCK_SSL_CERT == type)
  315. fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
  316. CRYPTO_thread_id(),
  317. mode,file,line);
  318. #endif
  319. if (mode & CRYPTO_LOCK)
  320. {
  321. pthread_mutex_lock(&(lock_cs[type]));
  322. lock_count[type]++;
  323. }
  324. else
  325. {
  326. pthread_mutex_unlock(&(lock_cs[type]));
  327. }
  328. }
  329. unsigned long pthreads_thread_id(void)
  330. {
  331. unsigned long ret;
  332. ret=(unsigned long)pthread_self();
  333. return(ret);
  334. }
  335. #endif /* PTHREADS */