]> git.proxmox.com Git - libgit2.git/blob - src/thread-utils.h
Properly export all external symbols in Win32
[libgit2.git] / src / thread-utils.h
1 #ifndef INCLUDE_thread_utils_h__
2 #define INCLUDE_thread_utils_h__
3
4 #if defined(GIT_HAS_PTHREAD)
5 typedef pthread_mutex_t git_lck;
6 # define GITLCK_INIT PTHREAD_MUTEX_INITIALIZER
7 # define gitlck_init(a) pthread_mutex_init(a, NULL)
8 # define gitlck_lock(a) pthread_mutex_lock(a)
9 # define gitlck_unlock(a) pthread_mutex_unlock(a)
10 # define gitlck_free(a) pthread_mutex_destroy(a)
11
12 # if defined(GIT_HAS_ASM_ATOMIC)
13 # include <asm/atomic.h>
14 typedef atomic_t git_refcnt;
15 # define gitrc_init(a) atomic_set(a, 0)
16 # define gitrc_inc(a) atomic_inc_return(a)
17 # define gitrc_dec(a) atomic_dec_and_test(a)
18 # define gitrc_free(a) (void)0
19
20 # else
21 typedef struct { git_lck lock; int counter; } git_refcnt;
22
23 /** Initialize to 0. No memory barrier is issued. */
24 GIT_INLINE(void) gitrc_init(git_refcnt *p)
25 {
26 gitlck_init(&p->lock);
27 p->counter = 0;
28 }
29
30 /**
31 * Increment.
32 *
33 * Atomically increments @p by 1. A memory barrier is also
34 * issued before and after the operation.
35 *
36 * @param p pointer of type git_refcnt
37 */
38 GIT_INLINE(void) gitrc_inc(git_refcnt *p)
39 {
40 gitlck_lock(&p->lock);
41 p->counter++;
42 gitlck_unlock(&p->lock);
43 }
44
45 /**
46 * Decrement and test.
47 *
48 * Atomically decrements @p by 1 and returns true if the
49 * result is 0, or false for all other cases. A memory
50 * barrier is also issued before and after the operation.
51 *
52 * @param p pointer of type git_refcnt
53 */
54 GIT_INLINE(int) gitrc_dec(git_refcnt *p)
55 {
56 int c;
57 gitlck_lock(&p->lock);
58 c = --p->counter;
59 gitlck_unlock(&p->lock);
60 return !c;
61 }
62
63 /** Free any resources associated with the counter. */
64 # define gitrc_free(p) gitlck_free(&(p)->lock)
65
66 # endif
67
68 #elif defined(GIT_THREADS)
69 # error GIT_THREADS but no git_lck implementation
70
71 #else
72 typedef struct { int dummy; } git_lck;
73 # define GIT_MUTEX_INIT {}
74 # define gitlck_init(a) (void)0
75 # define gitlck_lock(a) (void)0
76 # define gitlck_unlock(a) (void)0
77 # define gitlck_free(a) (void)0
78
79 typedef struct { int counter; } git_refcnt;
80 # define gitrc_init(a) ((a)->counter = 0)
81 # define gitrc_inc(a) ((a)->counter++)
82 # define gitrc_dec(a) (--(a)->counter == 0)
83 # define gitrc_free(a) (void)0
84
85 #endif
86
87 GIT_EXTERN(int) git_online_cpus(void);
88
89 #endif /* INCLUDE_thread_utils_h__ */