summaryrefslogtreecommitdiff
path: root/src/thread-utils.h
diff options
context:
space:
mode:
authorVicent Martí <vicent@github.com>2013-04-22 08:07:20 -0700
committerVicent Martí <vicent@github.com>2013-04-22 08:07:20 -0700
commitd08dd728a80eb993d80ec30d8c9f9025664c8990 (patch)
treef5cf7daf3913578f57076c2efc94bfd1f728068c /src/thread-utils.h
parenta92dd316079250b27cc933b1fd00cd6af88b88d9 (diff)
parentd87715926049390a2417a2476742114ec966686a (diff)
downloadlibgit2-d08dd728a80eb993d80ec30d8c9f9025664c8990.tar.gz
Merge pull request #1454 from libgit2/vmg/new-cache
New caching
Diffstat (limited to 'src/thread-utils.h')
-rw-r--r--src/thread-utils.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/thread-utils.h b/src/thread-utils.h
index 2ca290adf..dafe70ad6 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -68,6 +68,20 @@ GIT_INLINE(int) git_atomic_dec(git_atomic *a)
#endif
}
+GIT_INLINE(void *) git___compare_and_swap(
+ volatile void **ptr, void *oldval, void *newval)
+{
+ volatile void *foundval;
+#if defined(GIT_WIN32)
+ foundval = InterlockedCompareExchangePointer(ptr, newval, oldval);
+#elif defined(__GNUC__)
+ foundval = __sync_val_compare_and_swap(ptr, oldval, newval);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+ return (foundval == oldval) ? oldval : newval;
+}
+
#else
#define git_thread unsigned int
@@ -101,8 +115,34 @@ GIT_INLINE(int) git_atomic_dec(git_atomic *a)
return --a->val;
}
+GIT_INLINE(void *) git___compare_and_swap(
+ volatile void **ptr, void *oldval, void *newval)
+{
+ if (*ptr == oldval)
+ *ptr = newval;
+ else
+ oldval = newval;
+ return oldval;
+}
+
#endif
+/* Atomically replace oldval with newval
+ * @return oldval if it was replaced or newval if it was not
+ */
+#define git__compare_and_swap(P,O,N) \
+ git___compare_and_swap((volatile void **)P, O, N)
+
+#define git__swap(ptr, val) git__compare_and_swap(&ptr, ptr, val)
+
extern int git_online_cpus(void);
+#if defined(GIT_THREADS) && defined(GIT_WIN32)
+# define GIT_MEMORY_BARRIER MemoryBarrier()
+#elif defined(GIT_THREADS)
+# define GIT_MEMORY_BARRIER __sync_synchronize()
+#else
+# define GIT_MEMORY_BARRIER /* noop */
+#endif
+
#endif /* INCLUDE_thread_utils_h__ */