A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.5 KiB

  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) && !defined(OPENSSL_SYS_WINDOWS)
  11. CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
  12. {
  13. CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t));
  14. if (lock == NULL)
  15. return NULL;
  16. if (pthread_rwlock_init(lock, NULL) != 0) {
  17. OPENSSL_free(lock);
  18. return NULL;
  19. }
  20. return lock;
  21. }
  22. int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
  23. {
  24. if (pthread_rwlock_rdlock(lock) != 0)
  25. return 0;
  26. return 1;
  27. }
  28. int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
  29. {
  30. if (pthread_rwlock_wrlock(lock) != 0)
  31. return 0;
  32. return 1;
  33. }
  34. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
  35. {
  36. if (pthread_rwlock_unlock(lock) != 0)
  37. return 0;
  38. return 1;
  39. }
  40. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
  41. {
  42. if (lock == NULL)
  43. return;
  44. pthread_rwlock_destroy(lock);
  45. OPENSSL_free(lock);
  46. return;
  47. }
  48. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
  49. {
  50. if (pthread_once(once, init) != 0)
  51. return 0;
  52. return 1;
  53. }
  54. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  55. {
  56. if (pthread_key_create(key, cleanup) != 0)
  57. return 0;
  58. return 1;
  59. }
  60. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  61. {
  62. return pthread_getspecific(*key);
  63. }
  64. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  65. {
  66. if (pthread_setspecific(*key, val) != 0)
  67. return 0;
  68. return 1;
  69. }
  70. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  71. {
  72. if (pthread_key_delete(*key) != 0)
  73. return 0;
  74. return 1;
  75. }
  76. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  77. {
  78. return pthread_self();
  79. }
  80. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  81. {
  82. return pthread_equal(a, b);
  83. }
  84. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  85. {
  86. #ifdef __ATOMIC_RELAXED
  87. *ret = __atomic_add_fetch(val, amount, __ATOMIC_RELAXED);
  88. #else
  89. if (!CRYPTO_THREAD_write_lock(lock))
  90. return 0;
  91. *val += amount;
  92. *ret = *val;
  93. if (!CRYPTO_THREAD_unlock(lock))
  94. return 0;
  95. #endif
  96. return 1;
  97. }
  98. #endif