]> git.proxmox.com Git - libgit2.git/blob - src/thread-utils.h
Update Copyright header
[libgit2.git] / src / thread-utils.h
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
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 #include "common.h"
11
12 /* Common operations even if threading has been disabled */
13 typedef struct {
14 #if defined(GIT_WIN32)
15 volatile long val;
16 #else
17 volatile int val;
18 #endif
19 } git_atomic;
20
21 GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
22 {
23 a->val = val;
24 }
25
26 #ifdef GIT_THREADS
27
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
36 #define git_mutex_init(a) pthread_mutex_init(a, NULL)
37 #define git_mutex_lock(a) pthread_mutex_lock(a)
38 #define git_mutex_unlock(a) pthread_mutex_unlock(a)
39 #define git_mutex_free(a) pthread_mutex_destroy(a)
40
41 /* Pthreads condition vars -- disabled by now */
42 #define git_cond unsigned int //pthread_cond_t
43 #define git_cond_init(c, a) (void)0 //pthread_cond_init(c, a)
44 #define git_cond_free(c) (void)0 //pthread_cond_destroy(c)
45 #define git_cond_wait(c, l) (void)0 //pthread_cond_wait(c, l)
46 #define git_cond_signal(c) (void)0 //pthread_cond_signal(c)
47 #define git_cond_broadcast(c) (void)0 //pthread_cond_broadcast(c)
48
49 GIT_INLINE(int) git_atomic_inc(git_atomic *a)
50 {
51 #if defined(GIT_WIN32)
52 return InterlockedIncrement(&a->val);
53 #elif defined(__GNUC__)
54 return __sync_add_and_fetch(&a->val, 1);
55 #else
56 # error "Unsupported architecture for atomic operations"
57 #endif
58 }
59
60 GIT_INLINE(int) git_atomic_dec(git_atomic *a)
61 {
62 #if defined(GIT_WIN32)
63 return InterlockedDecrement(&a->val);
64 #elif defined(__GNUC__)
65 return __sync_sub_and_fetch(&a->val, 1);
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
82 #define git_mutex_lock(a) (void)0
83 #define git_mutex_unlock(a) (void)0
84 #define git_mutex_free(a) (void)0
85
86 /* Pthreads condition vars */
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
94 GIT_INLINE(int) git_atomic_inc(git_atomic *a)
95 {
96 return ++a->val;
97 }
98
99 GIT_INLINE(int) git_atomic_dec(git_atomic *a)
100 {
101 return --a->val;
102 }
103
104 #endif
105
106 extern int git_online_cpus(void);
107
108 #endif /* INCLUDE_thread_utils_h__ */