1
0

threads_none.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2016-2025 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 <openssl/crypto.h>
  10. #include "internal/cryptlib.h"
  11. #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
  12. # if defined(OPENSSL_SYS_UNIX)
  13. # include <sys/types.h>
  14. # include <unistd.h>
  15. # endif
  16. CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
  17. {
  18. CRYPTO_RWLOCK *lock;
  19. if ((lock = CRYPTO_zalloc(sizeof(unsigned int), NULL, 0)) == NULL)
  20. /* Don't set error, to avoid recursion blowup. */
  21. return NULL;
  22. *(unsigned int *)lock = 1;
  23. return lock;
  24. }
  25. __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
  26. {
  27. if (!ossl_assert(*(unsigned int *)lock == 1))
  28. return 0;
  29. return 1;
  30. }
  31. __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
  32. {
  33. if (!ossl_assert(*(unsigned int *)lock == 1))
  34. return 0;
  35. return 1;
  36. }
  37. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
  38. {
  39. if (!ossl_assert(*(unsigned int *)lock == 1))
  40. return 0;
  41. return 1;
  42. }
  43. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
  44. if (lock == NULL)
  45. return;
  46. *(unsigned int *)lock = 0;
  47. OPENSSL_free(lock);
  48. return;
  49. }
  50. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
  51. {
  52. if (*once != 0)
  53. return 1;
  54. init();
  55. *once = 1;
  56. return 1;
  57. }
  58. #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
  59. struct thread_local_storage_entry {
  60. void *data;
  61. uint8_t used;
  62. };
  63. static struct thread_local_storage_entry thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
  64. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  65. {
  66. int entry_idx = 0;
  67. for (entry_idx = 0; entry_idx < OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX; entry_idx++) {
  68. if (!thread_local_storage[entry_idx].used)
  69. break;
  70. }
  71. if (entry_idx == OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  72. return 0;
  73. *key = entry_idx;
  74. thread_local_storage[*key].used = 1;
  75. thread_local_storage[*key].data = NULL;
  76. return 1;
  77. }
  78. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  79. {
  80. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  81. return NULL;
  82. return thread_local_storage[*key].data;
  83. }
  84. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  85. {
  86. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  87. return 0;
  88. thread_local_storage[*key].data = val;
  89. return 1;
  90. }
  91. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  92. {
  93. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  94. return 0;
  95. thread_local_storage[*key].used = 0;
  96. thread_local_storage[*key].data = NULL;
  97. *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
  98. return 1;
  99. }
  100. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  101. {
  102. return 0;
  103. }
  104. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  105. {
  106. return (a == b);
  107. }
  108. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  109. {
  110. *val += amount;
  111. *ret = *val;
  112. return 1;
  113. }
  114. int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
  115. CRYPTO_RWLOCK *lock)
  116. {
  117. *val |= op;
  118. *ret = *val;
  119. return 1;
  120. }
  121. int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
  122. {
  123. *ret = *val;
  124. return 1;
  125. }
  126. int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
  127. {
  128. *ret = *val;
  129. return 1;
  130. }
  131. int openssl_init_fork_handlers(void)
  132. {
  133. return 0;
  134. }
  135. int openssl_get_fork_id(void)
  136. {
  137. # if defined(OPENSSL_SYS_UNIX)
  138. return getpid();
  139. # else
  140. return 0;
  141. # endif
  142. }
  143. #endif