libsync.h revision 037b3c26
1037b3c26Smrg/* 2037b3c26Smrg * sync abstraction 3037b3c26Smrg * Copyright 2015-2016 Collabora Ltd. 4037b3c26Smrg * 5037b3c26Smrg * Based on the implementation from the Android Open Source Project, 6037b3c26Smrg * 7037b3c26Smrg * Copyright 2012 Google, Inc 8037b3c26Smrg * 9037b3c26Smrg * Permission is hereby granted, free of charge, to any person obtaining a 10037b3c26Smrg * copy of this software and associated documentation files (the "Software"), 11037b3c26Smrg * to deal in the Software without restriction, including without limitation 12037b3c26Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13037b3c26Smrg * and/or sell copies of the Software, and to permit persons to whom the 14037b3c26Smrg * Software is furnished to do so, subject to the following conditions: 15037b3c26Smrg * 16037b3c26Smrg * The above copyright notice and this permission notice shall be included in 17037b3c26Smrg * all copies or substantial portions of the Software. 18037b3c26Smrg * 19037b3c26Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20037b3c26Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21037b3c26Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22037b3c26Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 23037b3c26Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 24037b3c26Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25037b3c26Smrg * OTHER DEALINGS IN THE SOFTWARE. 26037b3c26Smrg */ 27037b3c26Smrg 28037b3c26Smrg#ifndef _LIBSYNC_H 29037b3c26Smrg#define _LIBSYNC_H 30037b3c26Smrg 31037b3c26Smrg#include <assert.h> 32037b3c26Smrg#include <errno.h> 33037b3c26Smrg#include <stdint.h> 34037b3c26Smrg#include <string.h> 35037b3c26Smrg#include <sys/ioctl.h> 36037b3c26Smrg#include <sys/poll.h> 37037b3c26Smrg#include <unistd.h> 38037b3c26Smrg 39037b3c26Smrg#if defined(__cplusplus) 40037b3c26Smrgextern "C" { 41037b3c26Smrg#endif 42037b3c26Smrg 43037b3c26Smrg#ifndef SYNC_IOC_MERGE 44037b3c26Smrg/* duplicated from linux/sync_file.h to avoid build-time dependency 45037b3c26Smrg * on new (v4.7) kernel headers. Once distro's are mostly using 46037b3c26Smrg * something newer than v4.7 drop this and #include <linux/sync_file.h> 47037b3c26Smrg * instead. 48037b3c26Smrg */ 49037b3c26Smrgstruct sync_merge_data { 50037b3c26Smrg char name[32]; 51037b3c26Smrg int32_t fd2; 52037b3c26Smrg int32_t fence; 53037b3c26Smrg uint32_t flags; 54037b3c26Smrg uint32_t pad; 55037b3c26Smrg}; 56037b3c26Smrg#define SYNC_IOC_MAGIC '>' 57037b3c26Smrg#define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data) 58037b3c26Smrg#endif 59037b3c26Smrg 60037b3c26Smrg 61037b3c26Smrgstatic inline int sync_wait(int fd, int timeout) 62037b3c26Smrg{ 63037b3c26Smrg struct pollfd fds = {0}; 64037b3c26Smrg int ret; 65037b3c26Smrg 66037b3c26Smrg fds.fd = fd; 67037b3c26Smrg fds.events = POLLIN; 68037b3c26Smrg 69037b3c26Smrg do { 70037b3c26Smrg ret = poll(&fds, 1, timeout); 71037b3c26Smrg if (ret > 0) { 72037b3c26Smrg if (fds.revents & (POLLERR | POLLNVAL)) { 73037b3c26Smrg errno = EINVAL; 74037b3c26Smrg return -1; 75037b3c26Smrg } 76037b3c26Smrg return 0; 77037b3c26Smrg } else if (ret == 0) { 78037b3c26Smrg errno = ETIME; 79037b3c26Smrg return -1; 80037b3c26Smrg } 81037b3c26Smrg } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); 82037b3c26Smrg 83037b3c26Smrg return ret; 84037b3c26Smrg} 85037b3c26Smrg 86037b3c26Smrgstatic inline int sync_merge(const char *name, int fd1, int fd2) 87037b3c26Smrg{ 88037b3c26Smrg struct sync_merge_data data = {0}; 89037b3c26Smrg int ret; 90037b3c26Smrg 91037b3c26Smrg data.fd2 = fd2; 92037b3c26Smrg strncpy(data.name, name, sizeof(data.name)); 93037b3c26Smrg 94037b3c26Smrg do { 95037b3c26Smrg ret = ioctl(fd1, SYNC_IOC_MERGE, &data); 96037b3c26Smrg } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); 97037b3c26Smrg 98037b3c26Smrg if (ret < 0) 99037b3c26Smrg return ret; 100037b3c26Smrg 101037b3c26Smrg return data.fence; 102037b3c26Smrg} 103037b3c26Smrg 104037b3c26Smrg/* accumulate fd2 into fd1. If *fd1 is not a valid fd then dup fd2, 105037b3c26Smrg * otherwise sync_merge() and close the old *fd1. This can be used 106037b3c26Smrg * to implement the pattern: 107037b3c26Smrg * 108037b3c26Smrg * init() 109037b3c26Smrg * { 110037b3c26Smrg * batch.fence_fd = -1; 111037b3c26Smrg * } 112037b3c26Smrg * 113037b3c26Smrg * // does *NOT* take ownership of fd 114037b3c26Smrg * server_sync(int fd) 115037b3c26Smrg * { 116037b3c26Smrg * if (sync_accumulate("foo", &batch.fence_fd, fd)) { 117037b3c26Smrg * ... error ... 118037b3c26Smrg * } 119037b3c26Smrg * } 120037b3c26Smrg */ 121037b3c26Smrgstatic inline int sync_accumulate(const char *name, int *fd1, int fd2) 122037b3c26Smrg{ 123037b3c26Smrg int ret; 124037b3c26Smrg 125037b3c26Smrg assert(fd2 >= 0); 126037b3c26Smrg 127037b3c26Smrg if (*fd1 < 0) { 128037b3c26Smrg *fd1 = dup(fd2); 129037b3c26Smrg return 0; 130037b3c26Smrg } 131037b3c26Smrg 132037b3c26Smrg ret = sync_merge(name, *fd1, fd2); 133037b3c26Smrg if (ret < 0) { 134037b3c26Smrg /* leave *fd1 as it is */ 135037b3c26Smrg return ret; 136037b3c26Smrg } 137037b3c26Smrg 138037b3c26Smrg close(*fd1); 139037b3c26Smrg *fd1 = ret; 140037b3c26Smrg 141037b3c26Smrg return 0; 142037b3c26Smrg} 143037b3c26Smrg 144037b3c26Smrg#if defined(__cplusplus) 145037b3c26Smrg} 146037b3c26Smrg#endif 147037b3c26Smrg 148037b3c26Smrg#endif 149