2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
7 #ifndef INCLUDE_thread_utils_h__
8 #define INCLUDE_thread_utils_h__
10 /* Common operations even if threading has been disabled */
12 #if defined(GIT_WIN32)
22 #if defined(GIT_WIN32)
29 typedef git_atomic64 git_atomic_ssize
;
31 #define git_atomic_ssize_add git_atomic64_add
35 typedef git_atomic git_atomic_ssize
;
37 #define git_atomic_ssize_add git_atomic_add
43 #if !defined(GIT_WIN32)
49 #define git_thread_create(git_thread_ptr, attr, start_routine, arg) \
50 pthread_create(&(git_thread_ptr)->thread, attr, start_routine, arg)
51 #define git_thread_join(git_thread_ptr, status) \
52 pthread_join((git_thread_ptr)->thread, status)
56 #if defined(GIT_WIN32)
57 #define git_thread_yield() Sleep(0)
59 #define git_thread_yield() sched_yield()
63 #define git_mutex pthread_mutex_t
64 #define git_mutex_init(a) pthread_mutex_init(a, NULL)
65 #define git_mutex_lock(a) pthread_mutex_lock(a)
66 #define git_mutex_unlock(a) pthread_mutex_unlock(a)
67 #define git_mutex_free(a) pthread_mutex_destroy(a)
69 /* Pthreads condition vars */
70 #define git_cond pthread_cond_t
71 #define git_cond_init(c) pthread_cond_init(c, NULL)
72 #define git_cond_free(c) pthread_cond_destroy(c)
73 #define git_cond_wait(c, l) pthread_cond_wait(c, l)
74 #define git_cond_signal(c) pthread_cond_signal(c)
75 #define git_cond_broadcast(c) pthread_cond_broadcast(c)
77 /* Pthread (-ish) rwlock
79 * This differs from normal pthreads rwlocks in two ways:
80 * 1. Separate APIs for releasing read locks and write locks (as
81 * opposed to the pure POSIX API which only has one unlock fn)
82 * 2. You should not use recursive read locks (i.e. grabbing a read
83 * lock in a thread that already holds a read lock) because the
84 * Windows implementation doesn't support it
86 #define git_rwlock pthread_rwlock_t
87 #define git_rwlock_init(a) pthread_rwlock_init(a, NULL)
88 #define git_rwlock_rdlock(a) pthread_rwlock_rdlock(a)
89 #define git_rwlock_rdunlock(a) pthread_rwlock_rdunlock(a)
90 #define git_rwlock_wrlock(a) pthread_rwlock_wrlock(a)
91 #define git_rwlock_wrunlock(a) pthread_rwlock_wrunlock(a)
92 #define git_rwlock_free(a) pthread_rwlock_destroy(a)
93 #define GIT_RWLOCK_STATIC_INIT PTHREAD_RWLOCK_INITIALIZER
96 #define pthread_rwlock_rdunlock pthread_rwlock_unlock
97 #define pthread_rwlock_wrunlock pthread_rwlock_unlock
101 GIT_INLINE(void) git_atomic_set(git_atomic
*a
, int val
)
103 #if defined(GIT_WIN32)
104 InterlockedExchange(&a
->val
, (LONG
)val
);
105 #elif defined(__GNUC__)
106 __sync_lock_test_and_set(&a
->val
, val
);
108 # error "Unsupported architecture for atomic operations"
112 GIT_INLINE(int) git_atomic_inc(git_atomic
*a
)
114 #if defined(GIT_WIN32)
115 return InterlockedIncrement(&a
->val
);
116 #elif defined(__GNUC__)
117 return __sync_add_and_fetch(&a
->val
, 1);
119 # error "Unsupported architecture for atomic operations"
123 GIT_INLINE(int) git_atomic_add(git_atomic
*a
, int32_t addend
)
125 #if defined(GIT_WIN32)
126 return InterlockedExchangeAdd(&a
->val
, addend
);
127 #elif defined(__GNUC__)
128 return __sync_add_and_fetch(&a
->val
, addend
);
130 # error "Unsupported architecture for atomic operations"
134 GIT_INLINE(int) git_atomic_dec(git_atomic
*a
)
136 #if defined(GIT_WIN32)
137 return InterlockedDecrement(&a
->val
);
138 #elif defined(__GNUC__)
139 return __sync_sub_and_fetch(&a
->val
, 1);
141 # error "Unsupported architecture for atomic operations"
145 GIT_INLINE(void *) git___compare_and_swap(
146 void * volatile *ptr
, void *oldval
, void *newval
)
148 volatile void *foundval
;
149 #if defined(GIT_WIN32)
150 foundval
= InterlockedCompareExchangePointer((volatile PVOID
*)ptr
, newval
, oldval
);
151 #elif defined(__GNUC__)
152 foundval
= __sync_val_compare_and_swap(ptr
, oldval
, newval
);
154 # error "Unsupported architecture for atomic operations"
156 return (foundval
== oldval
) ? oldval
: newval
;
159 GIT_INLINE(volatile void *) git___swap(
160 void * volatile *ptr
, void *newval
)
162 #if defined(GIT_WIN32)
163 return InterlockedExchangePointer(ptr
, newval
);
165 return __sync_lock_test_and_set(ptr
, newval
);
171 GIT_INLINE(int64_t) git_atomic64_add(git_atomic64
*a
, int64_t addend
)
173 #if defined(GIT_WIN32)
174 return InterlockedExchangeAdd64(&a
->val
, addend
);
175 #elif defined(__GNUC__)
176 return __sync_add_and_fetch(&a
->val
, addend
);
178 # error "Unsupported architecture for atomic operations"
186 #define git_thread unsigned int
187 #define git_thread_create(thread, attr, start_routine, arg) 0
188 #define git_thread_join(id, status) (void)0
189 #define git_thread_yield() (void)0
192 #define git_mutex unsigned int
193 #define git_mutex_init(a) 0
194 #define git_mutex_lock(a) 0
195 #define git_mutex_unlock(a) (void)0
196 #define git_mutex_free(a) (void)0
198 /* Pthreads condition vars */
199 #define git_cond unsigned int
200 #define git_cond_init(c, a) (void)0
201 #define git_cond_free(c) (void)0
202 #define git_cond_wait(c, l) (void)0
203 #define git_cond_signal(c) (void)0
204 #define git_cond_broadcast(c) (void)0
206 /* Pthreads rwlock */
207 #define git_rwlock unsigned int
208 #define git_rwlock_init(a) 0
209 #define git_rwlock_rdlock(a) 0
210 #define git_rwlock_rdunlock(a) (void)0
211 #define git_rwlock_wrlock(a) 0
212 #define git_rwlock_wrunlock(a) (void)0
213 #define git_rwlock_free(a) (void)0
214 #define GIT_RWLOCK_STATIC_INIT 0
217 GIT_INLINE(void) git_atomic_set(git_atomic
*a
, int val
)
222 GIT_INLINE(int) git_atomic_inc(git_atomic
*a
)
227 GIT_INLINE(int) git_atomic_add(git_atomic
*a
, int32_t addend
)
233 GIT_INLINE(int) git_atomic_dec(git_atomic
*a
)
238 GIT_INLINE(void *) git___compare_and_swap(
239 void * volatile *ptr
, void *oldval
, void *newval
)
248 GIT_INLINE(volatile void *) git___swap(
249 void * volatile *ptr
, void *newval
)
251 volatile void *old
= *ptr
;
258 GIT_INLINE(int64_t) git_atomic64_add(git_atomic64
*a
, int64_t addend
)
268 GIT_INLINE(int) git_atomic_get(git_atomic
*a
)
273 /* Atomically replace oldval with newval
274 * @return oldval if it was replaced or newval if it was not
276 #define git__compare_and_swap(P,O,N) \
277 git___compare_and_swap((void * volatile *)P, O, N)
279 #define git__swap(ptr, val) (void *)git___swap((void * volatile *)&ptr, val)
281 extern int git_online_cpus(void);
283 #if defined(GIT_THREADS) && defined(GIT_WIN32)
284 # define GIT_MEMORY_BARRIER MemoryBarrier()
285 #elif defined(GIT_THREADS)
286 # define GIT_MEMORY_BARRIER __sync_synchronize()
288 # define GIT_MEMORY_BARRIER /* noop */
291 #endif /* INCLUDE_thread_utils_h__ */