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 #define git_thread pthread_t
44 #define git_thread_create(thread, attr, start_routine, arg) \
45 pthread_create(thread, attr, start_routine, arg)
46 #define git_thread_kill(thread) pthread_cancel(thread)
47 #define git_thread_exit(status) pthread_exit(status)
48 #define git_thread_join(id, status) pthread_join(id, status)
51 #define git_mutex pthread_mutex_t
52 #define git_mutex_init(a) pthread_mutex_init(a, NULL)
53 #define git_mutex_lock(a) pthread_mutex_lock(a)
54 #define git_mutex_unlock(a) pthread_mutex_unlock(a)
55 #define git_mutex_free(a) pthread_mutex_destroy(a)
57 /* Pthreads condition vars */
58 #define git_cond pthread_cond_t
59 #define git_cond_init(c) pthread_cond_init(c, NULL)
60 #define git_cond_free(c) pthread_cond_destroy(c)
61 #define git_cond_wait(c, l) pthread_cond_wait(c, l)
62 #define git_cond_signal(c) pthread_cond_signal(c)
63 #define git_cond_broadcast(c) pthread_cond_broadcast(c)
65 /* Pthread (-ish) rwlock
67 * This differs from normal pthreads rwlocks in two ways:
68 * 1. Separate APIs for releasing read locks and write locks (as
69 * opposed to the pure POSIX API which only has one unlock fn)
70 * 2. You should not use recursive read locks (i.e. grabbing a read
71 * lock in a thread that already holds a read lock) because the
72 * Windows implementation doesn't support it
74 #define git_rwlock pthread_rwlock_t
75 #define git_rwlock_init(a) pthread_rwlock_init(a, NULL)
76 #define git_rwlock_rdlock(a) pthread_rwlock_rdlock(a)
77 #define git_rwlock_rdunlock(a) pthread_rwlock_rdunlock(a)
78 #define git_rwlock_wrlock(a) pthread_rwlock_wrlock(a)
79 #define git_rwlock_wrunlock(a) pthread_rwlock_wrunlock(a)
80 #define git_rwlock_free(a) pthread_rwlock_destroy(a)
81 #define GIT_RWLOCK_STATIC_INIT PTHREAD_RWLOCK_INITIALIZER
84 #define pthread_rwlock_rdunlock pthread_rwlock_unlock
85 #define pthread_rwlock_wrunlock pthread_rwlock_unlock
89 GIT_INLINE(void) git_atomic_set(git_atomic
*a
, int val
)
91 #if defined(GIT_WIN32)
92 InterlockedExchange(&a
->val
, (LONG
)val
);
93 #elif defined(__GNUC__)
94 __sync_lock_test_and_set(&a
->val
, val
);
96 # error "Unsupported architecture for atomic operations"
100 GIT_INLINE(int) git_atomic_inc(git_atomic
*a
)
102 #if defined(GIT_WIN32)
103 return InterlockedIncrement(&a
->val
);
104 #elif defined(__GNUC__)
105 return __sync_add_and_fetch(&a
->val
, 1);
107 # error "Unsupported architecture for atomic operations"
111 GIT_INLINE(int) git_atomic_add(git_atomic
*a
, int32_t addend
)
113 #if defined(GIT_WIN32)
114 return InterlockedExchangeAdd(&a
->val
, addend
);
115 #elif defined(__GNUC__)
116 return __sync_add_and_fetch(&a
->val
, addend
);
118 # error "Unsupported architecture for atomic operations"
122 GIT_INLINE(int) git_atomic_dec(git_atomic
*a
)
124 #if defined(GIT_WIN32)
125 return InterlockedDecrement(&a
->val
);
126 #elif defined(__GNUC__)
127 return __sync_sub_and_fetch(&a
->val
, 1);
129 # error "Unsupported architecture for atomic operations"
133 GIT_INLINE(void *) git___compare_and_swap(
134 void * volatile *ptr
, void *oldval
, void *newval
)
136 volatile void *foundval
;
137 #if defined(GIT_WIN32)
138 foundval
= InterlockedCompareExchangePointer((volatile PVOID
*)ptr
, newval
, oldval
);
139 #elif defined(__GNUC__)
140 foundval
= __sync_val_compare_and_swap(ptr
, oldval
, newval
);
142 # error "Unsupported architecture for atomic operations"
144 return (foundval
== oldval
) ? oldval
: newval
;
147 GIT_INLINE(volatile void *) git___swap(
148 void * volatile *ptr
, void *newval
)
150 #if defined(GIT_WIN32)
151 return InterlockedExchangePointer(ptr
, newval
);
153 return __sync_lock_test_and_set(ptr
, newval
);
159 GIT_INLINE(int64_t) git_atomic64_add(git_atomic64
*a
, int64_t addend
)
161 #if defined(GIT_WIN32)
162 return InterlockedExchangeAdd64(&a
->val
, addend
);
163 #elif defined(__GNUC__)
164 return __sync_add_and_fetch(&a
->val
, addend
);
166 # error "Unsupported architecture for atomic operations"
174 #define git_thread unsigned int
175 #define git_thread_create(thread, attr, start_routine, arg) 0
176 #define git_thread_kill(thread) (void)0
177 #define git_thread_exit(status) (void)0
178 #define git_thread_join(id, status) (void)0
181 #define git_mutex unsigned int
182 #define git_mutex_init(a) 0
183 #define git_mutex_lock(a) 0
184 #define git_mutex_unlock(a) (void)0
185 #define git_mutex_free(a) (void)0
187 /* Pthreads condition vars */
188 #define git_cond unsigned int
189 #define git_cond_init(c, a) (void)0
190 #define git_cond_free(c) (void)0
191 #define git_cond_wait(c, l) (void)0
192 #define git_cond_signal(c) (void)0
193 #define git_cond_broadcast(c) (void)0
195 /* Pthreads rwlock */
196 #define git_rwlock unsigned int
197 #define git_rwlock_init(a) 0
198 #define git_rwlock_rdlock(a) 0
199 #define git_rwlock_rdunlock(a) (void)0
200 #define git_rwlock_wrlock(a) 0
201 #define git_rwlock_wrunlock(a) (void)0
202 #define git_rwlock_free(a) (void)0
203 #define GIT_RWLOCK_STATIC_INIT 0
206 GIT_INLINE(void) git_atomic_set(git_atomic
*a
, int val
)
211 GIT_INLINE(int) git_atomic_inc(git_atomic
*a
)
216 GIT_INLINE(int) git_atomic_add(git_atomic
*a
, int32_t addend
)
222 GIT_INLINE(int) git_atomic_dec(git_atomic
*a
)
227 GIT_INLINE(void *) git___compare_and_swap(
228 void * volatile *ptr
, void *oldval
, void *newval
)
237 GIT_INLINE(volatile void *) git___swap(
238 void * volatile *ptr
, void *newval
)
240 volatile void *old
= *ptr
;
247 GIT_INLINE(int64_t) git_atomic64_add(git_atomic64
*a
, int64_t addend
)
257 GIT_INLINE(int) git_atomic_get(git_atomic
*a
)
262 /* Atomically replace oldval with newval
263 * @return oldval if it was replaced or newval if it was not
265 #define git__compare_and_swap(P,O,N) \
266 git___compare_and_swap((void * volatile *)P, O, N)
268 #define git__swap(ptr, val) (void *)git___swap((void * volatile *)&ptr, val)
270 extern int git_online_cpus(void);
272 #if defined(GIT_THREADS) && defined(GIT_WIN32)
273 # define GIT_MEMORY_BARRIER MemoryBarrier()
274 #elif defined(GIT_THREADS)
275 # define GIT_MEMORY_BARRIER __sync_synchronize()
277 # define GIT_MEMORY_BARRIER /* noop */
280 #endif /* INCLUDE_thread_utils_h__ */