2010-05-17 22:18:00 +00:00
|
|
|
/*****************************************************************************\
|
|
|
|
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
|
|
|
* Copyright (C) 2007 The Regents of the University of California.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
|
|
|
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
2008-05-26 04:38:26 +00:00
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
2013-03-05 01:26:55 +00:00
|
|
|
* For details, see <http://zfsonlinux.org/>.
|
2010-05-17 22:18:00 +00:00
|
|
|
*
|
|
|
|
* The SPL is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
2008-05-26 04:38:26 +00:00
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
2008-05-26 04:38:26 +00:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2010-05-17 22:18:00 +00:00
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
\*****************************************************************************/
|
2008-05-26 04:38:26 +00:00
|
|
|
|
2008-03-07 00:28:32 +00:00
|
|
|
#ifndef _SPL_ATOMIC_H
|
|
|
|
#define _SPL_ATOMIC_H
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
2008-03-28 18:21:09 +00:00
|
|
|
#include <linux/spinlock.h>
|
2009-10-30 17:55:25 +00:00
|
|
|
#include <sys/types.h>
|
2009-12-04 23:54:12 +00:00
|
|
|
|
2009-10-30 17:55:25 +00:00
|
|
|
/*
|
|
|
|
* Two approaches to atomic operations are implemented each with its
|
|
|
|
* own benefits are drawbacks imposed by the Solaris API. Neither
|
|
|
|
* approach handles the issue of word breaking when using a 64-bit
|
|
|
|
* atomic variable on a 32-bit arch. The Solaris API would need to
|
|
|
|
* add an atomic read call to correctly support this.
|
|
|
|
*
|
|
|
|
* When ATOMIC_SPINLOCK is defined all atomic operations will be
|
|
|
|
* serialized through global spin locks. This is bad for performance
|
|
|
|
* but it does allow a simple generic implementation.
|
|
|
|
*
|
|
|
|
* When ATOMIC_SPINLOCK is not defined the Linux atomic operations
|
|
|
|
* are used. This is safe as long as the core Linux implementation
|
|
|
|
* doesn't change because we are relying on the fact that an atomic
|
|
|
|
* type is really just a uint32 or uint64. If this changes at some
|
|
|
|
* point in the future we need to fall-back to the spin approach.
|
2008-03-07 00:28:32 +00:00
|
|
|
*/
|
2009-10-30 17:55:25 +00:00
|
|
|
#ifdef ATOMIC_SPINLOCK
|
2008-03-28 18:21:09 +00:00
|
|
|
extern spinlock_t atomic32_lock;
|
2009-10-30 17:55:25 +00:00
|
|
|
extern spinlock_t atomic64_lock;
|
2008-03-28 18:21:09 +00:00
|
|
|
|
2009-10-30 17:55:25 +00:00
|
|
|
static __inline__ void
|
|
|
|
atomic_inc_32(volatile uint32_t *target)
|
|
|
|
{
|
|
|
|
spin_lock(&atomic32_lock);
|
|
|
|
(*target)++;
|
|
|
|
spin_unlock(&atomic32_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void
|
|
|
|
atomic_dec_32(volatile uint32_t *target)
|
|
|
|
{
|
|
|
|
spin_lock(&atomic32_lock);
|
|
|
|
(*target)--;
|
|
|
|
spin_unlock(&atomic32_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void
|
2008-03-28 18:21:09 +00:00
|
|
|
atomic_add_32(volatile uint32_t *target, int32_t delta)
|
2009-10-30 17:55:25 +00:00
|
|
|
{
|
|
|
|
spin_lock(&atomic32_lock);
|
|
|
|
*target += delta;
|
|
|
|
spin_unlock(&atomic32_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void
|
|
|
|
atomic_sub_32(volatile uint32_t *target, int32_t delta)
|
|
|
|
{
|
|
|
|
spin_lock(&atomic32_lock);
|
|
|
|
*target -= delta;
|
|
|
|
spin_unlock(&atomic32_lock);
|
|
|
|
}
|
|
|
|
|
2010-08-27 19:36:31 +00:00
|
|
|
static __inline__ uint32_t
|
|
|
|
atomic_inc_32_nv(volatile uint32_t *target)
|
|
|
|
{
|
2010-09-17 23:03:15 +00:00
|
|
|
uint32_t nv;
|
|
|
|
|
2010-08-27 19:36:31 +00:00
|
|
|
spin_lock(&atomic32_lock);
|
2010-09-17 23:03:15 +00:00
|
|
|
nv = ++(*target);
|
2010-08-27 19:36:31 +00:00
|
|
|
spin_unlock(&atomic32_lock);
|
|
|
|
|
2010-09-17 23:03:15 +00:00
|
|
|
return nv;
|
2010-08-27 19:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint32_t
|
|
|
|
atomic_dec_32_nv(volatile uint32_t *target)
|
|
|
|
{
|
2010-09-17 23:03:15 +00:00
|
|
|
uint32_t nv;
|
|
|
|
|
2010-08-27 19:36:31 +00:00
|
|
|
spin_lock(&atomic32_lock);
|
2010-09-17 23:03:15 +00:00
|
|
|
nv = --(*target);
|
2010-08-27 19:36:31 +00:00
|
|
|
spin_unlock(&atomic32_lock);
|
|
|
|
|
2010-09-17 23:03:15 +00:00
|
|
|
return nv;
|
2010-08-27 19:36:31 +00:00
|
|
|
}
|
|
|
|
|
2009-10-30 17:55:25 +00:00
|
|
|
static __inline__ uint32_t
|
|
|
|
atomic_add_32_nv(volatile uint32_t *target, uint32_t delta)
|
|
|
|
{
|
2010-09-17 23:03:15 +00:00
|
|
|
uint32_t nv;
|
|
|
|
|
2009-10-30 17:55:25 +00:00
|
|
|
spin_lock(&atomic32_lock);
|
|
|
|
*target += delta;
|
2010-09-17 23:03:15 +00:00
|
|
|
nv = *target;
|
2009-10-30 17:55:25 +00:00
|
|
|
spin_unlock(&atomic32_lock);
|
|
|
|
|
2010-09-17 23:03:15 +00:00
|
|
|
return nv;
|
2009-10-30 17:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint32_t
|
|
|
|
atomic_sub_32_nv(volatile uint32_t *target, uint32_t delta)
|
|
|
|
{
|
2010-09-17 23:03:15 +00:00
|
|
|
uint32_t nv;
|
|
|
|
|
2009-10-30 17:55:25 +00:00
|
|
|
spin_lock(&atomic32_lock);
|
|
|
|
*target -= delta;
|
2010-09-17 23:03:15 +00:00
|
|
|
nv = *target;
|
2009-10-30 17:55:25 +00:00
|
|
|
spin_unlock(&atomic32_lock);
|
|
|
|
|
2010-09-17 23:03:15 +00:00
|
|
|
return nv;
|
2009-10-30 17:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint32_t
|
|
|
|
atomic_cas_32(volatile uint32_t *target, uint32_t cmp,
|
|
|
|
uint32_t newval)
|
2008-03-28 18:21:09 +00:00
|
|
|
{
|
|
|
|
uint32_t rc;
|
|
|
|
|
|
|
|
spin_lock(&atomic32_lock);
|
|
|
|
rc = *target;
|
2009-10-30 17:55:25 +00:00
|
|
|
if (*target == cmp)
|
|
|
|
*target = newval;
|
|
|
|
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_unlock(&atomic32_lock);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2008-03-10 19:25:20 +00:00
|
|
|
|
2014-07-26 04:45:26 +00:00
|
|
|
static __inline__ uint32_t
|
|
|
|
atomic_swap_32(volatile uint32_t *target, uint32_t newval)
|
|
|
|
{
|
|
|
|
uint32_t rc;
|
|
|
|
|
|
|
|
spin_lock(&atomic32_lock);
|
|
|
|
rc = *target;
|
|
|
|
*target = newval;
|
|
|
|
spin_unlock(&atomic32_lock);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-10-30 17:55:25 +00:00
|
|
|
static __inline__ void
|
2008-03-28 18:21:09 +00:00
|
|
|
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
|
|
|
spin_lock(&atomic64_lock);
|
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
|
|
|
}
|
|
|
|
|
2009-10-30 17:55:25 +00:00
|
|
|
static __inline__ void
|
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
|
|
|
spin_lock(&atomic64_lock);
|
|
|
|
*target -= delta;
|
|
|
|
spin_unlock(&atomic64_lock);
|
2008-03-07 00:28:32 +00:00
|
|
|
}
|
|
|
|
|
2010-08-27 19:36:31 +00:00
|
|
|
static __inline__ uint64_t
|
|
|
|
atomic_inc_64_nv(volatile uint64_t *target)
|
|
|
|
{
|
2010-09-17 23:03:15 +00:00
|
|
|
uint64_t nv;
|
|
|
|
|
2010-08-27 19:36:31 +00:00
|
|
|
spin_lock(&atomic64_lock);
|
2010-09-17 23:03:15 +00:00
|
|
|
nv = ++(*target);
|
2010-08-27 19:36:31 +00:00
|
|
|
spin_unlock(&atomic64_lock);
|
|
|
|
|
2010-09-17 23:03:15 +00:00
|
|
|
return nv;
|
2010-08-27 19:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint64_t
|
|
|
|
atomic_dec_64_nv(volatile uint64_t *target)
|
|
|
|
{
|
2010-09-17 23:03:15 +00:00
|
|
|
uint64_t nv;
|
|
|
|
|
2010-08-27 19:36:31 +00:00
|
|
|
spin_lock(&atomic64_lock);
|
2010-09-17 23:03:15 +00:00
|
|
|
nv = --(*target);
|
2010-08-27 19:36:31 +00:00
|
|
|
spin_unlock(&atomic64_lock);
|
|
|
|
|
2010-09-17 23:03:15 +00:00
|
|
|
return nv;
|
2010-08-27 19:36:31 +00:00
|
|
|
}
|
|
|
|
|
2008-03-07 00:28:32 +00:00
|
|
|
static __inline__ uint64_t
|
|
|
|
atomic_add_64_nv(volatile uint64_t *target, uint64_t delta)
|
|
|
|
{
|
2010-09-17 23:03:15 +00:00
|
|
|
uint64_t nv;
|
|
|
|
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_lock(&atomic64_lock);
|
2008-03-07 00:28:32 +00:00
|
|
|
*target += delta;
|
2010-09-17 23:03:15 +00:00
|
|
|
nv = *target;
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_unlock(&atomic64_lock);
|
|
|
|
|
2010-09-17 23:03:15 +00:00
|
|
|
return nv;
|
2008-03-28 18:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint64_t
|
|
|
|
atomic_sub_64_nv(volatile uint64_t *target, uint64_t delta)
|
|
|
|
{
|
2010-09-17 23:03:15 +00:00
|
|
|
uint64_t nv;
|
|
|
|
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_lock(&atomic64_lock);
|
|
|
|
*target -= delta;
|
2010-09-17 23:03:15 +00:00
|
|
|
nv = *target;
|
2008-03-28 18:21:09 +00:00
|
|
|
spin_unlock(&atomic64_lock);
|
|
|
|
|
2010-09-17 23:03:15 +00:00
|
|
|
return nv;
|
2008-03-07 00:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ uint64_t
|
2008-04-03 21:48:57 +00:00
|
|
|
atomic_cas_64(volatile uint64_t *target, uint64_t cmp,
|
2009-10-30 17:55:25 +00:00
|
|
|
uint64_t newval)
|
2008-03-07 00:28:32 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-26 04:45:26 +00:00
|
|
|
static __inline__ uint64_t
|
|
|
|
atomic_swap_64(volatile uint64_t *target, uint64_t newval)
|
|
|
|
{
|
|
|
|
uint64_t rc;
|
|
|
|
|
|
|
|
spin_lock(&atomic64_lock);
|
|
|
|
rc = *target;
|
|
|
|
*target = newval;
|
|
|
|
spin_unlock(&atomic64_lock);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2008-08-11 22:42:04 +00:00
|
|
|
|
2009-10-30 17:55:25 +00:00
|
|
|
#else /* ATOMIC_SPINLOCK */
|
2008-08-11 22:42:04 +00:00
|
|
|
|
2009-10-30 17:55:25 +00:00
|
|
|
#define atomic_inc_32(v) atomic_inc((atomic_t *)(v))
|
|
|
|
#define atomic_dec_32(v) atomic_dec((atomic_t *)(v))
|
|
|
|
#define atomic_add_32(v, i) atomic_add((i), (atomic_t *)(v))
|
|
|
|
#define atomic_sub_32(v, i) atomic_sub((i), (atomic_t *)(v))
|
2010-08-27 19:36:31 +00:00
|
|
|
#define atomic_inc_32_nv(v) atomic_inc_return((atomic_t *)(v))
|
|
|
|
#define atomic_dec_32_nv(v) atomic_dec_return((atomic_t *)(v))
|
2009-10-30 17:55:25 +00:00
|
|
|
#define atomic_add_32_nv(v, i) atomic_add_return((i), (atomic_t *)(v))
|
|
|
|
#define atomic_sub_32_nv(v, i) atomic_sub_return((i), (atomic_t *)(v))
|
|
|
|
#define atomic_cas_32(v, x, y) atomic_cmpxchg((atomic_t *)(v), x, y)
|
2014-07-26 04:45:26 +00:00
|
|
|
#define atomic_swap_32(v, x) atomic_xchg((atomic_t *)(v), x)
|
2009-10-30 17:55:25 +00:00
|
|
|
#define atomic_inc_64(v) atomic64_inc((atomic64_t *)(v))
|
|
|
|
#define atomic_dec_64(v) atomic64_dec((atomic64_t *)(v))
|
|
|
|
#define atomic_add_64(v, i) atomic64_add((i), (atomic64_t *)(v))
|
|
|
|
#define atomic_sub_64(v, i) atomic64_sub((i), (atomic64_t *)(v))
|
2010-08-27 19:36:31 +00:00
|
|
|
#define atomic_inc_64_nv(v) atomic64_inc_return((atomic64_t *)(v))
|
|
|
|
#define atomic_dec_64_nv(v) atomic64_dec_return((atomic64_t *)(v))
|
2009-10-30 17:55:25 +00:00
|
|
|
#define atomic_add_64_nv(v, i) atomic64_add_return((i), (atomic64_t *)(v))
|
|
|
|
#define atomic_sub_64_nv(v, i) atomic64_sub_return((i), (atomic64_t *)(v))
|
|
|
|
#define atomic_cas_64(v, x, y) atomic64_cmpxchg((atomic64_t *)(v), x, y)
|
2014-07-26 04:45:26 +00:00
|
|
|
#define atomic_swap_64(v, x) atomic64_xchg((atomic64_t *)(v), x)
|
2008-08-11 22:42:04 +00:00
|
|
|
|
2009-10-30 17:55:25 +00:00
|
|
|
#endif /* ATOMIC_SPINLOCK */
|
2008-08-11 22:42:04 +00:00
|
|
|
|
|
|
|
#ifdef _LP64
|
2008-03-07 00:28:32 +00:00
|
|
|
static __inline__ void *
|
2008-04-03 21:48:57 +00:00
|
|
|
atomic_cas_ptr(volatile void *target, void *cmp, void *newval)
|
2008-03-07 00:28:32 +00:00
|
|
|
{
|
2008-04-03 21:48:57 +00:00
|
|
|
return (void *)atomic_cas_64((volatile uint64_t *)target,
|
|
|
|
(uint64_t)cmp, (uint64_t)newval);
|
2008-03-07 00:28:32 +00:00
|
|
|
}
|
2009-10-30 17:55:25 +00:00
|
|
|
#else /* _LP64 */
|
2008-08-11 22:42:04 +00:00
|
|
|
static __inline__ void *
|
|
|
|
atomic_cas_ptr(volatile void *target, void *cmp, void *newval)
|
|
|
|
{
|
|
|
|
return (void *)atomic_cas_32((volatile uint32_t *)target,
|
|
|
|
(uint32_t)cmp, (uint32_t)newval);
|
|
|
|
}
|
2009-10-30 17:55:25 +00:00
|
|
|
#endif /* _LP64 */
|
2008-03-07 00:28:32 +00:00
|
|
|
|
|
|
|
#endif /* _SPL_ATOMIC_H */
|