threads_none.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2016-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 <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 = OPENSSL_zalloc(sizeof(unsigned int))) == NULL) {
  20. /* Don't set error, to avoid recursion blowup. */
  21. return NULL;
  22. }
  23. *(unsigned int *)lock = 1;
  24. return lock;
  25. }
  26. __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
  27. {
  28. if (!ossl_assert(*(unsigned int *)lock == 1))
  29. return 0;
  30. return 1;
  31. }
  32. __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
  33. {
  34. if (!ossl_assert(*(unsigned int *)lock == 1))
  35. return 0;
  36. return 1;
  37. }
  38. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
  39. {
  40. if (!ossl_assert(*(unsigned int *)lock == 1))
  41. return 0;
  42. return 1;
  43. }
  44. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
  45. if (lock == NULL)
  46. return;
  47. *(unsigned int *)lock = 0;
  48. OPENSSL_free(lock);
  49. return;
  50. }
  51. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
  52. {
  53. if (*once != 0)
  54. return 1;
  55. init();
  56. *once = 1;
  57. return 1;
  58. }
  59. #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
  60. static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
  61. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  62. {
  63. static unsigned int thread_local_key = 0;
  64. if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  65. return 0;
  66. *key = thread_local_key++;
  67. thread_local_storage[*key] = NULL;
  68. return 1;
  69. }
  70. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  71. {
  72. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  73. return NULL;
  74. return thread_local_storage[*key];
  75. }
  76. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  77. {
  78. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  79. return 0;
  80. thread_local_storage[*key] = val;
  81. return 1;
  82. }
  83. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  84. {
  85. *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
  86. return 1;
  87. }
  88. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  89. {
  90. return 0;
  91. }
  92. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  93. {
  94. return (a == b);
  95. }
  96. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  97. {
  98. *val += amount;
  99. *ret = *val;
  100. return 1;
  101. }
  102. int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
  103. CRYPTO_RWLOCK *lock)
  104. {
  105. *val |= op;
  106. *ret = *val;
  107. return 1;
  108. }
  109. int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
  110. {
  111. *ret = *val;
  112. return 1;
  113. }
  114. int openssl_init_fork_handlers(void)
  115. {
  116. return 0;
  117. }
  118. int openssl_get_fork_id(void)
  119. {
  120. # if defined(OPENSSL_SYS_UNIX)
  121. return getpid();
  122. # else
  123. return 0;
  124. # endif
  125. }
  126. #endif