]> git.proxmox.com Git - libgit2.git/blob - src/thread-utils.h
Merge pull request #2612 from ethomson/warnings
[libgit2.git] / src / thread-utils.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7 #ifndef INCLUDE_thread_utils_h__
8 #define INCLUDE_thread_utils_h__
9
10 /* Common operations even if threading has been disabled */
11 typedef struct {
12 #if defined(GIT_WIN32)
13 volatile long val;
14 #else
15 volatile int val;
16 #endif
17 } git_atomic;
18
19 #ifdef GIT_ARCH_64
20
21 typedef struct {
22 #if defined(GIT_WIN32)
23 __int64 val;
24 #else
25 int64_t val;
26 #endif
27 } git_atomic64;
28
29 typedef git_atomic64 git_atomic_ssize;
30
31 #define git_atomic_ssize_add git_atomic64_add
32
33 #else
34
35 typedef git_atomic git_atomic_ssize;
36
37 #define git_atomic_ssize_add git_atomic_add
38
39 #endif
40
41 #ifdef GIT_THREADS
42
43 #if !defined(GIT_WIN32)
44
45 typedef struct {
46 pthread_t thread;
47 } git_thread;
48
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)
53
54 #endif
55
56 /* Pthreads Mutex */
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)
62
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)
70
71 /* Pthread (-ish) rwlock
72 *
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
79 */
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
88
89 #ifndef GIT_WIN32
90 #define pthread_rwlock_rdunlock pthread_rwlock_unlock
91 #define pthread_rwlock_wrunlock pthread_rwlock_unlock
92 #endif
93
94
95 GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
96 {
97 #if defined(GIT_WIN32)
98 InterlockedExchange(&a->val, (LONG)val);
99 #elif defined(__GNUC__)
100 __sync_lock_test_and_set(&a->val, val);
101 #else
102 # error "Unsupported architecture for atomic operations"
103 #endif
104 }
105
106 GIT_INLINE(int) git_atomic_inc(git_atomic *a)
107 {
108 #if defined(GIT_WIN32)
109 return InterlockedIncrement(&a->val);
110 #elif defined(__GNUC__)
111 return __sync_add_and_fetch(&a->val, 1);
112 #else
113 # error "Unsupported architecture for atomic operations"
114 #endif
115 }
116
117 GIT_INLINE(int) git_atomic_add(git_atomic *a, int32_t addend)
118 {
119 #if defined(GIT_WIN32)
120 return InterlockedExchangeAdd(&a->val, addend);
121 #elif defined(__GNUC__)
122 return __sync_add_and_fetch(&a->val, addend);
123 #else
124 # error "Unsupported architecture for atomic operations"
125 #endif
126 }
127
128 GIT_INLINE(int) git_atomic_dec(git_atomic *a)
129 {
130 #if defined(GIT_WIN32)
131 return InterlockedDecrement(&a->val);
132 #elif defined(__GNUC__)
133 return __sync_sub_and_fetch(&a->val, 1);
134 #else
135 # error "Unsupported architecture for atomic operations"
136 #endif
137 }
138
139 GIT_INLINE(void *) git___compare_and_swap(
140 void * volatile *ptr, void *oldval, void *newval)
141 {
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);
147 #else
148 # error "Unsupported architecture for atomic operations"
149 #endif
150 return (foundval == oldval) ? oldval : newval;
151 }
152
153 GIT_INLINE(volatile void *) git___swap(
154 void * volatile *ptr, void *newval)
155 {
156 #if defined(GIT_WIN32)
157 return InterlockedExchangePointer(ptr, newval);
158 #else
159 return __sync_lock_test_and_set(ptr, newval);
160 #endif
161 }
162
163 #ifdef GIT_ARCH_64
164
165 GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
166 {
167 #if defined(GIT_WIN32)
168 return InterlockedExchangeAdd64(&a->val, addend);
169 #elif defined(__GNUC__)
170 return __sync_add_and_fetch(&a->val, addend);
171 #else
172 # error "Unsupported architecture for atomic operations"
173 #endif
174 }
175
176 #endif
177
178 #else
179
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
183
184 /* Pthreads Mutex */
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
192
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
200
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
210
211
212 GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
213 {
214 a->val = val;
215 }
216
217 GIT_INLINE(int) git_atomic_inc(git_atomic *a)
218 {
219 return ++a->val;
220 }
221
222 GIT_INLINE(int) git_atomic_add(git_atomic *a, int32_t addend)
223 {
224 a->val += addend;
225 return a->val;
226 }
227
228 GIT_INLINE(int) git_atomic_dec(git_atomic *a)
229 {
230 return --a->val;
231 }
232
233 GIT_INLINE(void *) git___compare_and_swap(
234 void * volatile *ptr, void *oldval, void *newval)
235 {
236 if (*ptr == oldval)
237 *ptr = newval;
238 else
239 oldval = newval;
240 return oldval;
241 }
242
243 GIT_INLINE(volatile void *) git___swap(
244 void * volatile *ptr, void *newval)
245 {
246 volatile void *old = *ptr;
247 *ptr = newval;
248 return old;
249 }
250
251 #ifdef GIT_ARCH_64
252
253 GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
254 {
255 a->val += addend;
256 return a->val;
257 }
258
259 #endif
260
261 #endif
262
263 GIT_INLINE(int) git_atomic_get(git_atomic *a)
264 {
265 return (int)a->val;
266 }
267
268 /* Atomically replace oldval with newval
269 * @return oldval if it was replaced or newval if it was not
270 */
271 #define git__compare_and_swap(P,O,N) \
272 git___compare_and_swap((void * volatile *)P, O, N)
273
274 #define git__swap(ptr, val) (void *)git___swap((void * volatile *)&ptr, val)
275
276 extern int git_online_cpus(void);
277
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()
282 #else
283 # define GIT_MEMORY_BARRIER /* noop */
284 #endif
285
286 #endif /* INCLUDE_thread_utils_h__ */