threads_none.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 = 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. static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
  60. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  61. {
  62. static unsigned int thread_local_key = 0;
  63. if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  64. return 0;
  65. *key = thread_local_key++;
  66. thread_local_storage[*key] = NULL;
  67. return 1;
  68. }
  69. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  70. {
  71. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  72. return NULL;
  73. return thread_local_storage[*key];
  74. }
  75. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  76. {
  77. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  78. return 0;
  79. thread_local_storage[*key] = val;
  80. return 1;
  81. }
  82. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  83. {
  84. *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
  85. return 1;
  86. }
  87. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  88. {
  89. return 0;
  90. }
  91. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  92. {
  93. return (a == b);
  94. }
  95. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  96. {
  97. *val += amount;
  98. *ret = *val;
  99. return 1;
  100. }
  101. int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
  102. CRYPTO_RWLOCK *lock)
  103. {
  104. *val |= op;
  105. *ret = *val;
  106. return 1;
  107. }
  108. int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
  109. {
  110. *ret = *val;
  111. return 1;
  112. }
  113. int openssl_init_fork_handlers(void)
  114. {
  115. return 0;
  116. }
  117. int openssl_get_fork_id(void)
  118. {
  119. # if defined(OPENSSL_SYS_UNIX)
  120. return getpid();
  121. # else
  122. return 0;
  123. # endif
  124. }
  125. #endif