summaryrefslogtreecommitdiff
path: root/src/thread-utils.h
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-03-16 21:35:51 +0200
committerVicent Marti <tanoku@gmail.com>2011-03-20 21:45:06 +0200
commitbb3de0c472b2d5d6b8091c190bee3db79c4b5e27 (patch)
tree6be2facd0014fff4a4769b6a6a082c722c03ad87 /src/thread-utils.h
parentb5c5f0f8086ee4e9bccf0703386fd5219ac380c2 (diff)
downloadlibgit2-bb3de0c472b2d5d6b8091c190bee3db79c4b5e27.tar.gz
Thread safe cache
Diffstat (limited to 'src/thread-utils.h')
-rw-r--r--src/thread-utils.h82
1 files changed, 76 insertions, 6 deletions
diff --git a/src/thread-utils.h b/src/thread-utils.h
index e8372e731..1cf0e3407 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -2,6 +2,19 @@
#define INCLUDE_thread_utils_h__
+
+/* Common operations even if threading has been disabled */
+typedef struct {
+ volatile int val;
+} git_atomic;
+
+static inline void git_atomic_set(git_atomic *a, int val)
+{
+ a->val = val;
+}
+
+#ifdef GIT_THREADS
+
#define git_thread pthread_t
#define git_thread_create(thread, attr, start_routine, arg) pthread_create(thread, attr, start_routine, arg)
#define git_thread_kill(thread) pthread_cancel(thread)
@@ -15,13 +28,70 @@
#define git_mutex_unlock(a) pthread_mutex_unlock(a)
#define git_mutex_free(a) pthread_mutex_destroy(a)
+/* Pthreads condition vars -- disabled by now */
+#define git_cond unsigned int //pthread_cond_t
+#define git_cond_init(c, a) (void)0 //pthread_cond_init(c, a)
+#define git_cond_free(c) (void)0 //pthread_cond_destroy(c)
+#define git_cond_wait(c, l) (void)0 //pthread_cond_wait(c, l)
+#define git_cond_signal(c) (void)0 //pthread_cond_signal(c)
+#define git_cond_broadcast(c) (void)0 //pthread_cond_broadcast(c)
+
+static inline int git_atomic_inc(git_atomic *a)
+{
+#ifdef __GNUC__
+ return __sync_add_and_fetch(&a->val, 1);
+#elif defined(_MSC_VER)
+ return InterlockedIncrement(&a->val);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+static inline int git_atomic_dec(git_atomic *a)
+{
+#ifdef __GNUC__
+ return __sync_sub_and_fetch(&a->val, 1);
+#elif defined(_MSC_VER)
+ return InterlockedDecrement(&a->val);
+#else
+# error "Unsupported architecture for atomic operations"
+#endif
+}
+
+#else
+
+#define git_thread unsigned int
+#define git_thread_create(thread, attr, start_routine, arg) (void)0
+#define git_thread_kill(thread) (void)0
+#define git_thread_exit(status) (void)0
+#define git_thread_join(id, status) (void)0
+
+/* Pthreads Mutex */
+#define git_mutex unsigned int
+#define git_mutex_init(a) (void)0
+#define git_mutex_lock(a) (void)0
+#define git_mutex_unlock(a) (void)0
+#define git_mutex_free(a) (void)0
+
/* Pthreads condition vars */
-#define git_cond pthread_cond_t
-#define git_cond_init(c, a) pthread_cond_init(c, a)
-#define git_cond_free(c) pthread_cond_destroy(c)
-#define git_cond_wait(c, l) pthread_cond_wait(c, l)
-#define git_cond_signal(c) pthread_cond_signal(c)
-#define git_cond_broadcast(c) pthread_cond_broadcast(c)
+#define git_cond unsigned int
+#define git_cond_init(c, a) (void)0
+#define git_cond_free(c) (void)0
+#define git_cond_wait(c, l) (void)0
+#define git_cond_signal(c) (void)0
+#define git_cond_broadcast(c) (void)0
+
+static inline int git_atomic_inc(git_atomic *a)
+{
+ return ++a->val;
+}
+
+static inline int git_atomic_dec(git_atomic *a)
+{
+ return --a->val;
+}
+
+#endif
extern int git_online_cpus(void);