1
0

libsync.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #ifndef _LIBSYNC_H
  28. #define _LIBSYNC_H
  29. #include <assert.h>
  30. #include <errno.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <sys/ioctl.h>
  34. #include <poll.h>
  35. #include <unistd.h>
  36. #if defined(__cplusplus)
  37. extern "C" {
  38. #endif
  39. #ifndef SYNC_IOC_MERGE
  40. /* duplicated from linux/sync_file.h to avoid build-time dependency
  41. * on new (v4.7) kernel headers. Once distro's are mostly using
  42. * something newer than v4.7 drop this and #include <linux/sync_file.h>
  43. * instead.
  44. */
  45. struct sync_merge_data {
  46. char name[32];
  47. int32_t fd2;
  48. int32_t fence;
  49. uint32_t flags;
  50. uint32_t pad;
  51. };
  52. #define SYNC_IOC_MAGIC '>'
  53. #define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
  54. #endif
  55. static inline int sync_wait(int fd, int timeout)
  56. {
  57. struct pollfd fds = {0};
  58. int ret;
  59. fds.fd = fd;
  60. fds.events = POLLIN;
  61. do {
  62. ret = poll(&fds, 1, timeout);
  63. if (ret > 0) {
  64. if (fds.revents & (POLLERR | POLLNVAL)) {
  65. errno = EINVAL;
  66. return -1;
  67. }
  68. return 0;
  69. } else if (ret == 0) {
  70. errno = ETIME;
  71. return -1;
  72. }
  73. } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
  74. return ret;
  75. }
  76. static inline int sync_merge(const char *name, int fd1, int fd2)
  77. {
  78. struct sync_merge_data data = {0};
  79. int ret;
  80. data.fd2 = fd2;
  81. strncpy(data.name, name, sizeof(data.name));
  82. do {
  83. ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
  84. } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
  85. if (ret < 0)
  86. return ret;
  87. return data.fence;
  88. }
  89. /* accumulate fd2 into fd1. If *fd1 is not a valid fd then dup fd2,
  90. * otherwise sync_merge() and close the old *fd1. This can be used
  91. * to implement the pattern:
  92. *
  93. * init()
  94. * {
  95. * batch.fence_fd = -1;
  96. * }
  97. *
  98. * // does *NOT* take ownership of fd
  99. * server_sync(int fd)
  100. * {
  101. * if (sync_accumulate("foo", &batch.fence_fd, fd)) {
  102. * ... error ...
  103. * }
  104. * }
  105. */
  106. static inline int sync_accumulate(const char *name, int *fd1, int fd2)
  107. {
  108. int ret;
  109. assert(fd2 >= 0);
  110. if (*fd1 < 0) {
  111. *fd1 = dup(fd2);
  112. return 0;
  113. }
  114. ret = sync_merge(name, *fd1, fd2);
  115. if (ret < 0) {
  116. /* leave *fd1 as it is */
  117. return ret;
  118. }
  119. close(*fd1);
  120. *fd1 = ret;
  121. return 0;
  122. }
  123. #if defined(__cplusplus)
  124. }
  125. #endif
  126. #endif