db_object.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_freebsd_defs.h"
  20. #include "pal.h"
  21. #include "pal_internal.h"
  22. #include "pal_freebsd.h"
  23. #include "pal_error.h"
  24. #include "pal_debug.h"
  25. #include "api.h"
  26. #include <time.h>
  27. #include <sys/poll.h>
  28. #include <sys/wait.h>
  29. #include <atomic.h>
  30. #include <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. static int _DkObjectWaitOne (PAL_HANDLE handle, int timeout)
  35. {
  36. /* only for all these handle which has a file descriptor, or
  37. a eventfd. events and semaphores will skip this part */
  38. if (handle->hdr.flags & HAS_FDS) {
  39. struct pollfd fds[MAX_FDS];
  40. int off[MAX_FDS];
  41. int nfds = 0;
  42. for (int i = 0 ; i < MAX_FDS ; i++) {
  43. int events = 0;
  44. if ((handle->hdr.flags & RFD(i)) &&
  45. !(handle->hdr.flags & ERROR(i)))
  46. events |= POLLIN;
  47. if ((handle->hdr.flags & WFD(i)) &&
  48. !(handle->hdr.flags & WRITABLE(i)) &&
  49. !(handle->hdr.flags & ERROR(i)))
  50. events |= POLLOUT;
  51. if (events) {
  52. fds[nfds].fd = handle->hdr.fds[i];
  53. fds[nfds].events = events|POLLHUP|POLLERR;
  54. fds[nfds].revents = 0;
  55. off[nfds] = i;
  56. nfds++;
  57. }
  58. }
  59. if (!nfds)
  60. return -PAL_ERROR_TRYAGAIN;
  61. int ret = INLINE_SYSCALL(poll, 3, &fds, nfds,
  62. timeout ? timeout : -1);
  63. if (IS_ERR(ret))
  64. switch (ERRNO(ret)) {
  65. case EINTR:
  66. return -PAL_ERROR_INTERRUPTED;
  67. default:
  68. return unix_to_pal_error(ERRNO(ret));
  69. }
  70. if (!ret)
  71. return -PAL_ERROR_TRYAGAIN;
  72. for (int i = 0 ; i < nfds ; i++) {
  73. if (!fds[i].revents)
  74. continue;
  75. if (fds[i].revents & POLLOUT)
  76. handle->hdr.flags |= WRITABLE(off[i]);
  77. if (fds[i].revents & (POLLHUP|POLLERR))
  78. handle->hdr.flags |= ERROR(off[i]);
  79. }
  80. return 0;
  81. }
  82. const struct handle_ops * ops = HANDLE_OPS(handle);
  83. if (!ops)
  84. return -PAL_ERROR_BADHANDLE;
  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. }