File indexing completed on 2025-01-17 09:55:47
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #ifndef _LIBSYNC_H
0029 #define _LIBSYNC_H
0030
0031 #include <assert.h>
0032 #include <errno.h>
0033 #include <stdint.h>
0034 #include <string.h>
0035 #include <sys/ioctl.h>
0036 #include <poll.h>
0037 #include <unistd.h>
0038
0039 #if defined(__cplusplus)
0040 extern "C" {
0041 #endif
0042
0043 #ifndef SYNC_IOC_MERGE
0044
0045
0046
0047
0048
0049 struct sync_merge_data {
0050 char name[32];
0051 int32_t fd2;
0052 int32_t fence;
0053 uint32_t flags;
0054 uint32_t pad;
0055 };
0056 #define SYNC_IOC_MAGIC '>'
0057 #define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
0058 #endif
0059
0060
0061 static inline int sync_wait(int fd, int timeout)
0062 {
0063 struct pollfd fds = {0};
0064 int ret;
0065
0066 fds.fd = fd;
0067 fds.events = POLLIN;
0068
0069 do {
0070 ret = poll(&fds, 1, timeout);
0071 if (ret > 0) {
0072 if (fds.revents & (POLLERR | POLLNVAL)) {
0073 errno = EINVAL;
0074 return -1;
0075 }
0076 return 0;
0077 } else if (ret == 0) {
0078 errno = ETIME;
0079 return -1;
0080 }
0081 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
0082
0083 return ret;
0084 }
0085
0086 static inline int sync_merge(const char *name, int fd1, int fd2)
0087 {
0088 struct sync_merge_data data = {0};
0089 int ret;
0090
0091 data.fd2 = fd2;
0092 strncpy(data.name, name, sizeof(data.name));
0093
0094 do {
0095 ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
0096 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
0097
0098 if (ret < 0)
0099 return ret;
0100
0101 return data.fence;
0102 }
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121 static inline int sync_accumulate(const char *name, int *fd1, int fd2)
0122 {
0123 int ret;
0124
0125 assert(fd2 >= 0);
0126
0127 if (*fd1 < 0) {
0128 *fd1 = dup(fd2);
0129 return 0;
0130 }
0131
0132 ret = sync_merge(name, *fd1, fd2);
0133 if (ret < 0) {
0134
0135 return ret;
0136 }
0137
0138 close(*fd1);
0139 *fd1 = ret;
0140
0141 return 0;
0142 }
0143
0144 #if defined(__cplusplus)
0145 }
0146 #endif
0147
0148 #endif