]>
Commit | Line | Data |
---|---|---|
bb742ede | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
bb742ede VM |
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 | */ | |
36f0f61f AE |
7 | #ifndef INCLUDE_thread_utils_h__ |
8 | #define INCLUDE_thread_utils_h__ | |
9 | ||
a15c550d | 10 | #include "common.h" |
bb3de0c4 VM |
11 | |
12 | /* Common operations even if threading has been disabled */ | |
13 | typedef struct { | |
7a6f51de | 14 | #if defined(GIT_WIN32) |
b932ef5b VM |
15 | volatile long val; |
16 | #else | |
bb3de0c4 | 17 | volatile int val; |
b932ef5b | 18 | #endif |
bb3de0c4 VM |
19 | } git_atomic; |
20 | ||
72a3fe42 | 21 | GIT_INLINE(void) git_atomic_set(git_atomic *a, int val) |
bb3de0c4 VM |
22 | { |
23 | a->val = val; | |
24 | } | |
25 | ||
26 | #ifdef GIT_THREADS | |
27 | ||
bbcc7ffc VM |
28 | #define git_thread pthread_t |
29 | #define git_thread_create(thread, attr, start_routine, arg) pthread_create(thread, attr, start_routine, arg) | |
30 | #define git_thread_kill(thread) pthread_cancel(thread) | |
31 | #define git_thread_exit(status) pthread_exit(status) | |
32 | #define git_thread_join(id, status) pthread_join(id, status) | |
33 | ||
34 | /* Pthreads Mutex */ | |
35 | #define git_mutex pthread_mutex_t | |
87d9869f VM |
36 | #define git_mutex_init(a) pthread_mutex_init(a, NULL) |
37 | #define git_mutex_lock(a) pthread_mutex_lock(a) | |
bbcc7ffc | 38 | #define git_mutex_unlock(a) pthread_mutex_unlock(a) |
87d9869f | 39 | #define git_mutex_free(a) pthread_mutex_destroy(a) |
bbcc7ffc | 40 | |
fa16a6ec MS |
41 | /* Pthreads condition vars */ |
42 | #define git_cond pthread_cond_t | |
43 | #define git_cond_init(c) pthread_cond_init(c, NULL) | |
44 | #define git_cond_free(c) pthread_cond_destroy(c) | |
45 | #define git_cond_wait(c, l) pthread_cond_wait(c, l) | |
46 | #define git_cond_signal(c) pthread_cond_signal(c) | |
47 | #define git_cond_broadcast(c) pthread_cond_broadcast(c) | |
bb3de0c4 | 48 | |
72a3fe42 | 49 | GIT_INLINE(int) git_atomic_inc(git_atomic *a) |
bb3de0c4 | 50 | { |
7a6f51de | 51 | #if defined(GIT_WIN32) |
bb3de0c4 | 52 | return InterlockedIncrement(&a->val); |
7a6f51de VM |
53 | #elif defined(__GNUC__) |
54 | return __sync_add_and_fetch(&a->val, 1); | |
bb3de0c4 VM |
55 | #else |
56 | # error "Unsupported architecture for atomic operations" | |
57 | #endif | |
58 | } | |
59 | ||
72a3fe42 | 60 | GIT_INLINE(int) git_atomic_dec(git_atomic *a) |
bb3de0c4 | 61 | { |
7a6f51de | 62 | #if defined(GIT_WIN32) |
bb3de0c4 | 63 | return InterlockedDecrement(&a->val); |
7a6f51de VM |
64 | #elif defined(__GNUC__) |
65 | return __sync_sub_and_fetch(&a->val, 1); | |
bb3de0c4 VM |
66 | #else |
67 | # error "Unsupported architecture for atomic operations" | |
68 | #endif | |
69 | } | |
70 | ||
71 | #else | |
72 | ||
73 | #define git_thread unsigned int | |
74 | #define git_thread_create(thread, attr, start_routine, arg) (void)0 | |
75 | #define git_thread_kill(thread) (void)0 | |
76 | #define git_thread_exit(status) (void)0 | |
77 | #define git_thread_join(id, status) (void)0 | |
78 | ||
79 | /* Pthreads Mutex */ | |
80 | #define git_mutex unsigned int | |
81 | #define git_mutex_init(a) (void)0 | |
2bb1c7aa | 82 | #define git_mutex_lock(a) 0 |
bb3de0c4 VM |
83 | #define git_mutex_unlock(a) (void)0 |
84 | #define git_mutex_free(a) (void)0 | |
85 | ||
bbcc7ffc | 86 | /* Pthreads condition vars */ |
bb3de0c4 VM |
87 | #define git_cond unsigned int |
88 | #define git_cond_init(c, a) (void)0 | |
89 | #define git_cond_free(c) (void)0 | |
90 | #define git_cond_wait(c, l) (void)0 | |
91 | #define git_cond_signal(c) (void)0 | |
92 | #define git_cond_broadcast(c) (void)0 | |
93 | ||
72a3fe42 | 94 | GIT_INLINE(int) git_atomic_inc(git_atomic *a) |
bb3de0c4 VM |
95 | { |
96 | return ++a->val; | |
97 | } | |
98 | ||
72a3fe42 | 99 | GIT_INLINE(int) git_atomic_dec(git_atomic *a) |
bb3de0c4 VM |
100 | { |
101 | return --a->val; | |
102 | } | |
103 | ||
104 | #endif | |
36f0f61f | 105 | |
e035685f | 106 | extern int git_online_cpus(void); |
36f0f61f AE |
107 | |
108 | #endif /* INCLUDE_thread_utils_h__ */ |