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 <equinox@diac24.net>
This commit is contained in:
David Lamparter 2019-02-11 11:38:57 +01:00 committed by Renato Westphal
parent be566e4e45
commit c8a65463b2
8 changed files with 47 additions and 17 deletions

View 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;
};

View 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

View File

@ -26,7 +26,23 @@
#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
@ -198,4 +216,15 @@
#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 */

View 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 {

View File

@ -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
};

View 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;

View 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);

View 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;
};