spinlock.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef __SPINLOCK_H__
  2. #define __SPINLOCK_H__
  3. #pragma once
  4. #include "config.h"
  5. #if _WIN32
  6. #include <intrin.h>
  7. #endif
  8. #include <assert.h>
  9. #include <winpr/interlocked.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. typedef volatile LONG lock_t[1];
  14. #define FASTLOCK_INIT() {0}
  15. // non-reentrance
  16. static __inline void fastlock_init(lock_t l)
  17. {
  18. l[0] = 0;
  19. }
  20. TOOLKIT_API void fastlock_enter(lock_t l);
  21. static __inline void fastlock_leave(lock_t l)
  22. {
  23. _ReadWriteBarrier();
  24. InterlockedExchange(l, 0);
  25. }
  26. static __inline void fastlock_term(lock_t l)
  27. {
  28. assert(l[0] == 0);
  29. }
  30. /* spin lock, note: reentrance!!!
  31. * 11 1111 1111
  32. */
  33. #define DEFAULT_SPIN_COUNT 0x3ff
  34. // because zero is invalid thread id, so when a thread retrieve the lock
  35. // _value is set to the thread's id.
  36. typedef struct spinlock_t {
  37. volatile DWORD _value;
  38. volatile DWORD _reentrance;
  39. }spinlock_t;
  40. static __inline void spinlock_init(spinlock_t *plock)
  41. {
  42. plock->_value = 0;
  43. plock->_reentrance = 0;
  44. }
  45. /**
  46. * enter lock
  47. * @param spin_count -1 use default
  48. */
  49. TOOLKIT_API void spinlock_enter(volatile spinlock_t *sl, int spin_count);
  50. /**
  51. * leave lock
  52. */
  53. TOOLKIT_API void spinlock_leave(volatile spinlock_t *sl);
  54. union rw_spinlock_barrier {
  55. struct {
  56. unsigned int readerCount : 32 / 2;
  57. unsigned int writerCount : 32 / 2 - 1;
  58. unsigned int writerActive : 1 ;
  59. };
  60. unsigned int nAtomic ;
  61. };
  62. typedef volatile struct rw_spinlock_s {
  63. union rw_spinlock_barrier barrier;
  64. }rw_spinlock_t;
  65. static __inline void rw_spinlock_init(rw_spinlock_t *rwlock)
  66. {
  67. rwlock->barrier.nAtomic = 0;
  68. }
  69. // John M. Mellor-Crummey, Michael L. Scott "Scalable Reader-Writer Synchronization for Shared-Memory Multiprocessors", 1991
  70. TOOLKIT_API void rw_spinlock_read_lock(rw_spinlock_t *rwlock);
  71. TOOLKIT_API void rw_spinlock_read_unlock(rw_spinlock_t *rwlock);
  72. TOOLKIT_API void rw_spinlock_write_lock(rw_spinlock_t *rwlock);
  73. TOOLKIT_API void rw_spinlock_write_unlock(rw_spinlock_t *rwlock);
  74. static __inline int rw_spinlock_is_reading(rw_spinlock_t *rwlock)
  75. {
  76. return rwlock->barrier.readerCount != 0;
  77. }
  78. static __inline int rw_spinlock_is_writing(rw_spinlock_t *rwlock)
  79. {
  80. return rwlock->barrier.writerActive == 1 ;
  81. }
  82. #ifdef __cplusplus
  83. } // extern "C" {
  84. #endif
  85. #endif //__SPINLOCK_H__