]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: make atomic ops C++ compatible
authorDavid Lamparter <equinox@opensourcerouting.org>
Mon, 11 Feb 2019 10:38:57 +0000 (11:38 +0100)
committerRenato Westphal <renato@opensourcerouting.org>
Mon, 11 Feb 2019 17:49:49 +0000 (15:49 -0200)
C++ doesn't have ISO C11 stdatomic.h or "_Atomic inttype", so use
std::atomic instead to get the headers compatible.

Signed-off-by: David Lamparter <equinox@diac24.net>
lib/debug.h
lib/frr_pthread.h
lib/frratomic.h
lib/hash.h
lib/memory.h
lib/stream.h
lib/thread.c
lib/thread.h

index d0fa27d3fe74f80693e66f79be56b594e2077e1a..2b389e3d4c0c6aa5151e8071c43543ed223065ae 100644 (file)
@@ -76,7 +76,7 @@
  *    Human-readable description of this debugging record.
  */
 struct debug {
-       _Atomic uint32_t flags;
+       atomic_uint_fast32_t flags;
        const char *desc;
 };
 
index e6b3f031b3325447e09235c0e7b4316a03224989..d487c738af3b587b1fe63c9dbf1cd10cdeb3221b 100644 (file)
@@ -73,7 +73,7 @@ struct frr_pthread {
         */
        pthread_cond_t *running_cond;
        pthread_mutex_t *running_cond_mtx;
-       _Atomic bool running;
+       atomic_bool running;
 
        /*
         * Fake thread-specific storage. No constraints on usage. Helpful when
index 1f1d1b569a59cfc60163c1c8085ff7c7c6dafe77..e86030f83c7c0109860e51f15e997f67a06118af 100644 (file)
 #endif
 
 /* ISO C11 */
-#ifdef HAVE_STDATOMIC_H
+#ifdef __cplusplus
+#include <stdint.h>
+#include <atomic>
+using std::atomic_int;
+using std::memory_order;
+using std::memory_order_relaxed;
+using std::memory_order_acquire;
+using std::memory_order_release;
+using std::memory_order_acq_rel;
+using std::memory_order_consume;
+using std::memory_order_seq_cst;
+
+typedef std::atomic<bool>              atomic_bool;
+typedef std::atomic<size_t>            atomic_size_t;
+typedef std::atomic<uint_fast32_t>     atomic_uint_fast32_t;
+
+#elif defined(HAVE_STDATOMIC_H)
 #include <stdatomic.h>
 
 /* These are available in gcc, but not in stdatomic */
@@ -39,6 +55,7 @@
 #elif defined(HAVE___ATOMIC)
 
 #define _Atomic volatile
+#define _ATOMIC_WANT_TYPEDEFS
 
 #define memory_order_relaxed __ATOMIC_RELAXED
 #define memory_order_consume __ATOMIC_CONSUME
@@ -74,6 +91,7 @@
 #elif defined(HAVE___SYNC)
 
 #define _Atomic volatile
+#define _ATOMIC_WANT_TYPEDEFS
 
 #define memory_order_relaxed 0
 #define memory_order_consume 0
 #error no atomic functions...
 #endif
 
+#ifdef _ATOMIC_WANT_TYPEDEFS
+#undef _ATOMIC_WANT_TYPEDEFS
+
+#include <stdint.h>
+#include <stdbool.h>
+
+typedef _Atomic bool           atomic_bool;
+typedef _Atomic size_t         atomic_size_t;
+typedef _Atomic uint_fast32_t  atomic_uint_fast32_t;
+#endif
+
 #endif /* _FRRATOMIC_H */
index 45ae6ce60a104ec82ed178e5be08152a59f17686..aca2590a95731287a2a4cc60a3f475609616b30a 100644 (file)
@@ -54,9 +54,9 @@ struct hash_backet {
 
 struct hashstats {
        /* number of empty hash buckets */
-       _Atomic uint_fast32_t empty;
+       atomic_uint_fast32_t empty;
        /* sum of squares of bucket length */
-       _Atomic uint_fast32_t ssq;
+       atomic_uint_fast32_t ssq;
 };
 
 struct hash {
index 1a0269f8bb1fc6adce96cc75c5c21af36e3dcaad..40b9d36ee6722acec3a86724fd3a9be4c3e4f7ba 100644 (file)
 struct memtype {
        struct memtype *next, **ref;
        const char *name;
-       _Atomic size_t n_alloc;
-       _Atomic size_t n_max;
-       _Atomic size_t size;
+       atomic_size_t n_alloc;
+       atomic_size_t n_max;
+       atomic_size_t size;
 #ifdef HAVE_MALLOC_USABLE_SIZE
-       _Atomic size_t total;
-       _Atomic size_t max_size;
+       atomic_size_t total;
+       atomic_size_t max_size;
 #endif
 };
 
index 32b6fb5af1a052e7a7b6390a24448cd219d1ce0c..20c5d3d77d930644f1014e1f742831c3fa710bd6 100644 (file)
@@ -115,9 +115,9 @@ struct stream_fifo {
        pthread_mutex_t mtx;
 
        /* number of streams in this fifo */
-       _Atomic size_t count;
+       atomic_size_t count;
 #if defined DEV_BUILD
-       _Atomic size_t max_count;
+       atomic_size_t max_count;
 #endif
 
        struct stream *head;
index ae8e375a27c9405b9a49916a9c4da582ad6ecbce..8c1b3ff06d7ada941e6c8c9c145f96c8b7e9cb81 100644 (file)
@@ -93,7 +93,8 @@ static void cpu_record_hash_free(void *a)
 static void vty_out_cpu_thread_history(struct vty *vty,
                                       struct cpu_thread_history *a)
 {
-       vty_out(vty, "%5d %10lu.%03lu %9u %8lu %9lu %8lu %9lu", a->total_active,
+       vty_out(vty, "%5"PRIdFAST32" %10lu.%03lu %9"PRIuFAST32
+               " %8lu %9lu %8lu %9lu", a->total_active,
                a->cpu.total / 1000, a->cpu.total % 1000, a->total_calls,
                a->cpu.total / a->total_calls, a->cpu.max,
                a->real.total / a->total_calls, a->real.max);
index f404d92755e6b82a28671259b5f2e821c75a3385..036e39f77e5d066e5fc1d8d69af95519506c278c 100644 (file)
@@ -119,13 +119,13 @@ struct thread {
 
 struct cpu_thread_history {
        int (*func)(struct thread *);
-       _Atomic unsigned int total_calls;
-       _Atomic unsigned int total_active;
+       atomic_uint_fast32_t total_calls;
+       atomic_uint_fast32_t total_active;
        struct time_stats {
-               _Atomic unsigned long total, max;
+               atomic_size_t total, max;
        } real;
        struct time_stats cpu;
-       _Atomic uint32_t types;
+       atomic_uint_fast32_t types;
        const char *funcname;
 };