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.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /* Locks */
  14. #ifdef OTTERY_NO_LOCKS
  15. /* Nothing here. */
  16. #elif defined(__APPLE__) && !defined(OTTERY_NO_SPINLOCKS)
  17. #define OTTERY_OSATOMIC_LOCKS
  18. #include <libkern/OSAtomic.h>
  19. #elif defined(_WIN32)
  20. #define OTTERY_CRITICAL_SECTION
  21. #include <windows.h>
  22. #elif defined(HAVE_PTHREAD)
  23. #define OTTERY_PTHREADS
  24. #include <pthread.h>
  25. #else
  26. #define OTTERY_NO_LOCKS
  27. #endif
  28. #ifdef OTTERY_NO_LOCKS
  29. #define DECL_LOCK(mutex)
  30. #elif defined(OTTERY_OSATOMIC_LOCKS)
  31. #define DECL_LOCK(mutex) OSSpinLock mutex;
  32. #elif defined(OTTERY_CRITICAL_SECTION)
  33. #define DECL_LOCK(mutex) CRITICAL_SECTION mutex;
  34. #elif defined(OTTERY_PTHREADS)
  35. #define DECL_LOCK(mutex) pthread_mutex_t mutex;
  36. #endif
  37. #if defined(OTTERY_PTHREADS)
  38. #define INIT_LOCK(mutex) \
  39. (pthread_mutex_init((mutex), NULL) != 0)
  40. /** Acquire the lock for the state "st". */
  41. #define ACQUIRE_LOCK(mutex) do { \
  42. pthread_mutex_lock(mutex); \
  43. } while (0)
  44. /** Release the lock for the state "st". */
  45. #define RELEASE_LOCK(mutex) do { \
  46. pthread_mutex_unlock(mutex); \
  47. } while (0)
  48. #define DESTROY_LOCK(mutex) do { \
  49. pthread_mutex_destroy(mutex); \
  50. } while (0)
  51. #elif defined(OTTERY_CRITICAL_SECTION)
  52. #define INIT_LOCK(mutex) \
  53. (InitializeCriticalSectionAndSpinCount((mutex), 3000) == 0)
  54. #define ACQUIRE_LOCK(mutex) do { \
  55. EnterCriticalSection(mutex); \
  56. } while (0)
  57. #define RELEASE_LOCK(mutex) do { \
  58. LeaveCriticalSection(mutex); \
  59. } while (0)
  60. #define DESTROY_LOCK(mutex) do { \
  61. DeleteCriticalSection(mutex); \
  62. } while (0)
  63. #elif defined(OTTERY_OSATOMIC_LOCKS)
  64. #define INIT_LOCK(mutex) \
  65. ((*(mutex) = 0), 0)
  66. #define ACQUIRE_LOCK(mutex) do { \
  67. OSSpinLockLock(mutex); \
  68. } while (0)
  69. #define RELEASE_LOCK(mutex) do { \
  70. OSSpinLockUnlock(mutex); \
  71. } while (0)
  72. #define DESTROY_LOCK(mutex) ((void)0)
  73. #elif defined(OTTERY_NO_LOCKS)
  74. #define INIT_LOCK(mutex) (0)
  75. #define DESTROY_LOCK(mutex) ((void)0)
  76. #define ACQUIRE_LOCK(mutex) ((void)0)
  77. #define RELEASE_LOCK(mutex) ((void)0)
  78. #else
  79. #error How do I lock?
  80. #endif
  81. #endif