Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-17 09:55:47

0001 /*
0002  *  sync abstraction
0003  *  Copyright 2015-2016 Collabora Ltd.
0004  *
0005  *  Based on the implementation from the Android Open Source Project,
0006  *
0007  *  Copyright 2012 Google, Inc
0008  *
0009  *  Permission is hereby granted, free of charge, to any person obtaining a
0010  *  copy of this software and associated documentation files (the "Software"),
0011  *  to deal in the Software without restriction, including without limitation
0012  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
0013  *  and/or sell copies of the Software, and to permit persons to whom the
0014  *  Software is furnished to do so, subject to the following conditions:
0015  *
0016  *  The above copyright notice and this permission notice shall be included in
0017  *  all copies or substantial portions of the Software.
0018  *
0019  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0020  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0021  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
0022  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
0023  *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0024  *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0025  *  OTHER DEALINGS IN THE SOFTWARE.
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 /* duplicated from linux/sync_file.h to avoid build-time dependency
0045  * on new (v4.7) kernel headers.  Once distro's are mostly using
0046  * something newer than v4.7 drop this and #include <linux/sync_file.h>
0047  * instead.
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 /* accumulate fd2 into fd1.  If *fd1 is not a valid fd then dup fd2,
0105  * otherwise sync_merge() and close the old *fd1.  This can be used
0106  * to implement the pattern:
0107  *
0108  *    init()
0109  *    {
0110  *       batch.fence_fd = -1;
0111  *    }
0112  *
0113  *    // does *NOT* take ownership of fd
0114  *    server_sync(int fd)
0115  *    {
0116  *       if (sync_accumulate("foo", &batch.fence_fd, fd)) {
0117  *          ... error ...
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         /* leave *fd1 as it is */
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