2 * Copyright © 2013 Keith Packard
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 #include "xshmfenceint.h"
33 * Set @f to triggered, waking all waiters.
35 * Return value: 0 on success and -1 on error (in which case, errno
36 * will be set as appropriate).
39 xshmfence_trigger(struct xshmfence *f) {
40 pthread_mutex_lock(&f->lock);
45 pthread_cond_broadcast(&f->wakeup);
48 pthread_mutex_unlock(&f->lock);
56 * Wait for @f to be triggered. If @f is already triggered, this
57 * function returns immediately.
59 * Return value: 0 on success and -1 on error (in which case, errno
60 * will be set as appropriate).
63 xshmfence_await(struct xshmfence *f) {
64 pthread_mutex_lock(&f->lock);
65 while (f->value == 0) {
67 pthread_cond_wait(&f->wakeup, &f->lock);
69 pthread_mutex_unlock(&f->lock);
77 * Return value: 1 if @f is triggered, else returns 0.
80 xshmfence_query(struct xshmfence *f) {
83 pthread_mutex_lock(&f->lock);
85 pthread_mutex_unlock(&f->lock);
93 * Reset @f to untriggered. If @f is already untriggered,
94 * this function has no effect.
97 xshmfence_reset(struct xshmfence *f) {
99 pthread_mutex_lock(&f->lock);
101 pthread_mutex_unlock(&f->lock);
106 * @fd: An fd for an X fence
108 * Initialize the fence when first allocated
112 xshmfence_init(int fd)
114 struct xshmfence *f = xshmfence_map_shm(fd);
115 pthread_mutexattr_t mutex_attr;
116 pthread_condattr_t cond_attr;
121 pthread_mutexattr_init(&mutex_attr);
122 pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);
123 pthread_mutex_init(&f->lock, &mutex_attr);
125 pthread_condattr_init(&cond_attr);
126 pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
127 pthread_cond_init(&f->wakeup, &cond_attr);
130 xshmfence_unmap_shm(f);