This source file includes following definitions.
- init_atomic_int
- get_atomic_int_value
- set_atomic_int_value
- init_atomic_int
- init_atomic_int
- get_atomic_int_value
- set_atomic_int_value
- init_atomic_int
- get_atomic_int_value
- set_atomic_int_value
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #define USE_VOLATILE 0
25
26 #if USE_POSIX_THREADS && HAVE_SEMAPHORE_H
27
28
29
30 # define USE_SEMAPHORE 1
31
32
33 # if defined __APPLE__ && defined __MACH__
34 # define USE_NAMED_SEMAPHORE 1
35 # else
36 # define USE_UNNAMED_SEMAPHORE 1
37 # endif
38 #endif
39
40
41 #if USE_SEMAPHORE
42 # include <errno.h>
43 # include <fcntl.h>
44 # include <semaphore.h>
45 # include <unistd.h>
46 #endif
47
48
49 #if USE_VOLATILE
50 struct atomic_int {
51 volatile int value;
52 };
53 static void
54 init_atomic_int (struct atomic_int *ai)
55 {
56 }
57 static int
58 get_atomic_int_value (struct atomic_int *ai)
59 {
60 return ai->value;
61 }
62 static void
63 set_atomic_int_value (struct atomic_int *ai, int new_value)
64 {
65 ai->value = new_value;
66 }
67 #elif USE_SEMAPHORE
68
69
70 # if USE_UNNAMED_SEMAPHORE
71 struct atomic_int {
72 sem_t semaphore;
73 };
74 #define atomic_int_semaphore(ai) (&(ai)->semaphore)
75 static void
76 init_atomic_int (struct atomic_int *ai)
77 {
78 sem_init (&ai->semaphore, 0, 0);
79 }
80 # endif
81 # if USE_NAMED_SEMAPHORE
82 struct atomic_int {
83 sem_t *semaphore;
84 };
85 #define atomic_int_semaphore(ai) ((ai)->semaphore)
86 static void
87 init_atomic_int (struct atomic_int *ai)
88 {
89 sem_t *s;
90 unsigned int count;
91 for (count = 0; ; count++)
92 {
93 char name[80];
94
95
96
97
98
99 sprintf (name, "test-lock-%lu-%p-%u",
100 (unsigned long) getpid (), ai, count);
101 s = sem_open (name, O_CREAT | O_EXCL, 0600, 0);
102 if (s == SEM_FAILED)
103 {
104 if (errno == EEXIST)
105
106 continue;
107 else
108 {
109 perror ("sem_open failed");
110 abort ();
111 }
112 }
113 else
114 {
115
116
117 sem_unlink (name);
118 break;
119 }
120 }
121 ai->semaphore = s;
122 }
123 # endif
124 static int
125 get_atomic_int_value (struct atomic_int *ai)
126 {
127 if (sem_trywait (atomic_int_semaphore (ai)) == 0)
128 {
129 if (sem_post (atomic_int_semaphore (ai)))
130 abort ();
131 return 1;
132 }
133 else if (errno == EAGAIN)
134 return 0;
135 else
136 abort ();
137 }
138 static void
139 set_atomic_int_value (struct atomic_int *ai, int new_value)
140 {
141 if (new_value == 0)
142
143 return;
144
145 if (sem_post (atomic_int_semaphore (ai)))
146 abort ();
147 }
148 #else
149 struct atomic_int {
150 pthread_mutex_t lock;
151 int value;
152 };
153 static void
154 init_atomic_int (struct atomic_int *ai)
155 {
156 pthread_mutexattr_t attr;
157
158 ASSERT (pthread_mutexattr_init (&attr) == 0);
159 ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_NORMAL) == 0);
160 ASSERT (pthread_mutex_init (&ai->lock, &attr) == 0);
161 ASSERT (pthread_mutexattr_destroy (&attr) == 0);
162 }
163 static int
164 get_atomic_int_value (struct atomic_int *ai)
165 {
166 ASSERT (pthread_mutex_lock (&ai->lock) == 0);
167 int ret = ai->value;
168 ASSERT (pthread_mutex_unlock (&ai->lock) == 0);
169 return ret;
170 }
171 static void
172 set_atomic_int_value (struct atomic_int *ai, int new_value)
173 {
174 ASSERT (pthread_mutex_lock (&ai->lock) == 0);
175 ai->value = new_value;
176 ASSERT (pthread_mutex_unlock (&ai->lock) == 0);
177 }
178 #endif