summaryrefslogtreecommitdiff
path: root/atomic/impl/simpleops-atomic-impl-doc.h
diff options
context:
space:
mode:
Diffstat (limited to 'atomic/impl/simpleops-atomic-impl-doc.h')
-rw-r--r--atomic/impl/simpleops-atomic-impl-doc.h244
1 files changed, 244 insertions, 0 deletions
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