Fixed memory leaks in test suite
[libgit2.git] / src / thread-utils.h
blob0395b97d1e2df4bc7cf46a9a82b753a8908157b0
1 #ifndef INCLUDE_thread_utils_h__
2 #define INCLUDE_thread_utils_h__
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)
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
20 # else
21 typedef struct { git_lck lock; int counter; } git_refcnt;
23 /** Initialize to 0. No memory barrier is issued. */
24 GIT_INLINE(void) gitrc_init(git_refcnt *p)
26 gitlck_init(&p->lock);
27 p->counter = 0;
30 /**
31 * Increment.
33 * Atomically increments @p by 1. A memory barrier is also
34 * issued before and after the operation.
36 * @param p pointer of type git_refcnt
38 GIT_INLINE(void) gitrc_inc(git_refcnt *p)
40 gitlck_lock(&p->lock);
41 p->counter++;
42 gitlck_unlock(&p->lock);
45 /**
46 * Decrement and test.
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.
52 * @param p pointer of type git_refcnt
54 GIT_INLINE(int) gitrc_dec(git_refcnt *p)
56 int c;
57 gitlck_lock(&p->lock);
58 c = --p->counter;
59 gitlck_unlock(&p->lock);
60 return !c;
63 /** Free any resources associated with the counter. */
64 # define gitrc_free(p) gitlck_free(&(p)->lock)
66 # endif
68 #elif defined(GIT_THREADS)
69 # error GIT_THREADS but no git_lck implementation
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
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
85 #endif
87 extern int git_online_cpus(void);
89 #endif /* INCLUDE_thread_utils_h__ */