spinlock.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Minimal x86 spinlocks for the SMP paths. */
  2. #ifndef SPINLOCK_H
  3. #define SPINLOCK_H
  4. typedef struct {
  5. volatile unsigned int locked;
  6. } spinlock_t;
  7. static inline unsigned int spin_irq_save_disable(void)
  8. {
  9. unsigned int flags;
  10. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  11. return flags;
  12. }
  13. static inline void spin_irq_restore(unsigned int flags)
  14. {
  15. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  16. }
  17. static inline void spin_pause(void)
  18. {
  19. /* REP NOP is the PAUSE encoding and is harmless on older CPUs. */
  20. __asm__ volatile ("rep; nop" ::: "memory");
  21. }
  22. static inline void spin_lock(spinlock_t *l)
  23. {
  24. while (__sync_lock_test_and_set(&l->locked, 1u)) {
  25. while (l->locked)
  26. spin_pause();
  27. }
  28. }
  29. static inline void spin_unlock(spinlock_t *l)
  30. {
  31. __asm__ volatile ("" ::: "memory");
  32. l->locked = 0;
  33. }
  34. /* Caller saves/restores the interrupt state around the critical section. */
  35. static inline void spin_lock_irqsave(spinlock_t *l, unsigned int *flags)
  36. {
  37. *flags = spin_irq_save_disable();
  38. spin_lock(l);
  39. }
  40. static inline void spin_unlock_irqrestore(spinlock_t *l, unsigned int flags)
  41. {
  42. spin_unlock(l);
  43. spin_irq_restore(flags);
  44. }
  45. #endif