Conditionally include <linux/limits.h> and <sys/params.h> on Linux, BSD
[drm/libdrm.git] / libsync.h
blobf1a2f96d36bc7f95d5b276e72b300e34fcf1d959
1 /*
2 * sync abstraction
3 * Copyright 2015-2016 Collabora Ltd.
5 * Based on the implementation from the Android Open Source Project,
7 * Copyright 2012 Google, Inc
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 #ifndef _LIBSYNC_H
29 #define _LIBSYNC_H
31 #include <assert.h>
32 #include <errno.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <sys/ioctl.h>
36 #include <sys/poll.h>
37 #include <unistd.h>
39 #if defined(__cplusplus)
40 extern "C" {
41 #endif
43 #ifndef SYNC_IOC_MERGE
44 /* duplicated from linux/sync_file.h to avoid build-time dependency
45 * on new (v4.7) kernel headers. Once distro's are mostly using
46 * something newer than v4.7 drop this and #include <linux/sync_file.h>
47 * instead.
49 struct sync_merge_data {
50 char name[32];
51 int32_t fd2;
52 int32_t fence;
53 uint32_t flags;
54 uint32_t pad;
56 #define SYNC_IOC_MAGIC '>'
57 #define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
58 #endif
61 static inline int sync_wait(int fd, int timeout)
63 struct pollfd fds = {0};
64 int ret;
66 fds.fd = fd;
67 fds.events = POLLIN;
69 do {
70 ret = poll(&fds, 1, timeout);
71 if (ret > 0) {
72 if (fds.revents & (POLLERR | POLLNVAL)) {
73 errno = EINVAL;
74 return -1;
76 return 0;
77 } else if (ret == 0) {
78 errno = ETIME;
79 return -1;
81 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
83 return ret;
86 static inline int sync_merge(const char *name, int fd1, int fd2)
88 struct sync_merge_data data = {0};
89 int ret;
91 data.fd2 = fd2;
92 strncpy(data.name, name, sizeof(data.name));
94 do {
95 ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
96 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
98 if (ret < 0)
99 return ret;
101 return data.fence;
104 /* accumulate fd2 into fd1. If *fd1 is not a valid fd then dup fd2,
105 * otherwise sync_merge() and close the old *fd1. This can be used
106 * to implement the pattern:
108 * init()
110 * batch.fence_fd = -1;
113 * // does *NOT* take ownership of fd
114 * server_sync(int fd)
116 * if (sync_accumulate("foo", &batch.fence_fd, fd)) {
117 * ... error ...
121 static inline int sync_accumulate(const char *name, int *fd1, int fd2)
123 int ret;
125 assert(fd2 >= 0);
127 if (*fd1 < 0) {
128 *fd1 = dup(fd2);
129 return 0;
132 ret = sync_merge(name, *fd1, fd2);
133 if (ret < 0) {
134 /* leave *fd1 as it is */
135 return ret;
138 close(*fd1);
139 *fd1 = ret;
141 return 0;
144 #if defined(__cplusplus)
146 #endif
148 #endif