db_object.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_object.c
  15. *
  16. * This file contains APIs for closing or polling PAL handles.
  17. */
  18. #include "pal_defs.h"
  19. #include "pal_linux_defs.h"
  20. #include "pal.h"
  21. #include "pal_internal.h"
  22. #include "pal_linux.h"
  23. #include "pal_error.h"
  24. #include "pal_debug.h"
  25. #include "api.h"
  26. #include <linux/time.h>
  27. #include <linux/poll.h>
  28. #include <linux/wait.h>
  29. #include <atomic.h>
  30. #include <asm/errno.h>
  31. #define DEFAULT_QUANTUM 500
  32. /* internally to wait for one object. Also used as a shortcut to wait
  33. * on events and semaphores.
  34. *
  35. * Returns 0 on success, negative value on failure (e.g., -PAL_ERROR_TRYAGAIN)
  36. */
  37. static int _DkObjectWaitOne(PAL_HANDLE handle, int64_t timeout_us) {
  38. /* only for all these handle which has a file descriptor, or
  39. a eventfd. events and semaphores will skip this part */
  40. if (HANDLE_HDR(handle)->flags & HAS_FDS) {
  41. struct timespec timeout_ts;
  42. if (timeout_us >= 0) {
  43. int64_t sec = timeout_us / 1000000;
  44. int64_t microsec = timeout_us - (sec * 1000000);
  45. timeout_ts.tv_sec = sec;
  46. timeout_ts.tv_nsec = microsec * 1000;
  47. }
  48. struct pollfd fds[MAX_FDS];
  49. int off[MAX_FDS];
  50. int nfds = 0;
  51. for (int i = 0 ; i < MAX_FDS ; i++) {
  52. int events = 0;
  53. if ((HANDLE_HDR(handle)->flags & RFD(i)) &&
  54. !(HANDLE_HDR(handle)->flags & ERROR(i)))
  55. events |= POLLIN;
  56. if ((HANDLE_HDR(handle)->flags & WFD(i)) &&
  57. !(HANDLE_HDR(handle)->flags & WRITABLE(i)) &&
  58. !(HANDLE_HDR(handle)->flags & ERROR(i)))
  59. events |= POLLOUT;
  60. if (events) {
  61. fds[nfds].fd = handle->generic.fds[i];
  62. fds[nfds].events = events|POLLHUP|POLLERR;
  63. fds[nfds].revents = 0;
  64. off[nfds] = i;
  65. nfds++;
  66. }
  67. }
  68. if (!nfds)
  69. return -PAL_ERROR_TRYAGAIN;
  70. int ret = INLINE_SYSCALL(ppoll, 5, &fds, nfds,
  71. timeout_us >= 0 ? &timeout_ts : NULL,
  72. NULL, 0);
  73. if (IS_ERR(ret))
  74. switch (ERRNO(ret)) {
  75. case EINTR:
  76. case ERESTART:
  77. return -PAL_ERROR_INTERRUPTED;
  78. default:
  79. return unix_to_pal_error(ERRNO(ret));
  80. }
  81. if (!ret)
  82. return -PAL_ERROR_TRYAGAIN;
  83. for (int i = 0 ; i < nfds ; i++) {
  84. if (!fds[i].revents)
  85. continue;
  86. if (fds[i].revents & POLLOUT)
  87. HANDLE_HDR(handle)->flags |= WRITABLE(off[i]);
  88. if (fds[i].revents & (POLLHUP|POLLERR))
  89. HANDLE_HDR(handle)->flags |= ERROR(off[i]);
  90. }
  91. return 0;
  92. }
  93. const struct handle_ops * ops = HANDLE_OPS(handle);
  94. if (!ops)
  95. return -PAL_ERROR_BADHANDLE;
  96. if (!ops->wait)
  97. return -PAL_ERROR_NOTSUPPORT;
  98. return ops->wait(handle, timeout_us);
  99. }
  100. /* _DkObjectsWaitAny for internal use. The function wait for any of the handle
  101. in the handle array. timeout can be set for the wait. */
  102. int _DkObjectsWaitAny(int count, PAL_HANDLE* handleArray, int64_t timeout_us,
  103. PAL_HANDLE* polled) {
  104. if (count <= 0)
  105. return 0;
  106. if (count == 1) {
  107. // It is possible to have NULL pointers in the handle array.
  108. // In this case, assume nothing is polled.
  109. if (!handleArray[0])
  110. return -PAL_ERROR_TRYAGAIN;
  111. int rv = _DkObjectWaitOne(handleArray[0], timeout_us);
  112. if (rv == 0)
  113. *polled = handleArray[0];
  114. return rv;
  115. }
  116. int i, j, ret, maxfds = 0, nfds = 0;
  117. /* we are not gonna to allow any polling on muliple synchronous
  118. objects, doing this is simply violating the division of
  119. labor between PAL and library OS */
  120. for (i = 0 ; i < count ; i++) {
  121. PAL_HANDLE hdl = handleArray[i];
  122. if (!hdl)
  123. continue;
  124. if (!(HANDLE_HDR(hdl)->flags & HAS_FDS))
  125. return -PAL_ERROR_NOTSUPPORT;
  126. /* eliminate repeated entries */
  127. for (j = 0 ; j < i ; j++)
  128. if (hdl == handleArray[j])
  129. break;
  130. if (j == i) {
  131. for (j = 0 ; j < MAX_FDS ; j++)
  132. if (HANDLE_HDR(hdl)->flags & (RFD(j)|WFD(j)))
  133. maxfds++;
  134. }
  135. }
  136. struct pollfd * fds = __alloca(sizeof(struct pollfd) * maxfds);
  137. PAL_HANDLE * hdls = __alloca(sizeof(PAL_HANDLE) * maxfds);
  138. for (i = 0 ; i < count ; i++) {
  139. PAL_HANDLE hdl = handleArray[i];
  140. if (!hdl)
  141. continue;
  142. for (j = 0 ; j < i ; j++)
  143. if (hdl == handleArray[j])
  144. break;
  145. if (j < i)
  146. continue;
  147. for (j = 0 ; j < MAX_FDS ; j++) {
  148. int events = 0;
  149. if ((HANDLE_HDR(hdl)->flags & RFD(j)) &&
  150. !(HANDLE_HDR(hdl)->flags & ERROR(j)))
  151. events |= POLLIN;
  152. if ((HANDLE_HDR(hdl)->flags & WFD(j)) &&
  153. !(HANDLE_HDR(hdl)->flags & WRITABLE(j)) &&
  154. !(HANDLE_HDR(hdl)->flags & ERROR(j)))
  155. events |= POLLOUT;
  156. if (events && hdl->generic.fds[j] != PAL_IDX_POISON) {
  157. fds[nfds].fd = hdl->generic.fds[j];
  158. fds[nfds].events = events|POLLHUP|POLLERR;
  159. fds[nfds].revents = 0;
  160. hdls[nfds] = hdl;
  161. nfds++;
  162. }
  163. }
  164. }
  165. if (!nfds)
  166. return -PAL_ERROR_TRYAGAIN;
  167. struct timespec timeout_ts;
  168. if (timeout_us >= 0) {
  169. int64_t sec = timeout_us / 1000000;
  170. int64_t microsec = timeout_us - (sec * 1000000);
  171. timeout_ts.tv_sec = sec;
  172. timeout_ts.tv_nsec = microsec * 1000;
  173. }
  174. ret = INLINE_SYSCALL(ppoll, 5, fds, nfds,
  175. timeout_us >= 0 ? &timeout_ts : NULL,
  176. NULL, 0);
  177. if (IS_ERR(ret))
  178. switch (ERRNO(ret)) {
  179. case EINTR:
  180. case ERESTART:
  181. return -PAL_ERROR_INTERRUPTED;
  182. default:
  183. return unix_to_pal_error(ERRNO(ret));
  184. }
  185. if (!ret)
  186. return -PAL_ERROR_TRYAGAIN;
  187. PAL_HANDLE polled_hdl = NULL;
  188. for (i = 0 ; i < nfds ; i++) {
  189. if (!fds[i].revents)
  190. continue;
  191. PAL_HANDLE hdl = hdls[i];
  192. if (polled_hdl) {
  193. if (hdl != polled_hdl)
  194. continue;
  195. } else {
  196. polled_hdl = hdl;
  197. }
  198. for (j = 0 ; j < MAX_FDS ; j++)
  199. if ((HANDLE_HDR(hdl)->flags & (RFD(j)|WFD(j))) &&
  200. hdl->generic.fds[j] == (PAL_IDX)fds[i].fd)
  201. break;
  202. if (j == MAX_FDS)
  203. continue;
  204. if (fds[i].revents & POLLOUT)
  205. HANDLE_HDR(hdl)->flags |= WRITABLE(j);
  206. if (fds[i].revents & (POLLHUP|POLLERR))
  207. HANDLE_HDR(hdl)->flags |= ERROR(j);
  208. }
  209. *polled = polled_hdl;
  210. return polled_hdl ? 0 : -PAL_ERROR_TRYAGAIN;
  211. }