2008-03-07 00:28:32 +00:00
|
|
|
#ifndef _SPL_ATOMIC_H
|
|
|
|
#define _SPL_ATOMIC_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
2008-03-28 18:21:09 +00:00
|
|
|
#include <linux/spinlock.h>
|
|
|
|
|
|
|
|
/* XXX: Serialize everything through global locks. This is
|
|
|
|
* going to be bad for performance, but for now it's the easiest
|
|
|
|
* way to ensure correct behavior. I don't like it at all.
|
|
|
|
* It would be nicer to make these function to the atomic linux
|
|
|
|
* functions, but the normal uint64_t type complicates this.
|
2008-03-07 00:28:32 +00:00
|
|
|
*/
|
2008-03-28 18:21:09 +00:00
|
|
|
extern spinlock_t atomic64_lock;
|
|
|
|
extern spinlock_t atomic32_lock;
|
|
|
|
extern spinlock_t atomic_lock;
|
|
|
|
|
|
|
|
static __inline__ uint32_t
|
|
|
|
atomic_add_32(volatile uint32_t *target, int32_t delta)
|
|
|
|
{
|
|
|
|
uint32_t rc;
|
|
|
|
|
|
|
|
spin_lock(&atomic32_lock);
|
|
|
|
rc = *target;
|
|
|
|
*target += delta;
|
|
|
|
spin_unlock(&atomic32_lock);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2008-03-10 19:25:20 +00:00
|
|
|
|
2008-03-07 00:28:32 +00:00
|
|
|
static __inline__ void
|
|
|
|
atomic_inc_64(volatile uint64_t *target)
|
|
|
|
{
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_lock(&atomic64_lock);
|
2008-03-07 00:28:32 +00:00
|
|
|
(*target)++;
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_unlock(&atomic64_lock);
|
2008-03-07 00:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void
|
|
|
|
atomic_dec_64(volatile uint64_t *target)
|
|
|
|
{
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_lock(&atomic64_lock);
|
2008-03-07 00:28:32 +00:00
|
|
|
(*target)--;
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_unlock(&atomic64_lock);
|
2008-03-07 00:28:32 +00:00
|
|
|
}
|
|
|
|
|
2008-03-28 18:21:09 +00:00
|
|
|
static __inline__ uint64_t
|
|
|
|
atomic_add_64(volatile uint64_t *target, uint64_t delta)
|
2008-03-10 19:25:20 +00:00
|
|
|
{
|
2008-03-28 18:21:09 +00:00
|
|
|
uint64_t rc;
|
|
|
|
|
|
|
|
spin_lock(&atomic64_lock);
|
|
|
|
rc = *target;
|
2008-03-10 19:25:20 +00:00
|
|
|
*target += delta;
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_unlock(&atomic64_lock);
|
|
|
|
|
2008-03-10 19:25:20 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-03-07 00:28:32 +00:00
|
|
|
static __inline__ uint64_t
|
2008-03-28 18:21:09 +00:00
|
|
|
atomic_sub_64(volatile uint64_t *target, uint64_t delta)
|
2008-03-07 00:28:32 +00:00
|
|
|
{
|
2008-03-28 18:21:09 +00:00
|
|
|
uint64_t rc;
|
|
|
|
|
|
|
|
spin_lock(&atomic64_lock);
|
|
|
|
rc = *target;
|
|
|
|
*target -= delta;
|
|
|
|
spin_unlock(&atomic64_lock);
|
|
|
|
|
2008-03-07 00:28:32 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint64_t
|
|
|
|
atomic_add_64_nv(volatile uint64_t *target, uint64_t delta)
|
|
|
|
{
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_lock(&atomic64_lock);
|
2008-03-07 00:28:32 +00:00
|
|
|
*target += delta;
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_unlock(&atomic64_lock);
|
|
|
|
|
|
|
|
return *target;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint64_t
|
|
|
|
atomic_sub_64_nv(volatile uint64_t *target, uint64_t delta)
|
|
|
|
{
|
|
|
|
spin_lock(&atomic64_lock);
|
|
|
|
*target -= delta;
|
|
|
|
spin_unlock(&atomic64_lock);
|
|
|
|
|
2008-03-07 00:28:32 +00:00
|
|
|
return *target;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint64_t
|
|
|
|
atomic_cas_64(volatile uint64_t *target, uint64_t cmp,
|
|
|
|
uint64_t newval)
|
|
|
|
{
|
2008-03-28 18:21:09 +00:00
|
|
|
uint64_t rc;
|
2008-03-07 00:28:32 +00:00
|
|
|
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_lock(&atomic64_lock);
|
|
|
|
rc = *target;
|
2008-03-07 00:28:32 +00:00
|
|
|
if (*target == cmp)
|
|
|
|
*target = newval;
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_unlock(&atomic64_lock);
|
2008-03-07 00:28:32 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void *
|
|
|
|
atomic_cas_ptr(volatile void *target, void *cmp, void *newval)
|
|
|
|
{
|
2008-03-28 18:21:09 +00:00
|
|
|
void *rc;
|
2008-03-07 00:28:32 +00:00
|
|
|
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_lock(&atomic_lock);
|
|
|
|
rc = (void *)target;
|
2008-03-07 00:28:32 +00:00
|
|
|
if (target == cmp)
|
|
|
|
target = newval;
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_unlock(&atomic_lock);
|
2008-03-07 00:28:32 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* _SPL_ATOMIC_H */
|
|
|
|
|