threads_none.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2016 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. #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
  11. CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
  12. {
  13. CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(unsigned int));
  14. if (lock == NULL)
  15. return NULL;
  16. *(unsigned int *)lock = 1;
  17. return lock;
  18. }
  19. int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
  20. {
  21. OPENSSL_assert(*(unsigned int *)lock == 1);
  22. return 1;
  23. }
  24. int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
  25. {
  26. OPENSSL_assert(*(unsigned int *)lock == 1);
  27. return 1;
  28. }
  29. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
  30. {
  31. OPENSSL_assert(*(unsigned int *)lock == 1);
  32. return 1;
  33. }
  34. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
  35. if (lock == NULL)
  36. return;
  37. *(unsigned int *)lock = 0;
  38. OPENSSL_free(lock);
  39. return;
  40. }
  41. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
  42. {
  43. if (*once != 0)
  44. return 1;
  45. init();
  46. *once = 1;
  47. return 1;
  48. }
  49. #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
  50. static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
  51. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  52. {
  53. static unsigned int thread_local_key = 0;
  54. if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  55. return 0;
  56. *key = thread_local_key++;
  57. thread_local_storage[*key] = NULL;
  58. return 1;
  59. }
  60. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  61. {
  62. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  63. return NULL;
  64. return thread_local_storage[*key];
  65. }
  66. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  67. {
  68. if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
  69. return 0;
  70. thread_local_storage[*key] = val;
  71. return 1;
  72. }
  73. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  74. {
  75. *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
  76. return 1;
  77. }
  78. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  79. {
  80. return 0;
  81. }
  82. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  83. {
  84. return (a == b);
  85. }
  86. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  87. {
  88. *val += amount;
  89. *ret = *val;
  90. return 1;
  91. }
  92. #endif