db_object.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_object.c
  17. *
  18. * This file contains APIs for closing or polling PAL handles.
  19. */
  20. #include "pal_defs.h"
  21. #include "pal_freebsd_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_freebsd.h"
  25. #include "pal_error.h"
  26. #include "pal_debug.h"
  27. #include "api.h"
  28. #include <time.h>
  29. #include <sys/poll.h>
  30. #include <sys/wait.h>
  31. #include <atomic.h>
  32. #include <errno.h>
  33. #define DEFAULT_QUANTUM 500
  34. /* internally to wait for one object. Also used as a shortcut to wait
  35. on events and semaphores */
  36. static int _DkObjectWaitOne (PAL_HANDLE handle, int timeout)
  37. {
  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.flags & HAS_FDS) {
  41. struct pollfd fds[MAX_FDS];
  42. int off[MAX_FDS];
  43. int nfds = 0;
  44. for (int i = 0 ; i < MAX_FDS ; i++) {
  45. int events = 0;
  46. if ((handle->hdr.flags & RFD(i)) &&
  47. !(handle->hdr.flags & ERROR(i)))
  48. events |= POLLIN;
  49. if ((handle->hdr.flags & WFD(i)) &&
  50. !(handle->hdr.flags & WRITABLE(i)) &&
  51. !(handle->hdr.flags & ERROR(i)))
  52. events |= POLLOUT;
  53. if (events) {
  54. fds[nfds].fd = handle->hdr.fds[i];
  55. fds[nfds].events = events|POLLHUP|POLLERR;
  56. fds[nfds].revents = 0;
  57. off[nfds] = i;
  58. nfds++;
  59. }
  60. }
  61. if (!nfds)
  62. return -PAL_ERROR_TRYAGAIN;
  63. int ret = INLINE_SYSCALL(poll, 3, &fds, nfds,
  64. timeout ? timeout : -1);
  65. if (IS_ERR(ret))
  66. switch (ERRNO(ret)) {
  67. case EINTR:
  68. return -PAL_ERROR_INTERRUPTED;
  69. default:
  70. return unix_to_pal_error(ERRNO(ret));
  71. }
  72. if (!ret)
  73. return -PAL_ERROR_TRYAGAIN;
  74. for (int i = 0 ; i < nfds ; i++) {
  75. if (!fds[i].revents)
  76. continue;
  77. if (fds[i].revents & POLLOUT)
  78. handle->hdr.flags |= WRITABLE(off[i]);
  79. if (fds[i].revents & (POLLHUP|POLLERR))
  80. handle->hdr.flags |= ERROR(off[i]);
  81. }
  82. return 0;
  83. }
  84. const struct handle_ops * ops = HANDLE_OPS(handle);
  85. if (!ops->wait)
  86. return -PAL_ERROR_NOTSUPPORT;
  87. return ops->wait(handle, timeout);
  88. }
  89. /* _DkObjectsWaitAny for internal use. The function wait for any of the handle
  90. in the handle array. timeout can be set for the wait. */
  91. int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, uint64_t timeout,
  92. PAL_HANDLE * polled)
  93. {
  94. if (count <= 0)
  95. return 0;
  96. if (count == 1) {
  97. *polled = handleArray[0];
  98. return _DkObjectWaitOne(handleArray[0], timeout);
  99. }
  100. int i, j, ret, maxfds = 0, nfds = 0;
  101. /* we are not gonna to allow any polling on muliple synchronous
  102. objects, doing this is simply violating the division of
  103. labor between PAL and library OS */
  104. for (i = 0 ; i < count ; i++) {
  105. PAL_HANDLE hdl = handleArray[i];
  106. if (!hdl)
  107. continue;
  108. if (!(hdl->hdr.flags & HAS_FDS))
  109. return -PAL_ERROR_NOTSUPPORT;
  110. /* eliminate repeated entries */
  111. for (j = 0 ; j < i ; j++)
  112. if (hdl == handleArray[j])
  113. break;
  114. if (j == i) {
  115. for (j = 0 ; j < MAX_FDS ; j++)
  116. if (hdl->hdr.flags & (RFD(j)|WFD(j)))
  117. maxfds++;
  118. }
  119. }
  120. struct pollfd * fds = __alloca(sizeof(struct pollfd) * maxfds);
  121. PAL_HANDLE * hdls = __alloca(sizeof(PAL_HANDLE) * maxfds);
  122. for (i = 0 ; i < count ; i++) {
  123. PAL_HANDLE hdl = handleArray[i];
  124. if (!hdl)
  125. continue;
  126. for (j = 0 ; j < i ; j++)
  127. if (hdl == handleArray[j])
  128. break;
  129. if (j < i)
  130. continue;
  131. for (j = 0 ; j < MAX_FDS ; j++) {
  132. int events = 0;
  133. if ((hdl->hdr.flags & RFD(j)) &&
  134. !(hdl->hdr.flags & ERROR(j)))
  135. events |= POLLIN;
  136. if ((hdl->hdr.flags & WFD(j)) &&
  137. !(hdl->hdr.flags & WRITABLE(j)) &&
  138. !(hdl->hdr.flags & ERROR(j)))
  139. events |= POLLOUT;
  140. if (events && hdl->hdr.fds[j] != PAL_IDX_POISON) {
  141. fds[nfds].fd = hdl->hdr.fds[j];
  142. fds[nfds].events = events|POLLHUP|POLLERR;
  143. fds[nfds].revents = 0;
  144. hdls[nfds] = hdl;
  145. nfds++;
  146. }
  147. }
  148. }
  149. if (!nfds)
  150. return -PAL_ERROR_TRYAGAIN;
  151. ret = INLINE_SYSCALL(poll, 3, fds, nfds, timeout ? timeout : -1);
  152. if (IS_ERR(ret))
  153. switch (ERRNO(ret)) {
  154. case EINTR:
  155. return -PAL_ERROR_INTERRUPTED;
  156. default:
  157. return unix_to_pal_error(ERRNO(ret));
  158. }
  159. if (!ret)
  160. return -PAL_ERROR_TRYAGAIN;
  161. PAL_HANDLE polled_hdl = NULL;
  162. for (i = 0 ; i < nfds ; i++) {
  163. if (!fds[i].revents)
  164. continue;
  165. PAL_HANDLE hdl = hdls[i];
  166. if (polled_hdl) {
  167. if (hdl != polled_hdl)
  168. continue;
  169. } else {
  170. polled_hdl = hdl;
  171. }
  172. for (j = 0 ; j < MAX_FDS ; j++)
  173. if ((hdl->hdr.flags & (RFD(j)|WFD(j))) &&
  174. hdl->hdr.fds[j] == fds[i].fd)
  175. break;
  176. if (j == MAX_FDS)
  177. continue;
  178. if (fds[i].revents & POLLOUT)
  179. hdl->hdr.flags |= WRITABLE(j);
  180. if (fds[i].revents & (POLLHUP|POLLERR))
  181. hdl->hdr.flags |= ERROR(j);
  182. }
  183. *polled = polled_hdl;
  184. return polled_hdl ? 0 : -PAL_ERROR_TRYAGAIN;
  185. }