threads_none.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
  13. {
  14. CRYPTO_RWLOCK *lock;
  15. if ((lock = OPENSSL_zalloc(sizeof(unsigned int))) == NULL) {
  16. /* Don't set error, to avoid recursion blowup. */
  17. return NULL;
  18. }
  19. *(unsigned int *)lock = 1;
  20. return lock;
  21. }
  22. int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
  23. {
  24. if (!ossl_assert(*(unsigned int *)lock == 1))
  25. return 0;
  26. return 1;
  27. }
  28. int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
  29. {
  30. if (!ossl_assert(*(unsigned int *)lock == 1))
  31. return 0;
  32. return 1;
  33. }
  34. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
  35. {
  36. if (!ossl_assert(*(unsigned int *)lock == 1))
  37. return 0;
  38. return 1;
  39. }
  40. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
  41. if (lock == NULL)
  42. return;
  43. *(unsigned int *)lock = 0;
  44. OPENSSL_free(lock);
  45. return;
  46. }
  47. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
  48. {
  49. if (*once != 0)
  50. return 1;
  51. init();
  52. *once = 1;
  53. return 1;
  54. }
  55. #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
  56. static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
  57. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  58. {
  59. static unsigned int thread_local_key = 0;
  60. if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  61. return 0;
  62. *key = thread_local_key++;
  63. thread_local_storage[*key] = NULL;
  64. return 1;
  65. }
  66. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  67. {
  68. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  69. return NULL;
  70. return thread_local_storage[*key];
  71. }
  72. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  73. {
  74. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  75. return 0;
  76. thread_local_storage[*key] = val;
  77. return 1;
  78. }
  79. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  80. {
  81. *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
  82. return 1;
  83. }
  84. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  85. {
  86. return 0;
  87. }
  88. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  89. {
  90. return (a == b);
  91. }
  92. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  93. {
  94. *val += amount;
  95. *ret = *val;
  96. return 1;
  97. }
  98. int openssl_init_fork_handlers(void)
  99. {
  100. return 0;
  101. }
  102. #endif