[ovs-dev] [urcu 2/8] ovs-atomic: Add support for atomic pointer types.

Ben Pfaff blp at nicira.com
Thu Mar 6 07:12:57 UTC 2014


The upcoming RCU library will use these.

Signed-off-by: Ben Pfaff <blp at nicira.com>
---
 lib/ovs-atomic-clang.h    |  4 +++-
 lib/ovs-atomic-gcc4+.h    |  2 ++
 lib/ovs-atomic-gcc4.7+.h  |  4 +++-
 lib/ovs-atomic-pthreads.h |  1 +
 lib/ovs-atomic.h          | 23 +++++++++++++++++++++++
 5 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/lib/ovs-atomic-clang.h b/lib/ovs-atomic-clang.h
index 7449428..60552ae 100644
--- a/lib/ovs-atomic-clang.h
+++ b/lib/ovs-atomic-clang.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Nicira, Inc.
+ * Copyright (c) 2013, 2014 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -60,6 +60,8 @@ typedef _Atomic(int16_t)   atomic_int16_t;
 typedef _Atomic(int32_t)   atomic_int32_t;
 typedef _Atomic(int64_t)   atomic_int64_t;
 
+#define ATOMIC_POINTER_TYPE(TYPE) _Atomic(TYPE)
+
 #define ATOMIC_VAR_INIT(VALUE) (VALUE)
 
 #define atomic_init(OBJECT, VALUE) __c11_atomic_init(OBJECT, VALUE)
diff --git a/lib/ovs-atomic-gcc4+.h b/lib/ovs-atomic-gcc4+.h
index 8e8b8e0..5be606b 100644
--- a/lib/ovs-atomic-gcc4+.h
+++ b/lib/ovs-atomic-gcc4+.h
@@ -102,6 +102,8 @@ typedef LOCKLESS_ATOMIC(int32_t) atomic_int32_t;
     typedef struct locked_int64  atomic_int64_t;
 #endif
 
+#define ATOMIC_POINTER_TYPE(TYPE) struct { TYPE value; }
+
 typedef enum {
     memory_order_relaxed,
     memory_order_consume,
diff --git a/lib/ovs-atomic-gcc4.7+.h b/lib/ovs-atomic-gcc4.7+.h
index 56d265f..b11a546 100644
--- a/lib/ovs-atomic-gcc4.7+.h
+++ b/lib/ovs-atomic-gcc4.7+.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Nicira, Inc.
+ * Copyright (c) 2013, 2014 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -60,6 +60,8 @@ typedef uint32_t           atomic_uint32_t;
 typedef int64_t            atomic_int64_t;
 typedef uint64_t           atomic_uint64_t;
 
+#define ATOMIC_POINTER_TYPE(TYPE) TYPE
+
 typedef enum {
     memory_order_relaxed = __ATOMIC_RELAXED,
     memory_order_consume = __ATOMIC_CONSUME,
diff --git a/lib/ovs-atomic-pthreads.h b/lib/ovs-atomic-pthreads.h
index b0ca524..89eed45 100644
--- a/lib/ovs-atomic-pthreads.h
+++ b/lib/ovs-atomic-pthreads.h
@@ -66,6 +66,7 @@ typedef PTHREAD_ATOMIC_TYPE(int16_t) atomic_int16_t;
 typedef PTHREAD_ATOMIC_TYPE(int32_t) atomic_int32_t;
 typedef PTHREAD_ATOMIC_TYPE(uint64_t) atomic_uint64_t;
 typedef PTHREAD_ATOMIC_TYPE(int64_t) atomic_int64_t;
+#define ATOMIC_POINTER_TYPE(TYPE) struct { TYPE value; pthread_mutex_t mutex; }
 
 typedef enum {
     memory_order_relaxed,
diff --git a/lib/ovs-atomic.h b/lib/ovs-atomic.h
index 4d43a42..6110388 100644
--- a/lib/ovs-atomic.h
+++ b/lib/ovs-atomic.h
@@ -74,6 +74,29 @@
  *
  *     (*) Not specified by C11.
  *
+ * An additional category of atomic pointer types is available as
+ * ATOMIC_POINTER_TYPE(TYPE).  For example, ATOMIC_POINTER_TYPE(void *) is an
+ * atomic pointer to void (atomic void *).  The types generated by different
+ * invocations of ATOMIC_POINTER_TYPE aren't necessarily compatible, so use
+ * "typedef" where that is important.  For example, the following might
+ * generate a compiler error because the two uses of ATOMIC_POINTER_TYPE don't
+ * create compatible types:
+ *
+ *     void f(ATOMIC_POINTER_TYPE(void *) *);
+ *     void g(void) {
+ *         ATOMIC_POINTER_TYPE(void *) p;
+ *         f(&p);
+ *     }
+ *
+ * but correct code can be written as:
+ *
+ *     typedef ATOMIC_POINTER_TYPE(void *) atomic_voidp;
+ *     void f(atomic_voidp *);
+ *     void g(void) {
+ *         atomic_voidp p;
+ *         f(&p);
+ *     }
+ *
  * The atomic version of a type doesn't necessarily have the same size or
  * representation as the ordinary version; for example, atomic_int might be a
  * typedef for a struct that also includes a mutex.  The range of an atomic
-- 
1.8.5.3




More information about the dev mailing list