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)
57 #define git_mutex pthread_mutex_t
58 #define git_mutex_init(a) pthread_mutex_init(a, NULL)
59 #define git_mutex_lock(a) pthread_mutex_lock(a)
60 #define git_mutex_unlock(a) pthread_mutex_unlock(a)
61 #define git_mutex_free(a) pthread_mutex_destroy(a)
63 /* Pthreads condition vars */
64 #define git_cond pthread_cond_t
65 #define git_cond_init(c) pthread_cond_init(c, NULL)
66 #define git_cond_free(c) pthread_cond_destroy(c)
67 #define git_cond_wait(c, l) pthread_cond_wait(c, l)
68 #define git_cond_signal(c) pthread_cond_signal(c)
69 #define git_cond_broadcast(c) pthread_cond_broadcast(c)
71 /* Pthread (-ish) rwlock
73 * This differs from normal pthreads rwlocks in two ways:
74 * 1. Separate APIs for releasing read locks and write locks (as
75 * opposed to the pure POSIX API which only has one unlock fn)
76 * 2. You should not use recursive read locks (i.e. grabbing a read
77 * lock in a thread that already holds a read lock) because the
78 * Windows implementation doesn't support it
80 #define git_rwlock pthread_rwlock_t
81 #define git_rwlock_init(a) pthread_rwlock_init(a, NULL)
82 #define git_rwlock_rdlock(a) pthread_rwlock_rdlock(a)
83 #define git_rwlock_rdunlock(a) pthread_rwlock_rdunlock(a)
84 #define git_rwlock_wrlock(a) pthread_rwlock_wrlock(a)
85 #define git_rwlock_wrunlock(a) pthread_rwlock_wrunlock(a)
86 #define git_rwlock_free(a) pthread_rwlock_destroy(a)
87 #define GIT_RWLOCK_STATIC_INIT PTHREAD_RWLOCK_INITIALIZER
90 #define pthread_rwlock_rdunlock pthread_rwlock_unlock
91 #define pthread_rwlock_wrunlock pthread_rwlock_unlock
95 GIT_INLINE(void) git_atomic_set(git_atomic
*a
, int val
)
97 #if defined(GIT_WIN32)
98 InterlockedExchange(&a
->val
, (LONG
)val
);
99 #elif defined(__GNUC__)
100 __sync_lock_test_and_set(&a
->val
, val
);
102 # error "Unsupported architecture for atomic operations"
106 GIT_INLINE(int) git_atomic_inc(git_atomic
*a
)
108 #if defined(GIT_WIN32)
109 return InterlockedIncrement(&a
->val
);
110 #elif defined(__GNUC__)
111 return __sync_add_and_fetch(&a
->val
, 1);
113 # error "Unsupported architecture for atomic operations"
117 GIT_INLINE(int) git_atomic_add(git_atomic
*a
, int32_t addend
)
119 #if defined(GIT_WIN32)
120 return InterlockedExchangeAdd(&a
->val
, addend
);
121 #elif defined(__GNUC__)
122 return __sync_add_and_fetch(&a
->val
, addend
);
124 # error "Unsupported architecture for atomic operations"
128 GIT_INLINE(int) git_atomic_dec(git_atomic
*a
)
130 #if defined(GIT_WIN32)
131 return InterlockedDecrement(&a
->val
);
132 #elif defined(__GNUC__)
133 return __sync_sub_and_fetch(&a
->val
, 1);
135 # error "Unsupported architecture for atomic operations"
139 GIT_INLINE(void *) git___compare_and_swap(
140 void * volatile *ptr
, void *oldval
, void *newval
)
142 volatile void *foundval
;
143 #if defined(GIT_WIN32)
144 foundval
= InterlockedCompareExchangePointer((volatile PVOID
*)ptr
, newval
, oldval
);
145 #elif defined(__GNUC__)
146 foundval
= __sync_val_compare_and_swap(ptr
, oldval
, newval
);
148 # error "Unsupported architecture for atomic operations"
150 return (foundval
== oldval
) ? oldval
: newval
;
153 GIT_INLINE(volatile void *) git___swap(
154 void * volatile *ptr
, void *newval
)
156 #if defined(GIT_WIN32)
157 return InterlockedExchangePointer(ptr
, newval
);
159 return __sync_lock_test_and_set(ptr
, newval
);
165 GIT_INLINE(int64_t) git_atomic64_add(git_atomic64
*a
, int64_t addend
)
167 #if defined(GIT_WIN32)
168 return InterlockedExchangeAdd64(&a
->val
, addend
);
169 #elif defined(__GNUC__)
170 return __sync_add_and_fetch(&a
->val
, addend
);
172 # error "Unsupported architecture for atomic operations"
180 #define git_thread unsigned int
181 #define git_thread_create(thread, attr, start_routine, arg) 0
182 #define git_thread_join(id, status) (void)0
185 #define git_mutex unsigned int
186 GIT_INLINE(int) git_mutex_init(git_mutex
*mutex
) \
187 { GIT_UNUSED(mutex
); return 0; }
188 GIT_INLINE(int) git_mutex_lock(git_mutex
*mutex
) \
189 { GIT_UNUSED(mutex
); return 0; }
190 #define git_mutex_unlock(a) (void)0
191 #define git_mutex_free(a) (void)0
193 /* Pthreads condition vars */
194 #define git_cond unsigned int
195 #define git_cond_init(c, a) (void)0
196 #define git_cond_free(c) (void)0
197 #define git_cond_wait(c, l) (void)0
198 #define git_cond_signal(c) (void)0
199 #define git_cond_broadcast(c) (void)0
201 /* Pthreads rwlock */
202 #define git_rwlock unsigned int
203 #define git_rwlock_init(a) 0
204 #define git_rwlock_rdlock(a) 0
205 #define git_rwlock_rdunlock(a) (void)0
206 #define git_rwlock_wrlock(a) 0
207 #define git_rwlock_wrunlock(a) (void)0
208 #define git_rwlock_free(a) (void)0
209 #define GIT_RWLOCK_STATIC_INIT 0
212 GIT_INLINE(void) git_atomic_set(git_atomic
*a
, int val
)
217 GIT_INLINE(int) git_atomic_inc(git_atomic
*a
)
222 GIT_INLINE(int) git_atomic_add(git_atomic
*a
, int32_t addend
)
228 GIT_INLINE(int) git_atomic_dec(git_atomic
*a
)
233 GIT_INLINE(void *) git___compare_and_swap(
234 void * volatile *ptr
, void *oldval
, void *newval
)
243 GIT_INLINE(volatile void *) git___swap(
244 void * volatile *ptr
, void *newval
)
246 volatile void *old
= *ptr
;
253 GIT_INLINE(int64_t) git_atomic64_add(git_atomic64
*a
, int64_t addend
)
263 GIT_INLINE(int) git_atomic_get(git_atomic
*a
)
268 /* Atomically replace oldval with newval
269 * @return oldval if it was replaced or newval if it was not
271 #define git__compare_and_swap(P,O,N) \
272 git___compare_and_swap((void * volatile *)P, O, N)
274 #define git__swap(ptr, val) (void *)git___swap((void * volatile *)&ptr, val)
276 extern int git_online_cpus(void);
278 #if defined(GIT_THREADS) && defined(GIT_WIN32)
279 # define GIT_MEMORY_BARRIER MemoryBarrier()
280 #elif defined(GIT_THREADS)
281 # define GIT_MEMORY_BARRIER __sync_synchronize()
283 # define GIT_MEMORY_BARRIER /* noop */
286 #endif /* INCLUDE_thread_utils_h__ */