diff options
Diffstat (limited to 'atomic/impl')
-rw-r--r-- | atomic/impl/simpleops-atomic-impl-ao.h | 117 | ||||
-rw-r--r-- | atomic/impl/simpleops-atomic-impl-doc.h | 244 | ||||
-rw-r--r-- | atomic/impl/simpleops-atomic-impl-macosx.h | 142 | ||||
-rw-r--r-- | atomic/impl/simpleops-atomic-impl-msvc.h | 144 | ||||
-rw-r--r-- | atomic/impl/simpleops-atomic-impl-mutex.h | 171 | ||||
-rw-r--r-- | atomic/impl/simpleops-atomic-impl-native.h | 147 | ||||
-rw-r--r-- | atomic/impl/simpleops-atomic-impl-no.h | 147 | ||||
-rw-r--r-- | atomic/impl/simpleops-atomic-impl-template.h | 103 | ||||
-rw-r--r-- | atomic/impl/simpleops-atomic-impl.h | 50 |
9 files changed, 1265 insertions, 0 deletions
diff --git a/atomic/impl/simpleops-atomic-impl-ao.h b/atomic/impl/simpleops-atomic-impl-ao.h new file mode 100644 index 0000000..d1d2732 --- /dev/null +++ b/atomic/impl/simpleops-atomic-impl-ao.h @@ -0,0 +1,117 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2007 Chris Wilson + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + * Chris Wilson <chris@chris-wilson.co.uk> + */ + +#ifndef SIMPLEOPS_ATOMIC_IMPL_AO_H +#define SIMPLEOPS_ATOMIC_IMPL_AO_H + +#ifndef SIMPLEOPS_ATOMIC_IMPL_H +#error Private header used directly +#endif + +#if ! SIMPLEOPS_HAVE_ATOMIC_IMPL && \ + SIMPLEOPS_HAVE_ATOMIC_OPS_H + +#define SIMPLEOPS_HAVE_ATOMIC_IMPL 1 +#define SIMPLEOPS_HAVE_ATOMIC_IMPL_AO 1 + +#include <atomic_ops.h> + +SIMPLEOPS_COMPILE_TIME_ASSERT(sizeof (void *) == sizeof (AO_t)); + +/* Atomic integer functions */ +typedef AO_t simpleops_atomic_int_t; + + +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_int_compare_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t oldv, + simpleops_atomic_int_t newv) +{ + return AO_compare_and_swap_full (x, oldv, newv); +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + return _InterlockedExchange (x, newv); +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_load (simpleops_atomic_int_t const volatile *x) +{ + return AO_load_full (x); +} + +static simpleops_always_inline void +simpleops_atomic_int_store (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + AO_store_full (x, newv); +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_fetch_add (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t y) +{ + return AO_fetch_and_add_full (x, y); +} + +/* Atomic pointer functions */ +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_ptr_compare_exchange (void * volatile *x, + void * const oldv, + void * const newv) +{ + return AO_compare_and_swap_full ((AO_t *) x, (AO_t) oldv, (AO_t) newv); +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_exchange (void * volatile *x, + void * const newv) +{ + return _InterlockedExchangePointer (x, newv); +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_load (void * const volatile *x) +{ + return (void *) AO_load_full ((AO_t * const) x); +} + +static simpleops_always_inline void +simpleops_atomic_ptr_store (void * volatile *x, + void * const newv) +{ + AO_store_full ((AO_t *) x, (AO_t) newv) +} + +#endif + +#endif diff --git a/atomic/impl/simpleops-atomic-impl-doc.h b/atomic/impl/simpleops-atomic-impl-doc.h new file mode 100644 index 0000000..7a98948 --- /dev/null +++ b/atomic/impl/simpleops-atomic-impl-doc.h @@ -0,0 +1,244 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2007 Chris Wilson + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + * Chris Wilson <chris@chris-wilson.co.uk> + */ + +#ifndef SIMPLEOPS_ATOMIC_IMPL_DOC_H +#define SIMPLEOPS_ATOMIC_IMPL_DOC_H + +#ifndef SIMPLEOPS_ATOMIC_IMPL_H +#error Private header used directly +#endif + +#if ! SIMPLEOPS_HAVE_ATOMIC_IMPL && \ + SIMPLEOPS_DOC + +/** + * Defined whenever the simpleops library is providing an + * implementation of atomic operations. + * + * Using atomic operations it is possible to avoid race conditions + * when performing concurrent access to shared data without the need + * for mutexes, which are typically much more expensive. + */ +#define SIMPLEOPS_HAVE_ATOMIC_IMPL 1 + +/** + * SIMPLEOPS_HAVE_ATOMIC_IMPL_* identifies which implementation is + * being used in the simpleops library to provide atomic operations. + */ +#define SIMPLEOPS_HAVE_ATOMIC_IMPL_DOC 1 + +/* Atomic integer functions */ +/** + * The type on which atomic aritmetic operations can be performed. + * + * It is guaranteed to be able to represent at least all the int + * values. + */ +typedef int simpleops_atomic_int_t; + +/** + * Perform an atomic compare and swap on a #simpleops_atomic_int_t. + * + * If the value pointed by x is oldv, replace it with newv. + * + * \param[in,out] x the destination. + * \param[in] oldv the old value to be found at x. + * \param[in] newv the new value to be set at x. + * + * \return whether the exchange happened. + */ +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_int_compare_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t oldv, + simpleops_atomic_int_t newv) +{ + simpleops_bool_t r; + + r = *x == oldv; + if (r) + *x = newv; + + return r; +} + +/** + * Perform an atomic swap on a #simpleops_atomic_int_t. + * + * Set the value pointed by x to newv. + * + * \param[in,out] x the destination. + * \param[in] newv the new value to be set at x. + * + * \return the old value at x. + */ +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + simpleops_atomic_int_t r; + + r = *x; + *x = newv; + + return r; +} + +/** + * Perform an atomic load on a #simpleops_atomic_int_t. + * + * \param[in] x a pointer to the #simpleops_atomic_int_t to be read. + * + * \return the value pointed by x. + */ +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_load (simpleops_atomic_int_t const volatile *x) +{ + simpleops_atomic_int_t r; + + r = *x; + + return r; +} + +/** + * Perform an atomic store on a #simpleops_atomic_int_t. + * + * \param[out] x the destination. + * \param[in] newv the value to be set at x. + */ +static simpleops_always_inline void +simpleops_atomic_int_store (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + *x = newv; +} + +/** + * Perform an atomic addition. + * + * Add y to the value pointed by x. + * + * \param[in,out] x the destination. + * \param[in] y the value to be added to the value found at x. + * + * \return the old value at x. + */ +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_fetch_add (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t y) +{ + simpleops_atomic_int_t r; + + r = *x; + *x += y; + + return r; +} + +/* Atomic pointer functions */ +/** + * Perform an atomic compare and swap on a pointer. + * + * If the value pointed by x is oldv, replace it with newv. + * + * \param[in,out] x the destination. + * \param[in] oldv the old value to be found at x. + * \param[in] newv the new value to be set at x. + * + * \return whether the exchange happened. + */ +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_ptr_compare_exchange (void * volatile *x, + void * const oldv, + void * const newv) +{ + simpleops_bool_t r; + + r = *x == oldv; + if (r) + *x = newv; + + return r; +} + +/** + * Perform an atomic swap on a pointer. + * + * Set the value pointed by x to newv. + * + * \param[in,out] x the destination. + * \param[in] newv the new value to be set at x. + * + * \return the old value at x. + */ +static simpleops_always_inline void * +simpleops_atomic_ptr_exchange (void * volatile *x, + void * const newv) +{ + void *r; + + r = *x; + *x = newv; + + return r; +} + +/** + * Perform an atomic load on a pointer. + * + * \param[in] x a pointer to the pointer to be read. + * + * \return the value pointed by x. + */ +static simpleops_always_inline void * +simpleops_atomic_ptr_load (void * const volatile *x) +{ + void *r; + + r = *x; + + return r; +} + +/** + * Perform an atomic store on a pointer. + * + * \param[out] x the destination. + * \param[in] newv the value to be set at x. + */ +static simpleops_always_inline void +simpleops_atomic_ptr_store (void * volatile *x, + void * const newv) +{ + *x = newv; +} + +#endif + +#endif diff --git a/atomic/impl/simpleops-atomic-impl-macosx.h b/atomic/impl/simpleops-atomic-impl-macosx.h new file mode 100644 index 0000000..4848405 --- /dev/null +++ b/atomic/impl/simpleops-atomic-impl-macosx.h @@ -0,0 +1,142 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2007 Chris Wilson + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + * Chris Wilson <chris@chris-wilson.co.uk> + */ + +#ifndef SIMPLEOPS_ATOMIC_IMPL_MACOSX_H +#define SIMPLEOPS_ATOMIC_IMPL_MACOSX_H + +#ifndef SIMPLEOPS_ATOMIC_IMPL_H +#error Private header used directly +#endif + +#if ! SIMPLEOPS_HAVE_ATOMIC_IMPL && \ + SIMPLEOPS_HAVE_LIBKERN_OSATOMIC_H + +#define SIMPLEOPS_HAVE_ATOMIC_IMPL 1 +#define SIMPLEOPS_HAVE_ATOMIC_IMPL_MACOSX 1 + +#include <libkern/OSAtomic.h> + +static simpleops_always_inline void +_simpleops_atomic_barrier (void) +{ +#if SIMPLEOPS_ATOMIC_IMPL_NEEDS_MEMORY_BARRIER + OSMemoryBarrier (); +#endif +} + +/* Atomic integer functions */ +typedef int32_t simpleops_atomic_int_t; + +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_int_compare_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t oldv, + simpleops_atomic_int_t newv) +{ + return OSAtomicCompareAndSwap32Barrier (oldv, newv, x); +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + simpleops_atomic_int_t r; + + _simpleops_atomic_barrier (); + + do { + r = *x; + } while (! simpleops_atomic_int_compare_exchange (x, r, newv)); + + return r; +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_load (simpleops_atomic_int_t const volatile *x) +{ + _simpleops_atomic_barrier (); + return *x; +} + +static simpleops_always_inline void +simpleops_atomic_int_store (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + _simpleops_atomic_barrier (); + *x = newv; +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_fetch_add (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t y) +{ + return OSAtomicAdd32Barrier (y, x) - y; +} + +/* Atomic pointer functions */ +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_ptr_compare_exchange (void * volatile *x, + void * const oldv, + void * const newv) +{ + return OSAtomicCompareAndSwapPtrBarrier ((void *) oldv, (void *) newv, x); +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_exchange (void * volatile *x, + void * const newv) +{ + void *r; + + _simpleops_atomic_barrier (); + + do { + r = *x; + } while (! simpleops_atomic_ptr_compare_exchange (x, r, newv)); + + return r; +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_load (void * const volatile *x) +{ + _simpleops_atomic_barrier (); + return *x; +} + +static simpleops_always_inline void +simpleops_atomic_ptr_store (void * volatile *x, + void * const newv) +{ + _simpleops_atomic_barrier (); + *x = newv; +} + +#endif + +#endif diff --git a/atomic/impl/simpleops-atomic-impl-msvc.h b/atomic/impl/simpleops-atomic-impl-msvc.h new file mode 100644 index 0000000..c584bbe --- /dev/null +++ b/atomic/impl/simpleops-atomic-impl-msvc.h @@ -0,0 +1,144 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2007 Chris Wilson + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + * Chris Wilson <chris@chris-wilson.co.uk> + */ + +#ifndef SIMPLEOPS_ATOMIC_IMPL_MSVC_H +#define SIMPLEOPS_ATOMIC_IMPL_MSVC_H + +#ifndef SIMPLEOPS_ATOMIC_IMPL_H +#error Private header used directly +#endif + +#if ! SIMPLEOPS_HAVE_ATOMIC_IMPL && \ + defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) + +#define SIMPLEOPS_HAVE_ATOMIC_IMPL 1 +#define SIMPLEOPS_HAVE_ATOMIC_IMPL_WIN32 1 + +#include <intrin.h> + +#pragma intrinsic (_InterlockedCompareExchange) +#pragma intrinsic (_InterlockedExchange) +#pragma intrinsic (_InterlockedExchangeAdd) +#pragma intrinsic (_ReadWriteBarrier) + +#ifdef _M_X64 +#pragma intrinsic (_InterlockedCompareExchangePointer) +#pragma intrinsic (_InterlockedExchangePointer) +#endif + +static simpleops_always_inline void +_simpleops_atomic_barrier (void) +{ +#if SIMPLEOPS_ATOMIC_IMPL_NEEDS_MEMORY_BARRIER + _ReadWriteBarrier (); +#endif +} + +/* Atomic integer functions */ +typedef long simpleops_atomic_int_t; + +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_int_compare_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t oldv, + simpleops_atomic_int_t newv) +{ + return _InterlockedCompareExchange (x, newv, oldv) == oldv; +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + return _InterlockedExchange (x, newv); +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_load (simpleops_atomic_int_t const volatile *x) +{ + _simpleops_atomic_barrier (); + return *x; +} + +static simpleops_always_inline void +simpleops_atomic_int_store (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + _simpleops_atomic_barrier (); + *x = newv; +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_fetch_add (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t y) +{ + return _InterlockedExchangeAdd (x, y); +} + +/* Atomic pointer functions */ +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_ptr_compare_exchange (void * volatile *x, + void * const oldv, + void * const newv) +{ +#ifdef _M_X64 + return _InterlockedCompareExchangePointer (x, newv, oldv) == oldv; +#else + return _InterlockedCompareExchangePointer (x, (int) newv, (int) oldv) == (int) oldv; +#endif +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_exchange (void * volatile *x, + void * const newv) +{ +#ifdef _M_X64 + return _InterlockedExchangePointer (x, newv); +#else + return (void *) _InterlockedExchangePointer (x, (int) newv); +#endif +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_load (void * const volatile *x) +{ + _simpleops_atomic_barrier (); + return *x; +} + +static simpleops_always_inline void +simpleops_atomic_ptr_store (void * volatile *x, + void * const newv) +{ + _simpleops_atomic_barrier (); + *x = newv; +} + +#endif + +#endif diff --git a/atomic/impl/simpleops-atomic-impl-mutex.h b/atomic/impl/simpleops-atomic-impl-mutex.h new file mode 100644 index 0000000..9c76704 --- /dev/null +++ b/atomic/impl/simpleops-atomic-impl-mutex.h @@ -0,0 +1,171 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2007 Chris Wilson + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + * Chris Wilson <chris@chris-wilson.co.uk> + */ + +#ifndef SIMPLEOPS_ATOMIC_IMPL_MUTEX_H +#define SIMPLEOPS_ATOMIC_IMPL_MUTEX_H + +#ifndef SIMPLEOPS_ATOMIC_IMPL_H +#error Private header used directly +#endif + +#if ! SIMPLEOPS_HAVE_ATOMIC_IMPL && \ + 1 /* TODO: Do we want to warn about slow "atomics"? */ + +#define SIMPLEOPS_HAVE_ATOMIC_IMPL 1 +#define SIMPLEOPS_HAVE_ATOMIC_IMPL_MUTEX 1 + +#include "simpleops/mutex/simpleops-mutex.h" + +#define _SIMPLEOPS_ATOMIC_MUTEX SIMPLEOPS_ADD_PREFIX (_simpleops_atomic_mutex) + +extern simpleops_mutex_t _SIMPLEOPS_ATOMIC_MUTEX; + +/* Atomic integer functions */ +typedef int simpleops_atomic_int_t; + +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_int_compare_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t oldv, + simpleops_atomic_int_t newv) +{ + simpleops_bool_t r; + + simpleops_mutex_lock (&_SIMPLEOPS_ATOMIC_MUTEX); + r = *x == oldv; + if (r) + *x = newv; + simpleops_mutex_unlock (&_SIMPLEOPS_ATOMIC_MUTEX); + + return r; +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + simpleops_atomic_int_t r; + + simpleops_mutex_lock (&_SIMPLEOPS_ATOMIC_MUTEX); + r = *x; + *x = newv; + simpleops_mutex_unlock (&_SIMPLEOPS_ATOMIC_MUTEX); + + return r; +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_load (simpleops_atomic_int_t const volatile *x) +{ + simpleops_atomic_int_t r; + + simpleops_mutex_lock (&_SIMPLEOPS_ATOMIC_MUTEX); + r = *x; + simpleops_mutex_unlock (&_SIMPLEOPS_ATOMIC_MUTEX); + + return r; +} + +static simpleops_always_inline void +simpleops_atomic_int_store (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + simpleops_mutex_lock (&_SIMPLEOPS_ATOMIC_MUTEX); + *x = newv; + simpleops_mutex_unlock (&_SIMPLEOPS_ATOMIC_MUTEX); +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_fetch_add (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t y) +{ + simpleops_atomic_int_t r; + + simpleops_mutex_lock (&_SIMPLEOPS_ATOMIC_MUTEX); + r = *x; + *x += y; + simpleops_mutex_unlock (&_SIMPLEOPS_ATOMIC_MUTEX); + + return r; +} + +/* Atomic pointer functions */ +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_ptr_compare_exchange (void * volatile *x, + void * const oldv, + void * const newv) +{ + simpleops_bool_t r; + + simpleops_mutex_lock (&_SIMPLEOPS_ATOMIC_MUTEX); + r = *x == oldv; + if (r) + *x = newv; + simpleops_mutex_unlock (&_SIMPLEOPS_ATOMIC_MUTEX); + + return r; +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_exchange (void * volatile *x, + void * const newv) +{ + void *r; + + simpleops_mutex_lock (&_SIMPLEOPS_ATOMIC_MUTEX); + r = *x; + *x = newv; + simpleops_mutex_unlock (&_SIMPLEOPS_ATOMIC_MUTEX); + + return r; +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_load (void * const volatile *x) +{ + void *r; + + simpleops_mutex_lock (&_SIMPLEOPS_ATOMIC_MUTEX); + r = *x; + simpleops_mutex_unlock (&_SIMPLEOPS_ATOMIC_MUTEX); + + return r; +} + +static simpleops_always_inline void +simpleops_atomic_ptr_store (void * volatile *x, + void * const newv) +{ + simpleops_mutex_lock (&_SIMPLEOPS_ATOMIC_MUTEX); + *x = newv; + simpleops_mutex_unlock (&_SIMPLEOPS_ATOMIC_MUTEX); +} + +#endif + +#endif diff --git a/atomic/impl/simpleops-atomic-impl-native.h b/atomic/impl/simpleops-atomic-impl-native.h new file mode 100644 index 0000000..4c58e72 --- /dev/null +++ b/atomic/impl/simpleops-atomic-impl-native.h @@ -0,0 +1,147 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2007 Chris Wilson + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + * Chris Wilson <chris@chris-wilson.co.uk> + */ + +#ifndef SIMPLEOPS_ATOMIC_IMPL_NATIVE_H +#define SIMPLEOPS_ATOMIC_IMPL_NATIVE_H + +#ifndef SIMPLEOPS_ATOMIC_IMPL_H +#error Private header used directly +#endif + +#if ! SIMPLEOPS_HAVE_ATOMIC_IMPL && \ + SIMPLEOPS_HAVE_NATIVE_ATOMIC_PRIMITIVES + +#define SIMPLEOPS_HAVE_ATOMIC_IMPL 1 +#define SIMPLEOPS_HAVE_ATOMIC_IMPL_NATIVE 1 + +static simpleops_always_inline void _simpleops_atomic_barrier (void) +{ +#if SIMPLEOPS_ATOMIC_IMPL_NEEDS_MEMORY_BARRIER + __sync_synchronize (); +#endif +} + +/* Atomic integer functions */ +typedef int simpleops_atomic_int_t; + +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_int_compare_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t oldv, + simpleops_atomic_int_t newv) +{ + return __sync_bool_compare_and_swap (x, oldv, newv); +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ +#if SIMPLEOPS_HAVE_NATIVE_ATOMIC_SWAP + return __sync_swap (x, newv); +#else + simpleops_atomic_int_t r; + + _simpleops_atomic_barrier (); + + do { + r = *x; + } while (! simpleops_atomic_int_compare_exchange (x, r, newv)); + + return r; +#endif +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_load (simpleops_atomic_int_t const volatile *x) +{ + _simpleops_atomic_barrier (); + return *x; +} + +static simpleops_always_inline void +simpleops_atomic_int_store (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + _simpleops_atomic_barrier (); + *x = newv; +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_fetch_add (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t y) +{ + return __sync_fetch_and_add (x, y); +} + +/* Atomic pointer functions */ +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_ptr_compare_exchange (void * volatile *x, + void * const oldv, + void * const newv) +{ + return __sync_bool_compare_and_swap (x, oldv, newv); +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_exchange (void * volatile *x, + void * const newv) +{ +#if SIMPLEOPS_HAVE_SYNC_SWAP + return __sync_swap (x, newv); +#else + void *r; + + _simpleops_atomic_barrier (); + + do { + r = *x; + } while (! simpleops_atomic_ptr_compare_exchange (x, r, newv)); + + return r; +#endif +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_load (void * const volatile *x) +{ + _simpleops_atomic_barrier (); + return *x; +} + +static simpleops_always_inline void +simpleops_atomic_ptr_store (void * volatile *x, + void * const newv) +{ + _simpleops_atomic_barrier (); + *x = newv; +} + +#endif + +#endif diff --git a/atomic/impl/simpleops-atomic-impl-no.h b/atomic/impl/simpleops-atomic-impl-no.h new file mode 100644 index 0000000..84e2ad9 --- /dev/null +++ b/atomic/impl/simpleops-atomic-impl-no.h @@ -0,0 +1,147 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2007 Chris Wilson + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + * Chris Wilson <chris@chris-wilson.co.uk> + */ + +#ifndef SIMPLEOPS_ATOMIC_IMPL_NO_H +#define SIMPLEOPS_ATOMIC_IMPL_NO_H + +#ifndef SIMPLEOPS_ATOMIC_IMPL_H +#error Private header used directly +#endif + +#if ! SIMPLEOPS_HAVE_ATOMIC_IMPL && \ + defined(SIMPLEOPS_DISABLE_ATOMIC) + +#define SIMPLEOPS_HAVE_ATOMIC_IMPL 1 +#define SIMPLEOPS_HAVE_ATOMIC_IMPL_NO 1 + +/* Atomic integer functions */ +typedef int simpleops_atomic_int_t; + +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_int_compare_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t oldv, + simpleops_atomic_int_t newv) +{ + simpleops_bool_t r; + + r = *x == oldv; + if (r) + *x = newv; + + return r; +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + simpleops_atomic_int_t r; + + r = *x; + *x = newv; + + return r; +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_load (simpleops_atomic_int_t const volatile *x) +{ + simpleops_atomic_int_t r; + + r = *x; + + return r; +} + +static simpleops_always_inline void +simpleops_atomic_int_store (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ + *x = newv; +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_fetch_add (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t y) +{ + simpleops_atomic_int_t r; + + r = *x; + *x += y; + + return r; +} + +/* Atomic pointer functions */ +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_ptr_compare_exchange (void * volatile *x, + void * const oldv, + void * const newv) +{ + simpleops_bool_t r; + + r = *x == oldv; + if (r) + *x = newv; + + return r; +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_exchange (void * volatile *x, + void * const newv) +{ + void *r; + + r = *x; + *x = newv; + + return r; +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_load (void * const volatile *x) +{ + void *r; + + r = *x; + + return r; +} + +static simpleops_always_inline void +simpleops_atomic_ptr_store (void * volatile *x, + void * const newv) +{ + *x = newv; +} + +#endif + +#endif diff --git a/atomic/impl/simpleops-atomic-impl-template.h b/atomic/impl/simpleops-atomic-impl-template.h new file mode 100644 index 0000000..9cb9ce8 --- /dev/null +++ b/atomic/impl/simpleops-atomic-impl-template.h @@ -0,0 +1,103 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2007 Chris Wilson + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + * Chris Wilson <chris@chris-wilson.co.uk> + */ + +#ifndef SIMPLEOPS_ATOMIC_IMPL_TEMPLATE_H +#define SIMPLEOPS_ATOMIC_IMPL_TEMPLATE_H + +#ifndef SIMPLEOPS_ATOMIC_IMPL_H +#error Private header used directly +#endif + +#if ! SIMPLEOPS_HAVE_ATOMIC_IMPL && \ + 0 + +#define SIMPLEOPS_HAVE_ATOMIC_IMPL 1 +#define SIMPLEOPS_HAVE_ATOMIC_IMPL_TEMPLATE 1 + +/* Atomic integer functions */ +typedef int simpleops_atomic_int_t; + +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_int_compare_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t oldv, + simpleops_atomic_int_t newv) +{ +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_exchange (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_load (simpleops_atomic_int_t const volatile *x) +{ +} + +static simpleops_always_inline void +simpleops_atomic_int_store (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t newv) +{ +} + +static simpleops_always_inline simpleops_atomic_int_t +simpleops_atomic_int_fetch_add (simpleops_atomic_int_t volatile *x, + simpleops_atomic_int_t y) +{ +} + +/* Atomic pointer functions */ +static simpleops_always_inline simpleops_bool_t +simpleops_atomic_ptr_compare_exchange (void * volatile *x, + void * const oldv, + void * const newv) +{ +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_exchange (void * volatile *x, + void * const newv) +{ +} + +static simpleops_always_inline void * +simpleops_atomic_ptr_load (void * const volatile *x) +{ +} + +static simpleops_always_inline void +simpleops_atomic_ptr_store (void * volatile *x, + void * const newv) +{ +} + +#endif + +#endif diff --git a/atomic/impl/simpleops-atomic-impl.h b/atomic/impl/simpleops-atomic-impl.h new file mode 100644 index 0000000..01358ed --- /dev/null +++ b/atomic/impl/simpleops-atomic-impl.h @@ -0,0 +1,50 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + */ + +#ifndef SIMPLEOPS_ATOMIC_IMPL_H +#define SIMPLEOPS_ATOMIC_IMPL_H + +#ifndef SIMPLEOPS_ATOMIC_H +#error Private header used directly +#endif + +/* DIFFTABLE */ +#include "simpleops/atomic/impl/simpleops-atomic-impl-template.h" +#include "simpleops/atomic/impl/simpleops-atomic-impl-doc.h" +#include "simpleops/atomic/impl/simpleops-atomic-impl-no.h" +#include "simpleops/atomic/impl/simpleops-atomic-impl-native.h" +#include "simpleops/atomic/impl/simpleops-atomic-impl-msvc.h" +#include "simpleops/atomic/impl/simpleops-atomic-impl-macosx.h" +#include "simpleops/atomic/impl/simpleops-atomic-impl-ao.h" +#include "simpleops/atomic/impl/simpleops-atomic-impl-mutex.h" +/* DIFFTABLE */ + +#if ! SIMPLEOPS_HAVE_ATOMIC_IMPL +#error No implementation available +#endif + +#endif |