diff options
author | David Lamparter <equinox@opensourcerouting.org> | 2016-10-26 17:19:56 +0200 |
---|---|---|
committer | David Lamparter <equinox@opensourcerouting.org> | 2017-03-31 17:59:48 +0200 |
commit | a31446a88f7751b763d0af2a3d92c9e0887faa61 (patch) | |
tree | 91a7bcf0fd64b1fe3d0ef581d338fdeb2993b758 /lib/memory.c | |
parent | build: try to find some kind of atomic ops (diff) | |
download | frr-a31446a88f7751b763d0af2a3d92c9e0887faa61.tar.xz frr-a31446a88f7751b763d0af2a3d92c9e0887faa61.zip |
lib: memtypes: restore atomicity
the original version of this code already used _Atomic and atomic_*().
Restore this functionality for future multithreading.
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'lib/memory.c')
-rw-r--r-- | lib/memory.c | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/lib/memory.c b/lib/memory.c index ad55366f6..28e358dfc 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -30,19 +30,22 @@ DEFINE_MTYPE(LIB, TMP, "Temporary memory") static inline void mt_count_alloc (struct memtype *mt, size_t size) { - mt->n_alloc++; + size_t oldsize; - if (mt->size == 0) - mt->size = size; - else if (mt->size != size) - mt->size = SIZE_VAR; + atomic_fetch_add_explicit(&mt->n_alloc, 1, memory_order_relaxed); + + oldsize = atomic_load_explicit(&mt->size, memory_order_relaxed); + if (oldsize == 0) + oldsize = atomic_exchange_explicit(&mt->size, size, memory_order_relaxed); + if (oldsize != 0 && oldsize != size && oldsize != SIZE_VAR) + atomic_store_explicit(&mt->size, SIZE_VAR, memory_order_relaxed); } static inline void mt_count_free (struct memtype *mt) { assert(mt->n_alloc); - mt->n_alloc--; + atomic_fetch_sub_explicit(&mt->n_alloc, 1, memory_order_relaxed); } static inline void * |