thread_posix.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2019-2023 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. int rc;
  98. pthread_mutex_t *mutex_p;
  99. mutex_p = (pthread_mutex_t *)mutex;
  100. rc = pthread_mutex_lock(mutex_p);
  101. OPENSSL_assert(rc == 0);
  102. }
  103. void ossl_crypto_mutex_unlock(CRYPTO_MUTEX *mutex)
  104. {
  105. int rc;
  106. pthread_mutex_t *mutex_p;
  107. mutex_p = (pthread_mutex_t *)mutex;
  108. rc = pthread_mutex_unlock(mutex_p);
  109. OPENSSL_assert(rc == 0);
  110. }
  111. void ossl_crypto_mutex_free(CRYPTO_MUTEX **mutex)
  112. {
  113. pthread_mutex_t **mutex_p;
  114. if (mutex == NULL)
  115. return;
  116. mutex_p = (pthread_mutex_t **)mutex;
  117. if (*mutex_p != NULL)
  118. pthread_mutex_destroy(*mutex_p);
  119. OPENSSL_free(*mutex_p);
  120. *mutex = NULL;
  121. }
  122. CRYPTO_CONDVAR *ossl_crypto_condvar_new(void)
  123. {
  124. pthread_cond_t *cv_p;
  125. if ((cv_p = OPENSSL_zalloc(sizeof(*cv_p))) == NULL)
  126. return NULL;
  127. if (pthread_cond_init(cv_p, NULL) != 0) {
  128. OPENSSL_free(cv_p);
  129. return NULL;
  130. }
  131. return (CRYPTO_CONDVAR *) cv_p;
  132. }
  133. void ossl_crypto_condvar_wait(CRYPTO_CONDVAR *cv, CRYPTO_MUTEX *mutex)
  134. {
  135. pthread_cond_t *cv_p;
  136. pthread_mutex_t *mutex_p;
  137. cv_p = (pthread_cond_t *)cv;
  138. mutex_p = (pthread_mutex_t *)mutex;
  139. pthread_cond_wait(cv_p, mutex_p);
  140. }
  141. void ossl_crypto_condvar_wait_timeout(CRYPTO_CONDVAR *cv, CRYPTO_MUTEX *mutex,
  142. OSSL_TIME deadline)
  143. {
  144. pthread_cond_t *cv_p = (pthread_cond_t *)cv;
  145. pthread_mutex_t *mutex_p = (pthread_mutex_t *)mutex;
  146. if (ossl_time_is_infinite(deadline)) {
  147. /*
  148. * No deadline. Some pthread implementations allow
  149. * pthread_cond_timedwait to work the same as pthread_cond_wait when
  150. * abstime is NULL, but it is unclear whether this is POSIXly correct.
  151. */
  152. pthread_cond_wait(cv_p, mutex_p);
  153. } else {
  154. struct timespec deadline_ts;
  155. deadline_ts.tv_sec
  156. = ossl_time2seconds(deadline);
  157. deadline_ts.tv_nsec
  158. = (ossl_time2ticks(deadline) % OSSL_TIME_SECOND) / OSSL_TIME_NS;
  159. pthread_cond_timedwait(cv_p, mutex_p, &deadline_ts);
  160. }
  161. }
  162. void ossl_crypto_condvar_broadcast(CRYPTO_CONDVAR *cv)
  163. {
  164. pthread_cond_t *cv_p;
  165. cv_p = (pthread_cond_t *)cv;
  166. pthread_cond_broadcast(cv_p);
  167. }
  168. void ossl_crypto_condvar_signal(CRYPTO_CONDVAR *cv)
  169. {
  170. pthread_cond_t *cv_p;
  171. cv_p = (pthread_cond_t *)cv;
  172. pthread_cond_signal(cv_p);
  173. }
  174. void ossl_crypto_condvar_free(CRYPTO_CONDVAR **cv)
  175. {
  176. pthread_cond_t **cv_p;
  177. if (cv == NULL)
  178. return;
  179. cv_p = (pthread_cond_t **)cv;
  180. if (*cv_p != NULL)
  181. pthread_cond_destroy(*cv_p);
  182. OPENSSL_free(*cv_p);
  183. *cv_p = NULL;
  184. }
  185. #endif