libsync.h revision 9ac5b8b6
1/* 2 * sync abstraction 3 * Copyright 2015-2016 Collabora Ltd. 4 * 5 * Based on the implementation from the Android Open Source Project, 6 * 7 * Copyright 2012 Google, Inc 8 * 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: 15 * 16 * The above copyright notice and this permission notice shall be included in 17 * all copies or substantial portions of the Software. 18 * 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. 26 */ 27 28#ifndef _LIBSYNC_H 29#define _LIBSYNC_H 30 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> 38 39#if defined(__cplusplus) 40extern "C" { 41#endif 42 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. 48 */ 49struct sync_merge_data { 50 char name[32]; 51 int32_t fd2; 52 int32_t fence; 53 uint32_t flags; 54 uint32_t pad; 55}; 56#define SYNC_IOC_MAGIC '>' 57#define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data) 58#endif 59 60 61static inline int sync_wait(int fd, int timeout) 62{ 63 struct pollfd fds; 64 int ret; 65 66 memset(&fds, 0, sizeof(fds)); 67 fds.fd = fd; 68 fds.events = POLLIN; 69 fds.revents = 0; 70 71 do { 72 ret = poll(&fds, 1, timeout); 73 if (ret > 0) { 74 if (fds.revents & (POLLERR | POLLNVAL)) { 75 errno = EINVAL; 76 return -1; 77 } 78 return 0; 79 } else if (ret == 0) { 80 errno = ETIME; 81 return -1; 82 } 83 } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); 84 85 return ret; 86} 87 88static inline int sync_merge(const char *name, int fd1, int fd2) 89{ 90 struct sync_merge_data data; 91 int ret; 92 93 data.fd2 = fd2; 94 strncpy(data.name, name, sizeof(data.name)); 95 data.fence = 0; 96 data.flags = 0; 97 data.pad = 0; 98 99 do { 100 ret = ioctl(fd1, SYNC_IOC_MERGE, &data); 101 } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); 102 103 if (ret < 0) 104 return ret; 105 106 return data.fence; 107} 108 109/* accumulate fd2 into fd1. If *fd1 is not a valid fd then dup fd2, 110 * otherwise sync_merge() and close the old *fd1. This can be used 111 * to implement the pattern: 112 * 113 * init() 114 * { 115 * batch.fence_fd = -1; 116 * } 117 * 118 * // does *NOT* take ownership of fd 119 * server_sync(int fd) 120 * { 121 * if (sync_accumulate("foo", &batch.fence_fd, fd)) { 122 * ... error ... 123 * } 124 * } 125 */ 126static inline int sync_accumulate(const char *name, int *fd1, int fd2) 127{ 128 int ret; 129 130 assert(fd2 >= 0); 131 132 if (*fd1 < 0) { 133 *fd1 = dup(fd2); 134 return 0; 135 } 136 137 ret = sync_merge(name, *fd1, fd2); 138 if (ret < 0) { 139 /* leave *fd1 as it is */ 140 return ret; 141 } 142 143 close(*fd1); 144 *fd1 = ret; 145 146 return 0; 147} 148 149#if defined(__cplusplus) 150} 151#endif 152 153#endif 154