mirror_zfs/include/asm/atomic_compat.h
Brian Behlendorf a5d6f6020a Add missing atomic64 compat helpers for 32-bit systems.
The use of these functions was added with the recent atomic work
and not tested on 32-bit systems.  Add the missing compat functions:
atomic64_inc, atomic64_dec, atomic64_add_return, atomic64_sub_return,
atomic64_inc_return, atomic64_dec_return.
2009-12-01 10:15:27 -08:00

100 lines
1.9 KiB
C

#ifndef _SPL_ATOMIC_COMPAT_H
#define _SPL_ATOMIC_COMPAT_H
#include <asm/atomic.h>
#include <spl_config.h>
#ifndef HAVE_ATOMIC64_T
#include <linux/spinlock.h>
typedef struct {
spinlock_t lock;
__s64 val;
} atomic64_t;
#define ATOMIC64_INIT(i) { .lock = SPIN_LOCK_UNLOCKED, .val = (i) }
static inline void atomic64_add(__s64 i, atomic64_t *v)
{
unsigned long flags;
spin_lock_irqsave(&v->lock, flags);
v->val += i;
spin_unlock_irqrestore(&v->lock, flags);
}
static inline void atomic64_sub(__s64 i, atomic64_t *v)
{
unsigned long flags;
spin_lock_irqsave(&v->lock, flags);
v->val -= i;
spin_unlock_irqrestore(&v->lock, flags);
}
#define atomic64_inc(v) (atomic64_add(1, (v)))
#define atomic64_dec(v) (atomic64_sub(1, (v)))
static inline __s64 atomic64_add_return(__s64 i, atomic64_t *v)
{
unsigned long flags;
__s64 ret;
spin_lock_irqsave(&v->lock, flags);
v->val += i;
ret = v->val;
spin_unlock_irqrestore(&v->lock, flags);
return ret;
}
static inline __s64 atomic64_sub_return(__s64 i, atomic64_t *v)
{
unsigned long flags;
__s64 ret;
spin_lock_irqsave(&v->lock, flags);
v->val -= i;
ret = v->val;
spin_unlock_irqrestore(&v->lock, flags);
return ret;
}
#define atomic64_inc_return(v) (atomic64_add_return(1, (v)))
#define atomic64_dec_return(v) (atomic64_sub_return(1, (v)))
static inline __s64 atomic64_read(atomic64_t *v)
{
unsigned long flags;
__s64 r;
spin_lock_irqsave(&v->lock, flags);
r = v->val;
spin_unlock_irqrestore(&v->lock, flags);
return r;
}
static inline void atomic64_set(atomic64_t *v, __s64 i)
{
unsigned long flags;
spin_lock_irqsave(&v->lock, flags);
v->val = i;
spin_unlock_irqrestore(&v->lock, flags);
}
#endif /* HAVE_ATOMIC64_T */
#ifndef HAVE_ATOMIC64_CMPXCHG
#define atomic64_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
#endif
#ifndef HAVE_ATOMIC64_XCHG
#define atomic64_xchg(v, n) (xchg(&((v)->counter), n))
#endif
#endif /* _SPL_ATOMIC_COMPAT_H */