2008-03-07 03:28:32 +03:00
|
|
|
#ifndef _SPL_ATOMIC_H
|
|
|
|
#define _SPL_ATOMIC_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
2008-03-28 21:21:09 +03: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 03:28:32 +03:00
|
|
|
*/
|
2008-03-28 21:21:09 +03:00
|
|
|
extern spinlock_t atomic64_lock;
|
|
|
|
extern spinlock_t atomic32_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 22:25:20 +03:00
|
|
|
|
2008-03-07 03:28:32 +03:00
|
|
|
static __inline__ void
|
|
|
|
atomic_inc_64(volatile uint64_t *target)
|
|
|
|
{
|
2008-03-28 21:21:09 +03:00
|
|
|
spin_lock(&atomic64_lock);
|
2008-03-07 03:28:32 +03:00
|
|
|
(*target)++;
|
2008-03-28 21:21:09 +03:00
|
|
|
spin_unlock(&atomic64_lock);
|
2008-03-07 03:28:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void
|
|
|
|
atomic_dec_64(volatile uint64_t *target)
|
|
|
|
{
|
2008-03-28 21:21:09 +03:00
|
|
|
spin_lock(&atomic64_lock);
|
2008-03-07 03:28:32 +03:00
|
|
|
(*target)--;
|
2008-03-28 21:21:09 +03:00
|
|
|
spin_unlock(&atomic64_lock);
|
2008-03-07 03:28:32 +03:00
|
|
|
}
|
|
|
|
|
2008-03-28 21:21:09 +03:00
|
|
|
static __inline__ uint64_t
|
|
|
|
atomic_add_64(volatile uint64_t *target, uint64_t delta)
|
2008-03-10 22:25:20 +03:00
|
|
|
{
|
2008-03-28 21:21:09 +03:00
|
|
|
uint64_t rc;
|
|
|
|
|
|
|
|
spin_lock(&atomic64_lock);
|
|
|
|
rc = *target;
|
2008-03-10 22:25:20 +03:00
|
|
|
*target += delta;
|
2008-03-28 21:21:09 +03:00
|
|
|
spin_unlock(&atomic64_lock);
|
|
|
|
|
2008-03-10 22:25:20 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-03-07 03:28:32 +03:00
|
|
|
static __inline__ uint64_t
|
2008-03-28 21:21:09 +03:00
|
|
|
atomic_sub_64(volatile uint64_t *target, uint64_t delta)
|
2008-03-07 03:28:32 +03:00
|
|
|
{
|
2008-03-28 21:21:09 +03:00
|
|
|
uint64_t rc;
|
|
|
|
|
|
|
|
spin_lock(&atomic64_lock);
|
|
|
|
rc = *target;
|
|
|
|
*target -= delta;
|
|
|
|
spin_unlock(&atomic64_lock);
|
|
|
|
|
2008-03-07 03:28:32 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint64_t
|
|
|
|
atomic_add_64_nv(volatile uint64_t *target, uint64_t delta)
|
|
|
|
{
|
2008-03-28 21:21:09 +03:00
|
|
|
spin_lock(&atomic64_lock);
|
2008-03-07 03:28:32 +03:00
|
|
|
*target += delta;
|
2008-03-28 21:21:09 +03: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 03:28:32 +03:00
|
|
|
return *target;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint64_t
|
2008-04-04 01:48:57 +04:00
|
|
|
atomic_cas_64(volatile uint64_t *target, uint64_t cmp,
|
2008-03-07 03:28:32 +03:00
|
|
|
uint64_t newval)
|
|
|
|
{
|
2008-03-28 21:21:09 +03:00
|
|
|
uint64_t rc;
|
2008-03-07 03:28:32 +03:00
|
|
|
|
2008-03-28 21:21:09 +03:00
|
|
|
spin_lock(&atomic64_lock);
|
|
|
|
rc = *target;
|
2008-03-07 03:28:32 +03:00
|
|
|
if (*target == cmp)
|
|
|
|
*target = newval;
|
2008-03-28 21:21:09 +03:00
|
|
|
spin_unlock(&atomic64_lock);
|
2008-03-07 03:28:32 +03:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-04-04 01:48:57 +04:00
|
|
|
#if defined(__x86_64__)
|
|
|
|
/* XXX: Implement atomic_cas_ptr() in terms of uint64'ts. This
|
|
|
|
* is of course only safe and correct for 64 bit arches... but
|
|
|
|
* for now I'm OK with that.
|
|
|
|
*/
|
2008-03-07 03:28:32 +03:00
|
|
|
static __inline__ void *
|
2008-04-04 01:48:57 +04:00
|
|
|
atomic_cas_ptr(volatile void *target, void *cmp, void *newval)
|
2008-03-07 03:28:32 +03:00
|
|
|
{
|
2008-04-04 01:48:57 +04:00
|
|
|
return (void *)atomic_cas_64((volatile uint64_t *)target,
|
|
|
|
(uint64_t)cmp, (uint64_t)newval);
|
2008-03-07 03:28:32 +03:00
|
|
|
}
|
2008-04-04 01:48:57 +04:00
|
|
|
#endif
|
2008-03-07 03:28:32 +03:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* _SPL_ATOMIC_H */
|
|
|
|
|