thread_posix.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <internal/thread_arch.h>
  10. #if defined(OPENSSL_THREADS_POSIX)
  11. # define _GNU_SOURCE
  12. # include <errno.h>
  13. # include <sys/types.h>
  14. # include <unistd.h>
  15. static void *thread_start_thunk(void *vthread)
  16. {
  17. CRYPTO_THREAD *thread;
  18. CRYPTO_THREAD_RETVAL ret;
  19. thread = (CRYPTO_THREAD *)vthread;
  20. ret = thread->routine(thread->data);
  21. ossl_crypto_mutex_lock(thread->statelock);
  22. CRYPTO_THREAD_SET_STATE(thread, CRYPTO_THREAD_FINISHED);
  23. thread->retval = ret;
  24. ossl_crypto_condvar_broadcast(thread->condvar);
  25. ossl_crypto_mutex_unlock(thread->statelock);
  26. return NULL;
  27. }
  28. int ossl_crypto_thread_native_spawn(CRYPTO_THREAD *thread)
  29. {
  30. int ret;
  31. pthread_attr_t attr;
  32. pthread_t *handle;
  33. handle = OPENSSL_zalloc(sizeof(*handle));
  34. if (handle == NULL)
  35. goto fail;
  36. pthread_attr_init(&attr);
  37. if (!thread->joinable)
  38. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  39. ret = pthread_create(handle, &attr, thread_start_thunk, thread);
  40. pthread_attr_destroy(&attr);
  41. if (ret != 0)
  42. goto fail;
  43. thread->handle = handle;
  44. return 1;
  45. fail:
  46. thread->handle = NULL;
  47. OPENSSL_free(handle);
  48. return 0;
  49. }
  50. int ossl_crypto_thread_native_perform_join(CRYPTO_THREAD *thread, CRYPTO_THREAD_RETVAL *retval)
  51. {
  52. void *thread_retval;
  53. pthread_t *handle;
  54. if (thread == NULL || thread->handle == NULL)
  55. return 0;
  56. handle = (pthread_t *) thread->handle;
  57. if (pthread_join(*handle, &thread_retval) != 0)
  58. return 0;
  59. /*
  60. * Join return value may be non-NULL when the thread has been cancelled,
  61. * as indicated by thread_retval set to PTHREAD_CANCELLED.
  62. */
  63. if (thread_retval != NULL)
  64. return 0;
  65. return 1;
  66. }
  67. int ossl_crypto_thread_native_exit(void)
  68. {
  69. pthread_exit(NULL);
  70. return 1;
  71. }
  72. int ossl_crypto_thread_native_is_self(CRYPTO_THREAD *thread)
  73. {
  74. return pthread_equal(*(pthread_t *)thread->handle, pthread_self());
  75. }
  76. CRYPTO_MUTEX *ossl_crypto_mutex_new(void)
  77. {
  78. pthread_mutex_t *mutex;
  79. if ((mutex = OPENSSL_zalloc(sizeof(*mutex))) == NULL)
  80. return NULL;
  81. if (pthread_mutex_init(mutex, NULL) != 0) {
  82. OPENSSL_free(mutex);
  83. return NULL;
  84. }
  85. return (CRYPTO_MUTEX *)mutex;
  86. }
  87. int ossl_crypto_mutex_try_lock(CRYPTO_MUTEX *mutex)
  88. {
  89. pthread_mutex_t *mutex_p;
  90. mutex_p = (pthread_mutex_t *)mutex;
  91. if (pthread_mutex_trylock(mutex_p) == EBUSY)
  92. return 0;
  93. return 1;
  94. }
  95. void ossl_crypto_mutex_lock(CRYPTO_MUTEX *mutex)
  96. {
  97. pthread_mutex_t *mutex_p;
  98. mutex_p = (pthread_mutex_t *)mutex;
  99. pthread_mutex_lock(mutex_p);
  100. }
  101. void ossl_crypto_mutex_unlock(CRYPTO_MUTEX *mutex)
  102. {
  103. pthread_mutex_t *mutex_p;
  104. mutex_p = (pthread_mutex_t *)mutex;
  105. pthread_mutex_unlock(mutex_p);
  106. }
  107. void ossl_crypto_mutex_free(CRYPTO_MUTEX **mutex)
  108. {
  109. pthread_mutex_t **mutex_p;
  110. if (mutex == NULL)
  111. return;
  112. mutex_p = (pthread_mutex_t **)mutex;
  113. if (*mutex_p != NULL)
  114. pthread_mutex_destroy(*mutex_p);
  115. OPENSSL_free(*mutex_p);
  116. *mutex = NULL;
  117. }
  118. CRYPTO_CONDVAR *ossl_crypto_condvar_new(void)
  119. {
  120. pthread_cond_t *cv_p;
  121. if ((cv_p = OPENSSL_zalloc(sizeof(*cv_p))) == NULL)
  122. return NULL;
  123. if (pthread_cond_init(cv_p, NULL) != 0) {
  124. OPENSSL_free(cv_p);
  125. return NULL;
  126. }
  127. return (CRYPTO_CONDVAR *) cv_p;
  128. }
  129. void ossl_crypto_condvar_wait(CRYPTO_CONDVAR *cv, CRYPTO_MUTEX *mutex)
  130. {
  131. pthread_cond_t *cv_p;
  132. pthread_mutex_t *mutex_p;
  133. cv_p = (pthread_cond_t *)cv;
  134. mutex_p = (pthread_mutex_t *)mutex;
  135. pthread_cond_wait(cv_p, mutex_p);
  136. }
  137. void ossl_crypto_condvar_broadcast(CRYPTO_CONDVAR *cv)
  138. {
  139. pthread_cond_t *cv_p;
  140. cv_p = (pthread_cond_t *)cv;
  141. pthread_cond_broadcast(cv_p);
  142. }
  143. void ossl_crypto_condvar_free(CRYPTO_CONDVAR **cv)
  144. {
  145. pthread_cond_t **cv_p;
  146. if (cv == NULL)
  147. return;
  148. cv_p = (pthread_cond_t **)cv;
  149. if (*cv_p != NULL)
  150. pthread_cond_destroy(*cv_p);
  151. OPENSSL_free(*cv_p);
  152. *cv_p = NULL;
  153. }
  154. void ossl_crypto_mem_barrier(void)
  155. {
  156. # if defined(__clang__) || defined(__GNUC__)
  157. __sync_synchronize();
  158. # elif !defined(OPENSSL_NO_ASM)
  159. # if defined(__alpha__) /* Alpha */
  160. __asm__ volatile("mb" : : : "memory");
  161. # elif defined(__amd64__) || defined(__i386__) || defined(__i486__) \
  162. || defined(__i586__) || defined(__i686__) || defined(__i386) /* x86 */
  163. __asm__ volatile("mfence" : : : "memory");
  164. # elif defined(__arm__) || defined(__aarch64__) /* ARMv7, ARMv8 */
  165. __asm__ volatile("dmb ish" : : : "memory");
  166. # elif defined(__hppa__) /* PARISC */
  167. __asm__ volatile("" : : : "memory");
  168. # elif defined(__mips__) /* MIPS */
  169. __asm__ volatile("sync" : : : "memory");
  170. # elif defined(__powerpc__) || defined(__powerpc64__) /* power, ppc64, ppc64le */
  171. __asm__ volatile("sync" : : : "memory");
  172. # elif defined(__sparc__)
  173. __asm__ volatile("ba,pt %%xcc, 1f\n\t" \
  174. " membar #Sync\n" \
  175. "1:\n" \
  176. : : : "memory");
  177. # elif defined(__s390__) || defined(__s390x__) /* z */
  178. __asm__ volatile("bcr 15,0" : : : "memory");
  179. # elif defined(__riscv) || defined(__riscv__) /* riscv */
  180. __asm__ volatile("fence iorw,iorw" : : : "memory");
  181. # else /* others, compiler only */
  182. __asm__ volatile("" : : : "memory");
  183. # endif
  184. # else
  185. /* compiler only barrier */
  186. __asm__ volatile("" : : : "memory");
  187. # endif
  188. }
  189. #endif