Files
OrangePi_CM5/arch/parisc/include/asm/spinlock.h
T

198 lines
4.7 KiB
C
Raw Normal View History

2005-04-16 15:20:36 -07:00
#ifndef __ASM_SPINLOCK_H
#define __ASM_SPINLOCK_H
#include <asm/barrier.h>
#include <asm/ldcw.h>
2005-09-10 00:25:56 -07:00
#include <asm/processor.h>
#include <asm/spinlock_types.h>
2005-04-16 15:20:36 -07:00
static inline int arch_spin_is_locked(arch_spinlock_t *x)
2005-04-16 15:20:36 -07:00
{
volatile unsigned int *a = __ldcw_align(x);
return *a == 0;
}
#define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0)
#define arch_spin_unlock_wait(x) \
do { cpu_relax(); } while (arch_spin_is_locked(x))
2005-04-16 15:20:36 -07:00
static inline void arch_spin_lock_flags(arch_spinlock_t *x,
2005-11-17 16:35:09 -05:00
unsigned long flags)
2005-04-16 15:20:36 -07:00
{
volatile unsigned int *a;
mb();
a = __ldcw_align(x);
while (__ldcw(a) == 0)
2005-11-17 16:35:09 -05:00
while (*a == 0)
if (flags & PSW_SM_I) {
local_irq_enable();
cpu_relax();
local_irq_disable();
} else
cpu_relax();
2005-04-16 15:20:36 -07:00
mb();
}
static inline void arch_spin_unlock(arch_spinlock_t *x)
2005-04-16 15:20:36 -07:00
{
volatile unsigned int *a;
mb();
a = __ldcw_align(x);
*a = 1;
mb();
}
static inline int arch_spin_trylock(arch_spinlock_t *x)
2005-04-16 15:20:36 -07:00
{
volatile unsigned int *a;
int ret;
mb();
a = __ldcw_align(x);
ret = __ldcw(a) != 0;
mb();
return ret;
}
/*
2006-09-02 07:54:58 -06:00
* Read-write spinlocks, allowing multiple readers but only one writer.
2006-09-08 05:43:44 -06:00
* Linux rwlocks are unfair to writers; they can be starved for an indefinite
* time by readers. With care, they can also be taken in interrupt context.
*
* In the PA-RISC implementation, we have a spinlock and a counter.
* Readers use the lock to serialise their access to the counter (which
* records how many readers currently hold the lock).
* Writers hold the spinlock, preventing any readers or other writers from
* grabbing the rwlock.
2005-04-16 15:20:36 -07:00
*/
2006-09-08 05:43:44 -06:00
/* Note that we have to ensure interrupts are disabled in case we're
* interrupted by some other code that wants to grab the same read lock */
static __inline__ void arch_read_lock(arch_rwlock_t *rw)
2005-04-16 15:20:36 -07:00
{
2006-09-02 07:54:58 -06:00
unsigned long flags;
local_irq_save(flags);
arch_spin_lock_flags(&rw->lock, flags);
2005-04-16 15:20:36 -07:00
rw->counter++;
arch_spin_unlock(&rw->lock);
2006-09-02 07:54:58 -06:00
local_irq_restore(flags);
2005-04-16 15:20:36 -07:00
}
2006-09-08 05:43:44 -06:00
/* Note that we have to ensure interrupts are disabled in case we're
* interrupted by some other code that wants to grab the same read lock */
static __inline__ void arch_read_unlock(arch_rwlock_t *rw)
2005-04-16 15:20:36 -07:00
{
2006-09-02 07:54:58 -06:00
unsigned long flags;
local_irq_save(flags);
arch_spin_lock_flags(&rw->lock, flags);
2005-04-16 15:20:36 -07:00
rw->counter--;
arch_spin_unlock(&rw->lock);
2006-09-02 07:54:58 -06:00
local_irq_restore(flags);
2005-04-16 15:20:36 -07:00
}
2006-09-08 05:43:44 -06:00
/* Note that we have to ensure interrupts are disabled in case we're
* interrupted by some other code that wants to grab the same read lock */
static __inline__ int arch_read_trylock(arch_rwlock_t *rw)
2006-09-02 07:54:58 -06:00
{
unsigned long flags;
retry:
local_irq_save(flags);
if (arch_spin_trylock(&rw->lock)) {
2006-09-02 07:54:58 -06:00
rw->counter++;
arch_spin_unlock(&rw->lock);
2006-09-02 07:54:58 -06:00
local_irq_restore(flags);
return 1;
}
local_irq_restore(flags);
/* If write-locked, we fail to acquire the lock */
if (rw->counter < 0)
return 0;
/* Wait until we have a realistic chance at the lock */
while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0)
2006-09-02 07:54:58 -06:00
cpu_relax();
goto retry;
}
2005-04-16 15:20:36 -07:00
2006-09-08 05:43:44 -06:00
/* Note that we have to ensure interrupts are disabled in case we're
* interrupted by some other code that wants to read_trylock() this lock */
static __inline__ void arch_write_lock(arch_rwlock_t *rw)
2005-04-16 15:20:36 -07:00
{
2006-09-02 07:54:58 -06:00
unsigned long flags;
2005-04-16 15:20:36 -07:00
retry:
2006-09-02 07:54:58 -06:00
local_irq_save(flags);
arch_spin_lock_flags(&rw->lock, flags);
2005-04-16 15:20:36 -07:00
2006-09-02 07:54:58 -06:00
if (rw->counter != 0) {
arch_spin_unlock(&rw->lock);
2006-09-02 07:54:58 -06:00
local_irq_restore(flags);
2005-04-16 15:20:36 -07:00
2005-09-10 00:25:56 -07:00
while (rw->counter != 0)
cpu_relax();
2005-04-16 15:20:36 -07:00
goto retry;
}
2006-09-02 07:54:58 -06:00
rw->counter = -1; /* mark as write-locked */
mb();
local_irq_restore(flags);
2005-04-16 15:20:36 -07:00
}
static __inline__ void arch_write_unlock(arch_rwlock_t *rw)
2005-04-16 15:20:36 -07:00
{
rw->counter = 0;
arch_spin_unlock(&rw->lock);
2005-04-16 15:20:36 -07:00
}
2006-09-08 05:43:44 -06:00
/* Note that we have to ensure interrupts are disabled in case we're
* interrupted by some other code that wants to read_trylock() this lock */
static __inline__ int arch_write_trylock(arch_rwlock_t *rw)
2005-04-16 15:20:36 -07:00
{
2006-09-02 07:54:58 -06:00
unsigned long flags;
int result = 0;
local_irq_save(flags);
if (arch_spin_trylock(&rw->lock)) {
2006-09-02 07:54:58 -06:00
if (rw->counter == 0) {
rw->counter = -1;
result = 1;
} else {
/* Read-locked. Oh well. */
arch_spin_unlock(&rw->lock);
2006-09-02 07:54:58 -06:00
}
2005-04-16 15:20:36 -07:00
}
2006-09-02 07:54:58 -06:00
local_irq_restore(flags);
2005-04-16 15:20:36 -07:00
2006-09-02 07:54:58 -06:00
return result;
2005-04-16 15:20:36 -07:00
}
2006-03-24 21:22:02 -07:00
/*
* read_can_lock - would read_trylock() succeed?
* @lock: the rwlock in question.
*/
static __inline__ int arch_read_can_lock(arch_rwlock_t *rw)
2005-04-16 15:20:36 -07:00
{
2006-03-24 21:22:02 -07:00
return rw->counter >= 0;
2005-04-16 15:20:36 -07:00
}
2006-03-24 21:22:02 -07:00
/*
* write_can_lock - would write_trylock() succeed?
* @lock: the rwlock in question.
*/
static __inline__ int arch_write_can_lock(arch_rwlock_t *rw)
2005-04-16 15:20:36 -07:00
{
2006-03-24 21:22:02 -07:00
return !rw->counter;
2005-04-16 15:20:36 -07:00
}
#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
2009-04-02 16:59:46 -07:00
#define arch_spin_relax(lock) cpu_relax()
#define arch_read_relax(lock) cpu_relax()
#define arch_write_relax(lock) cpu_relax()
2005-04-16 15:20:36 -07:00
#endif /* __ASM_SPINLOCK_H */