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.

ottery-threading.h 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Libottery by Nick Mathewson.
  2. This software has been dedicated to the public domain under the CC0
  3. public domain dedication.
  4. To the extent possible under law, the person who associated CC0 with
  5. libottery has waived all copyright and related or neighboring rights
  6. to libottery.
  7. You should have received a copy of the CC0 legalcode along with this
  8. work in doc/cc0.txt. If not, see
  9. <http://creativecommons.org/publicdomain/zero/1.0/>.
  10. */
  11. #ifndef OTTERY_LOCKING_H_HEADER_INCLUDED_
  12. #define OTTERY_LOCKING_H_HEADER_INCLUDED_
  13. /* We don't need locks when building rspamd */
  14. #ifdef BUILD_RSPAMD
  15. #define OTTERY_NO_LOCKS
  16. #endif
  17. /* Locks */
  18. #ifdef OTTERY_NO_LOCKS
  19. /* Nothing here. */
  20. #elif defined(__APPLE__) && !defined(OTTERY_NO_SPINLOCKS)
  21. #define OTTERY_OSATOMIC_LOCKS
  22. #include <libkern/OSAtomic.h>
  23. #elif defined(_WIN32)
  24. #define OTTERY_CRITICAL_SECTION
  25. #include <windows.h>
  26. #elif defined(HAVE_PTHREAD)
  27. #define OTTERY_PTHREADS
  28. #include <pthread.h>
  29. #else
  30. #define OTTERY_NO_LOCKS
  31. #endif
  32. #ifdef OTTERY_NO_LOCKS
  33. #define DECL_LOCK(mutex)
  34. #elif defined(OTTERY_OSATOMIC_LOCKS)
  35. #define DECL_LOCK(mutex) OSSpinLock mutex;
  36. #elif defined(OTTERY_CRITICAL_SECTION)
  37. #define DECL_LOCK(mutex) CRITICAL_SECTION mutex;
  38. #elif defined(OTTERY_PTHREADS)
  39. #define DECL_LOCK(mutex) pthread_mutex_t mutex;
  40. #endif
  41. #if defined(OTTERY_PTHREADS)
  42. #define INIT_LOCK(mutex) \
  43. (pthread_mutex_init((mutex), NULL) != 0)
  44. /** Acquire the lock for the state "st". */
  45. #define ACQUIRE_LOCK(mutex) do { \
  46. pthread_mutex_lock(mutex); \
  47. } while (0)
  48. /** Release the lock for the state "st". */
  49. #define RELEASE_LOCK(mutex) do { \
  50. pthread_mutex_unlock(mutex); \
  51. } while (0)
  52. #define DESTROY_LOCK(mutex) do { \
  53. pthread_mutex_destroy(mutex); \
  54. } while (0)
  55. #elif defined(OTTERY_CRITICAL_SECTION)
  56. #define INIT_LOCK(mutex) \
  57. (InitializeCriticalSectionAndSpinCount((mutex), 3000) == 0)
  58. #define ACQUIRE_LOCK(mutex) do { \
  59. EnterCriticalSection(mutex); \
  60. } while (0)
  61. #define RELEASE_LOCK(mutex) do { \
  62. LeaveCriticalSection(mutex); \
  63. } while (0)
  64. #define DESTROY_LOCK(mutex) do { \
  65. DeleteCriticalSection(mutex); \
  66. } while (0)
  67. #elif defined(OTTERY_OSATOMIC_LOCKS)
  68. #define INIT_LOCK(mutex) \
  69. ((*(mutex) = 0), 0)
  70. #define ACQUIRE_LOCK(mutex) do { \
  71. OSSpinLockLock(mutex); \
  72. } while (0)
  73. #define RELEASE_LOCK(mutex) do { \
  74. OSSpinLockUnlock(mutex); \
  75. } while (0)
  76. #define DESTROY_LOCK(mutex) ((void)0)
  77. #elif defined(OTTERY_NO_LOCKS)
  78. #define INIT_LOCK(mutex) (0)
  79. #define DESTROY_LOCK(mutex) ((void)0)
  80. #define ACQUIRE_LOCK(mutex) ((void)0)
  81. #define RELEASE_LOCK(mutex) ((void)0)
  82. #else
  83. #error How do I lock?
  84. #endif
  85. #endif