From f49f7f94c10de1ab39bd045e4ff6bb281541b51f Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 22 Dec 2009 17:08:14 -0800 Subject: More progress, upb_data and upb_def compile (but are incomplete). --- src/upb_atomic.h | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'src/upb_atomic.h') diff --git a/src/upb_atomic.h b/src/upb_atomic.h index e425502..de2238c 100644 --- a/src/upb_atomic.h +++ b/src/upb_atomic.h @@ -35,28 +35,34 @@ extern "C" { /* Non-thread-safe implementations. ******************************************/ typedef struct { - int val; + int v; } upb_atomic_refcount_t; INLINE void upb_atomic_refcount_init(upb_atomic_refcount_t *a, int val) { - a->val = val; + a->v = val; } INLINE bool upb_atomic_ref(upb_atomic_refcount_t *a) { - return a->val++ == 0; + return a->v++ == 0; } INLINE bool upb_atomic_unref(upb_atomic_refcount_t *a) { - return --a->val == 0; + return --a->v == 0; } INLINE int upb_atomic_read(upb_atomic_refcount_t *a) { - return a->val; + return a->v; } INLINE bool upb_atomic_add(upb_atomic_refcount_t *a, int val) { - a->val += val; - return a->val == 0; + a->v += val; + return a->v == 0; +} + +INLINE bool upb_atomic_fetch_and_add(upb_atomic_refcount_t *a, int val) { + int ret = a->v; + a->v += val; + return ret; } typedef struct { @@ -81,32 +87,32 @@ INLINE void upb_rwlock_unlock(upb_rwlock_t *l) { (void)l; } /* GCC includes atomic primitives. */ typedef struct { - volatile int val; + volatile int v; } upb_atomic_refcount_t; INLINE void upb_atomic_refcount_init(upb_atomic_refcount_t *a, int val) { - a->val = val; + a->v = val; __sync_synchronize(); /* Ensure the initialized value is visible. */ } INLINE bool upb_atomic_ref(upb_atomic_refcount_t *a) { - return __sync_fetch_and_add(&a->val, 1) == 0; + return __sync_fetch_and_add(&a->v, 1) == 0; } INLINE bool upb_atomic_add(upb_atomic_refcount_t *a, int n) { - return __sync_add_and_fetch(&a->val, n) == 0; + return __sync_add_and_fetch(&a->v, n) == 0; } INLINE bool upb_atomic_unref(upb_atomic_refcount_t *a) { - return __sync_sub_and_fetch(&a->val, 1) == 0; + return __sync_sub_and_fetch(&a->v, 1) == 0; } INLINE bool upb_atomic_read(upb_atomic_refcount_t *a) { - return __sync_fetch_and_add(&a->val, 0); + return __sync_fetch_and_add(&a->v, 0); } INLINE bool upb_atomic_write(upb_atomic_refcount_t *a, int val) { - a->val = val; + a->v = val; } #elif defined(WIN32) -- cgit v1.2.3