libsync.h revision 9ac5b8b6
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{
639ac5b8b6Schristos	struct pollfd fds;
64037b3c26Smrg	int ret;
65037b3c26Smrg
669ac5b8b6Schristos	memset(&fds, 0, sizeof(fds));
67037b3c26Smrg	fds.fd = fd;
68037b3c26Smrg	fds.events = POLLIN;
699ac5b8b6Schristos	fds.revents = 0;
70037b3c26Smrg
71037b3c26Smrg	do {
72037b3c26Smrg		ret = poll(&fds, 1, timeout);
73037b3c26Smrg		if (ret > 0) {
74037b3c26Smrg			if (fds.revents & (POLLERR | POLLNVAL)) {
75037b3c26Smrg				errno = EINVAL;
76037b3c26Smrg				return -1;
77037b3c26Smrg			}
78037b3c26Smrg			return 0;
79037b3c26Smrg		} else if (ret == 0) {
80037b3c26Smrg			errno = ETIME;
81037b3c26Smrg			return -1;
82037b3c26Smrg		}
83037b3c26Smrg	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
84037b3c26Smrg
85037b3c26Smrg	return ret;
86037b3c26Smrg}
87037b3c26Smrg
88037b3c26Smrgstatic inline int sync_merge(const char *name, int fd1, int fd2)
89037b3c26Smrg{
909ac5b8b6Schristos	struct sync_merge_data data;
91037b3c26Smrg	int ret;
92037b3c26Smrg
93037b3c26Smrg	data.fd2 = fd2;
94037b3c26Smrg	strncpy(data.name, name, sizeof(data.name));
959ac5b8b6Schristos	data.fence = 0;
969ac5b8b6Schristos	data.flags = 0;
979ac5b8b6Schristos	data.pad = 0;
98037b3c26Smrg
99037b3c26Smrg	do {
100037b3c26Smrg		ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
101037b3c26Smrg	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
102037b3c26Smrg
103037b3c26Smrg	if (ret < 0)
104037b3c26Smrg		return ret;
105037b3c26Smrg
106037b3c26Smrg	return data.fence;
107037b3c26Smrg}
108037b3c26Smrg
109037b3c26Smrg/* accumulate fd2 into fd1.  If *fd1 is not a valid fd then dup fd2,
110037b3c26Smrg * otherwise sync_merge() and close the old *fd1.  This can be used
111037b3c26Smrg * to implement the pattern:
112037b3c26Smrg *
113037b3c26Smrg *    init()
114037b3c26Smrg *    {
115037b3c26Smrg *       batch.fence_fd = -1;
116037b3c26Smrg *    }
117037b3c26Smrg *
118037b3c26Smrg *    // does *NOT* take ownership of fd
119037b3c26Smrg *    server_sync(int fd)
120037b3c26Smrg *    {
121037b3c26Smrg *       if (sync_accumulate("foo", &batch.fence_fd, fd)) {
122037b3c26Smrg *          ... error ...
123037b3c26Smrg *       }
124037b3c26Smrg *    }
125037b3c26Smrg */
126037b3c26Smrgstatic inline int sync_accumulate(const char *name, int *fd1, int fd2)
127037b3c26Smrg{
128037b3c26Smrg	int ret;
129037b3c26Smrg
130037b3c26Smrg	assert(fd2 >= 0);
131037b3c26Smrg
132037b3c26Smrg	if (*fd1 < 0) {
133037b3c26Smrg		*fd1 = dup(fd2);
134037b3c26Smrg		return 0;
135037b3c26Smrg	}
136037b3c26Smrg
137037b3c26Smrg	ret = sync_merge(name, *fd1, fd2);
138037b3c26Smrg	if (ret < 0) {
139037b3c26Smrg		/* leave *fd1 as it is */
140037b3c26Smrg		return ret;
141037b3c26Smrg	}
142037b3c26Smrg
143037b3c26Smrg	close(*fd1);
144037b3c26Smrg	*fd1 = ret;
145037b3c26Smrg
146037b3c26Smrg	return 0;
147037b3c26Smrg}
148037b3c26Smrg
149037b3c26Smrg#if defined(__cplusplus)
150037b3c26Smrg}
151037b3c26Smrg#endif
152037b3c26Smrg
153037b3c26Smrg#endif
154