1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #include <asm/system.h>
5 #include <asm/processor.h>
6 #include <asm/spinlock_types.h>
8 static inline int __raw_spin_is_locked(raw_spinlock_t
*x
)
10 volatile unsigned int *a
= __ldcw_align(x
);
14 #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
15 #define __raw_spin_unlock_wait(x) \
16 do { cpu_relax(); } while (__raw_spin_is_locked(x))
18 static inline void __raw_spin_lock(raw_spinlock_t
*x
)
20 volatile unsigned int *a
;
24 while (__ldcw(a
) == 0)
29 static inline void __raw_spin_unlock(raw_spinlock_t
*x
)
31 volatile unsigned int *a
;
38 static inline int __raw_spin_trylock(raw_spinlock_t
*x
)
40 volatile unsigned int *a
;
52 * Read-write spinlocks, allowing multiple readers
53 * but only one writer.
56 #define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
58 /* read_lock, read_unlock are pretty straightforward. Of course it somehow
59 * sucks we end up saving/restoring flags twice for read_lock_irqsave aso. */
61 static __inline__
void __raw_read_lock(raw_rwlock_t
*rw
)
64 local_irq_save(flags
);
65 __raw_spin_lock(&rw
->lock
);
69 __raw_spin_unlock(&rw
->lock
);
70 local_irq_restore(flags
);
73 static __inline__
void __raw_read_unlock(raw_rwlock_t
*rw
)
76 local_irq_save(flags
);
77 __raw_spin_lock(&rw
->lock
);
81 __raw_spin_unlock(&rw
->lock
);
82 local_irq_restore(flags
);
85 /* write_lock is less trivial. We optimistically grab the lock and check
86 * if we surprised any readers. If so we release the lock and wait till
87 * they're all gone before trying again
89 * Also note that we don't use the _irqsave / _irqrestore suffixes here.
90 * If we're called with interrupts enabled and we've got readers (or other
91 * writers) in interrupt handlers someone fucked up and we'd dead-lock
92 * sooner or later anyway. prumpf */
94 static __inline__
void __raw_write_lock(raw_rwlock_t
*rw
)
97 __raw_spin_lock(&rw
->lock
);
99 if(rw
->counter
!= 0) {
100 /* this basically never happens */
101 __raw_spin_unlock(&rw
->lock
);
103 while (rw
->counter
!= 0)
109 /* got it. now leave without unlocking */
110 rw
->counter
= -1; /* remember we are locked */
113 /* write_unlock is absolutely trivial - we don't have to wait for anything */
115 static __inline__
void __raw_write_unlock(raw_rwlock_t
*rw
)
118 __raw_spin_unlock(&rw
->lock
);
121 static __inline__
int __raw_write_trylock(raw_rwlock_t
*rw
)
123 __raw_spin_lock(&rw
->lock
);
124 if (rw
->counter
!= 0) {
125 /* this basically never happens */
126 __raw_spin_unlock(&rw
->lock
);
131 /* got it. now leave without unlocking */
132 rw
->counter
= -1; /* remember we are locked */
136 static __inline__
int __raw_is_read_locked(raw_rwlock_t
*rw
)
138 return rw
->counter
> 0;
141 static __inline__
int __raw_is_write_locked(raw_rwlock_t
*rw
)
143 return rw
->counter
< 0;
146 #endif /* __ASM_SPINLOCK_H */