diff options
| author | Vicent Martà <vicent@github.com> | 2013-04-23 11:48:12 -0700 |
|---|---|---|
| committer | Vicent Martà <vicent@github.com> | 2013-04-23 11:48:12 -0700 |
| commit | 7915e6cc66a28d0767fc9899e58b2ca544ee12d0 (patch) | |
| tree | 9ab7355df505fcb0b9c2b210212321b752f0ec12 /src/thread-utils.h | |
| parent | 6c9dc12b6a86290af63ed11f1209ebad91e3e1ca (diff) | |
| parent | a2378ae4fee55c95eb9a1f6b44f5a837d39fa724 (diff) | |
| download | libgit2-7915e6cc66a28d0767fc9899e58b2ca544ee12d0.tar.gz | |
Merge pull request #1498 from libgit2/vmg/atomic64
64 bit atomic operations and shared cache memory usage
Diffstat (limited to 'src/thread-utils.h')
| -rw-r--r-- | src/thread-utils.h | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/thread-utils.h b/src/thread-utils.h index dafe70ad6..28ecd297e 100644 --- a/src/thread-utils.h +++ b/src/thread-utils.h @@ -18,6 +18,14 @@ typedef struct { #endif } git_atomic; +typedef struct { +#if defined(GIT_WIN32) + __int64 val; +#else + int64_t val; +#endif +} git_atomic64; + GIT_INLINE(void) git_atomic_set(git_atomic *a, int val) { a->val = val; @@ -57,6 +65,17 @@ GIT_INLINE(int) git_atomic_inc(git_atomic *a) #endif } +GIT_INLINE(int) git_atomic_add(git_atomic *a, int32_t addend) +{ +#if defined(GIT_WIN32) + return _InterlockedExchangeAdd(&a->val, addend); +#elif defined(__GNUC__) + return __sync_add_and_fetch(&a->val, addend); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + GIT_INLINE(int) git_atomic_dec(git_atomic *a) { #if defined(GIT_WIN32) @@ -82,6 +101,17 @@ GIT_INLINE(void *) git___compare_and_swap( return (foundval == oldval) ? oldval : newval; } +GIT_INLINE(int) git_atomic64_add(git_atomic64 *a, int64_t addend) +{ +#if defined(GIT_WIN32) + return _InterlockedExchangeAdd64(&a->val, addend); +#elif defined(__GNUC__) + return __sync_add_and_fetch(&a->val, addend); +#else +# error "Unsupported architecture for atomic operations" +#endif +} + #else #define git_thread unsigned int @@ -110,6 +140,12 @@ GIT_INLINE(int) git_atomic_inc(git_atomic *a) return ++a->val; } +GIT_INLINE(int) git_atomic_add(git_atomic *a, int32_t addend) +{ + a->val += addend; + return a->val; +} + GIT_INLINE(int) git_atomic_dec(git_atomic *a) { return --a->val; @@ -125,6 +161,12 @@ GIT_INLINE(void *) git___compare_and_swap( return oldval; } +GIT_INLINE(int) git_atomic64_add(git_atomic64 *a, int64_t addend) +{ + a->val += addend; + return a->val; +} + #endif /* Atomically replace oldval with newval |
