| @@ -15,33 +15,36 @@ | | | @@ -15,33 +15,36 @@ |
15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | | 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | | 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR | | 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | | 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | | 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | | 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
21 | * OF THIS SOFTWARE. | | 21 | * OF THIS SOFTWARE. |
22 | */ | | 22 | */ |
23 | | | 23 | |
24 | #if HAVE_CONFIG_H | | 24 | #if HAVE_CONFIG_H |
25 | #include "config.h" | | 25 | #include "config.h" |
26 | #endif | | 26 | #endif |
27 | | | 27 | |
28 | #include <fcntl.h> | | | |
29 | #include <string.h> | | | |
30 | #include <err.h> | | 28 | #include <err.h> |
| | | 29 | #include <errno.h> |
| | | 30 | #include <fcntl.h> |
| | | 31 | #include <stdio.h> |
31 | #include <unistd.h> | | 32 | #include <unistd.h> |
32 | | | 33 | |
33 | #include "xshmfenceint.h" | | 34 | #include "xshmfenceint.h" |
34 | | | 35 | |
| | | 36 | static sem_t *mksemtemp(char *, const char *); |
| | | 37 | |
35 | #define LOCK() do {} while (sem_wait(f->lock) != 0) | | 38 | #define LOCK() do {} while (sem_wait(f->lock) != 0) |
36 | #define UNLOCK() do { sem_post(f->lock); } while (0) | | 39 | #define UNLOCK() do { sem_post(f->lock); } while (0) |
37 | #define COND_WAIT() do {} while (sem_wait(f->cond) != 0) | | 40 | #define COND_WAIT() do {} while (sem_wait(f->cond) != 0) |
38 | #define COND_SIGNAL() do { sem_post(f->cond); } while (0) | | 41 | #define COND_SIGNAL() do { sem_post(f->cond); } while (0) |
39 | | | 42 | |
40 | /** | | 43 | /** |
41 | * xshmfence_trigger: | | 44 | * xshmfence_trigger: |
42 | * @f: An X fence | | 45 | * @f: An X fence |
43 | * | | 46 | * |
44 | * Set @f to triggered, waking all waiters. | | 47 | * Set @f to triggered, waking all waiters. |
45 | * | | 48 | * |
46 | * Return value: 0 on success and -1 on error (in which case, errno | | 49 | * Return value: 0 on success and -1 on error (in which case, errno |
47 | * will be set as appropriate). | | 50 | * will be set as appropriate). |
| @@ -145,36 +148,32 @@ xshmfence_reset(struct xshmfence *f) { | | | @@ -145,36 +148,32 @@ xshmfence_reset(struct xshmfence *f) { |
145 | * Initialize the fence when first allocated | | 148 | * Initialize the fence when first allocated |
146 | **/ | | 149 | **/ |
147 | void | | 150 | void |
148 | xshmfence_init(int fd) | | 151 | xshmfence_init(int fd) |
149 | { | | 152 | { |
150 | sem_t *lock; | | 153 | sem_t *lock; |
151 | sem_t *cond; | | 154 | sem_t *cond; |
152 | struct xshmfence f; | | 155 | struct xshmfence f; |
153 | | | 156 | |
154 | __sync_fetch_and_and(&f.refcnt, 0); | | 157 | __sync_fetch_and_and(&f.refcnt, 0); |
155 | __sync_fetch_and_and(&f.triggered, 0); | | 158 | __sync_fetch_and_and(&f.triggered, 0); |
156 | __sync_fetch_and_and(&f.waiting, 0); | | 159 | __sync_fetch_and_and(&f.waiting, 0); |
157 | | | 160 | |
158 | strlcpy(f.lockname, "/xshmfl-XXXX", sizeof(f.lockname)); | | 161 | lock = mksemtemp(f.lockname, "/xshmfl-%i"); |
159 | mktemp(f.lockname); | | | |
160 | lock = sem_open(f.lockname, O_CREAT|O_EXCL, 0600, 1); | | | |
161 | if (lock == SEM_FAILED) { | | 162 | if (lock == SEM_FAILED) { |
162 | err(EXIT_FAILURE, "xshmfence_init: sem_open"); | | 163 | err(EXIT_FAILURE, "xshmfence_init: sem_open"); |
163 | } | | 164 | } |
164 | | | 165 | |
165 | strlcpy(f.condname, "/xshmfc-XXXX", sizeof(f.condname)); | | 166 | cond = mksemtemp(f.condname, "/xshmfl-%i"); |
166 | mktemp(f.condname); | | | |
167 | cond = sem_open(f.condname, O_CREAT|O_EXCL, 0600, 0); | | | |
168 | if (cond == SEM_FAILED) { | | 167 | if (cond == SEM_FAILED) { |
169 | err(EXIT_FAILURE, "xshmfence_init: sem_open"); | | 168 | err(EXIT_FAILURE, "xshmfence_init: sem_open"); |
170 | } | | 169 | } |
171 | | | 170 | |
172 | sem_close(lock); | | 171 | sem_close(lock); |
173 | sem_close(cond); | | 172 | sem_close(cond); |
174 | | | 173 | |
175 | pwrite(fd, &f, sizeof(f), 0); | | 174 | pwrite(fd, &f, sizeof(f), 0); |
176 | } | | 175 | } |
177 | | | 176 | |
178 | /** | | 177 | /** |
179 | * xshmfence_open_semaphore: | | 178 | * xshmfence_open_semaphore: |
180 | * @f: An X fence | | 179 | * @f: An X fence |
| @@ -211,13 +210,32 @@ xshmfence_open_semaphore(struct xshmfenc | | | @@ -211,13 +210,32 @@ xshmfence_open_semaphore(struct xshmfenc |
211 | * | | 210 | * |
212 | * Close the semaphore before unmapping the fence | | 211 | * Close the semaphore before unmapping the fence |
213 | **/ | | 212 | **/ |
214 | void | | 213 | void |
215 | xshmfence_close_semaphore(struct xshmfence *f) | | 214 | xshmfence_close_semaphore(struct xshmfence *f) |
216 | { | | 215 | { |
217 | sem_close(f->lock); | | 216 | sem_close(f->lock); |
218 | sem_close(f->cond); | | 217 | sem_close(f->cond); |
219 | if (__sync_sub_and_fetch(&f->refcnt, 1) == 0) { | | 218 | if (__sync_sub_and_fetch(&f->refcnt, 1) == 0) { |
220 | sem_unlink(f->lockname); | | 219 | sem_unlink(f->lockname); |
221 | sem_unlink(f->condname); | | 220 | sem_unlink(f->condname); |
222 | } | | 221 | } |
223 | } | | 222 | } |
| | | 223 | |
| | | 224 | static sem_t * |
| | | 225 | mksemtemp(char *name, const char *template) |
| | | 226 | { |
| | | 227 | sem_t *ret; |
| | | 228 | pid_t p; |
| | | 229 | p = getpid(); |
| | | 230 | for(;;) { |
| | | 231 | sprintf(name, template, p); |
| | | 232 | ret = sem_open(name, O_CREAT|O_EXCL, 0600, 1); |
| | | 233 | if (ret == SEM_FAILED) { |
| | | 234 | if (errno == EEXIST) { |
| | | 235 | p++; |
| | | 236 | continue; |
| | | 237 | } |
| | | 238 | } |
| | | 239 | return ret; |
| | | 240 | } |
| | | 241 | } |