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 <xshmfence.h>
30 #include <sys/types.h>
33 #define NCHILD 5 /* number of child processes to fork */
34 #define NCHECK 10 /* number of times to signal the fence */
36 /* Catch an alarm and bail
41 write(2, "caught alarm\n", 13);
46 main(int argc, char **argv)
58 fd = xshmfence_alloc_shm();
60 perror("xshmfence_alloc_shm");
64 /* fork NCHILD processes to wait for the fence
66 for (c = 0; c < NCHILD; c++) {
73 /* Set an alarm to limit how long
76 signal(SIGALRM, sigalrm);
81 x = xshmfence_map_shm(fd);
83 fprintf(stderr, "%6d: ", c);
84 perror("xshmfence_map_shm");
88 for (i = 0; i < NCHECK; i++) {
90 /* Verify that the fence is currently reset
92 if (xshmfence_query(x) != 0) {
93 fprintf(stderr, "%6d: query reset failed\n", c);
99 fprintf(stderr, "%6d: waiting\n", c);
100 if (xshmfence_await(x) < 0) {
101 fprintf(stderr, "%6d: ", c);
102 perror("xshmfence_await");
106 fprintf(stderr, "%6d: awoken\n", c);
108 /* Verify that the fence is currently triggered
110 if (xshmfence_query(x) == 0) {
111 fprintf(stderr, "%6d: query triggered failed\n", c);
124 fprintf(stderr, "%6d: done\n", c);
129 /* Map the fence into the parent process
131 x = xshmfence_map_shm(fd);
133 perror("xshmfence_map_shm");
137 for (i = 0; i < NCHECK; i++) {
139 fprintf(stderr, "trigger\n");
141 /* Verify that the fence is reset
143 if (xshmfence_query(x) != 0) {
144 fprintf(stderr, "query reset failed\n");
150 if (xshmfence_trigger(x) < 0) {
151 perror("xshmfence_trigger");
155 /* Verify that the fence is triggered
157 if (xshmfence_query(x) == 0) {
158 fprintf (stderr, "query triggered failed\n");
162 fprintf(stderr, "trigger done\n");
165 /* Reap all of the child processes
167 for (c = 0; c < NCHILD; c++) {
173 fprintf(stderr, "child %d done %d\n", pid, status);