From c8a65463b20c00b3fd635eb4ca97f3b53927c0f9 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Mon, 11 Feb 2019 11:38:57 +0100 Subject: [PATCH] lib: make atomic ops C++ compatible 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 --- lib/debug.h | 2 +- lib/frr_pthread.h | 2 +- lib/frratomic.h | 31 ++++++++++++++++++++++++++++++- lib/hash.h | 4 ++-- lib/memory.h | 10 +++++----- lib/stream.h | 4 ++-- lib/thread.c | 3 ++- lib/thread.h | 8 ++++---- 8 files changed, 47 insertions(+), 17 deletions(-) diff --git a/lib/debug.h b/lib/debug.h index d0fa27d3fe..2b389e3d4c 100644 --- a/lib/debug.h +++ b/lib/debug.h @@ -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; }; diff --git a/lib/frr_pthread.h b/lib/frr_pthread.h index e6b3f031b3..d487c738af 100644 --- a/lib/frr_pthread.h +++ b/lib/frr_pthread.h @@ -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 diff --git a/lib/frratomic.h b/lib/frratomic.h index 1f1d1b569a..e86030f83c 100644 --- a/lib/frratomic.h +++ b/lib/frratomic.h @@ -26,7 +26,23 @@ #endif /* ISO C11 */ -#ifdef HAVE_STDATOMIC_H +#ifdef __cplusplus +#include +#include +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 atomic_bool; +typedef std::atomic atomic_size_t; +typedef std::atomic atomic_uint_fast32_t; + +#elif defined(HAVE_STDATOMIC_H) #include /* 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 @@ -198,4 +216,15 @@ #error no atomic functions... #endif +#ifdef _ATOMIC_WANT_TYPEDEFS +#undef _ATOMIC_WANT_TYPEDEFS + +#include +#include + +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 */ diff --git a/lib/hash.h b/lib/hash.h index 45ae6ce60a..aca2590a95 100644 --- a/lib/hash.h +++ b/lib/hash.h @@ -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 { diff --git a/lib/memory.h b/lib/memory.h index 1a0269f8bb..40b9d36ee6 100644 --- a/lib/memory.h +++ b/lib/memory.h @@ -33,12 +33,12 @@ 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 }; diff --git a/lib/stream.h b/lib/stream.h index 32b6fb5af1..20c5d3d77d 100644 --- a/lib/stream.h +++ b/lib/stream.h @@ -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; diff --git a/lib/thread.c b/lib/thread.c index ae8e375a27..8c1b3ff06d 100644 --- a/lib/thread.c +++ b/lib/thread.c @@ -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); diff --git a/lib/thread.h b/lib/thread.h index f404d92755..036e39f77e 100644 --- a/lib/thread.h +++ b/lib/thread.h @@ -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; }; -- 2.39.5