Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. #include <atomic.h>
  3.  
  4. #ifdef USE_SMP
  5.  
  6. typedef struct
  7. {
  8.         atomic_t val;
  9. } spinlock_t;
  10.  
  11. /*
  12.  * SPINLOCK_DECLARE is to be used for dynamically allocated spinlocks,
  13.  * where the lock gets initialized in run time.
  14.  */
  15.  
  16. #define SPINLOCK_DECLARE(slname)  spinlock_t slname
  17.  
  18. /*
  19.  * SPINLOCK_INITIALIZE is to be used for statically allocated spinlocks.
  20.  * It declares and initializes the lock.
  21.  */
  22.  
  23. #define SPINLOCK_INITIALIZE(slname)   \
  24.         spinlock_t slname = {           \
  25.                 .val = { 0 }            \
  26.         }
  27.  
  28. extern void spinlock_initialize(spinlock_t *sl);
  29. extern int spinlock_trylock(spinlock_t *sl);
  30.  
  31. #define spinlock_lock(x) atomic_lock_arch(&(x)->val)
  32.  
  33. /** Unlock spinlock
  34.  *
  35.  * Unlock spinlock.
  36.  *
  37.  * @param sl Pointer to spinlock_t structure.
  38.  */
  39. static inline void spinlock_unlock(spinlock_t *sl)
  40. {
  41.   ASSERT(atomic_get(&sl->val) != 0);
  42.  
  43.         /*
  44.          * Prevent critical section code from bleeding out this way down.
  45.          */
  46.  // CS_LEAVE_BARRIER();
  47.  
  48.         atomic_set(&sl->val, 0);
  49. //  preemption_enable();
  50. }
  51.  
  52. #else
  53.  
  54. /* On UP systems, spinlocks are effectively left out. */
  55. #define SPINLOCK_DECLARE(name)
  56. #define SPINLOCK_EXTERN(name)
  57. #define SPINLOCK_INITIALIZE(name)
  58.  
  59. #define spinlock_initialize(x)
  60. #define spinlock_lock(x)
  61. #define spinlock_trylock(x)
  62. #define spinlock_unlock(x)
  63.  
  64. #endif
  65.  
  66.