db_object.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <cmpxchg.h>
  33. #include <errno.h>
  34. #define DEFAULT_QUANTUM 500
  35. /* internally to wait for one object. Also used as a shortcut to wait
  36. on events and semaphores */
  37. static int _DkObjectWaitOne (PAL_HANDLE handle, int timeout)
  38. {
  39. /* only for all these handle which has a file descriptor, or
  40. a eventfd. events and semaphores will skip this part */
  41. if (handle->__in.flags & HAS_FDS) {
  42. struct pollfd fds[MAX_FDS];
  43. int off[MAX_FDS];
  44. int nfds = 0;
  45. for (int i = 0 ; i < MAX_FDS ; i++) {
  46. int events = 0;
  47. if ((handle->__in.flags & RFD(i)) &&
  48. !(handle->__in.flags & ERROR(i)))
  49. events |= POLLIN;
  50. if ((handle->__in.flags & WFD(i)) &&
  51. !(handle->__in.flags & WRITEABLE(i)) &&
  52. !(handle->__in.flags & ERROR(i)))
  53. events |= POLLOUT;
  54. if (events) {
  55. fds[nfds].fd = handle->__in.fds[i];
  56. fds[nfds].events = events|POLLHUP|POLLERR;
  57. fds[nfds].revents = 0;
  58. off[nfds] = i;
  59. nfds++;
  60. }
  61. }
  62. if (!nfds)
  63. return -PAL_ERROR_TRYAGAIN;
  64. int ret = INLINE_SYSCALL(poll, 3, &fds, nfds,
  65. timeout ? timeout : -1);
  66. if (IS_ERR(ret))
  67. switch (ERRNO(ret)) {
  68. case EINTR:
  69. return -PAL_ERROR_INTERRUPTED;
  70. default:
  71. return unix_to_pal_error(ERRNO(ret));
  72. }
  73. if (!ret)
  74. return -PAL_ERROR_TRYAGAIN;
  75. for (int i = 0 ; i < nfds ; i++) {
  76. if (!fds[i].revents)
  77. continue;
  78. if (fds[i].revents & POLLOUT)
  79. handle->__in.flags |= WRITEABLE(off[i]);
  80. if (fds[i].revents & (POLLHUP|POLLERR))
  81. handle->__in.flags |= ERROR(off[i]);
  82. }
  83. return 0;
  84. }
  85. const struct handle_ops * ops = HANDLE_OPS(handle);
  86. if (!ops->wait)
  87. return -PAL_ERROR_NOTSUPPORT;
  88. return ops->wait(handle, timeout);
  89. }
  90. /* _DkObjectsWaitAny for internal use. The function wait for any of the handle
  91. in the handle array. timeout can be set for the wait. */
  92. int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, int timeout,
  93. PAL_HANDLE * polled)
  94. {
  95. if (count <= 0)
  96. return 0;
  97. if (count == 1) {
  98. *polled = handleArray[0];
  99. return _DkObjectWaitOne(handleArray[0], timeout);
  100. }
  101. int i, j, ret, maxfds = 0, nfds = 0;
  102. /* we are not gonna to allow any polling on muliple synchronous
  103. objects, doing this is simply violating the division of
  104. labor between PAL and library OS */
  105. for (i = 0 ; i < count ; i++) {
  106. PAL_HANDLE hdl = handleArray[i];
  107. if (!hdl)
  108. continue;
  109. if (!(hdl->__in.flags & HAS_FDS))
  110. return -PAL_ERROR_NOTSUPPORT;
  111. /* eliminate repeated entries */
  112. for (j = 0 ; j < i ; j++)
  113. if (hdl == handleArray[j])
  114. break;
  115. if (j == i) {
  116. for (j = 0 ; j < MAX_FDS ; j++)
  117. if (hdl->__in.flags & (RFD(j)|WFD(j)))
  118. maxfds++;
  119. }
  120. }
  121. struct pollfd * fds = __alloca(sizeof(struct pollfd) * maxfds);
  122. PAL_HANDLE * hdls = __alloca(sizeof(PAL_HANDLE) * maxfds);
  123. for (i = 0 ; i < count ; i++) {
  124. PAL_HANDLE hdl = handleArray[i];
  125. if (!hdl)
  126. continue;
  127. for (j = 0 ; j < i ; j++)
  128. if (hdl == handleArray[j])
  129. break;
  130. if (j < i)
  131. continue;
  132. for (j = 0 ; j < MAX_FDS ; j++) {
  133. int events = 0;
  134. if ((hdl->__in.flags & RFD(j)) &&
  135. !(hdl->__in.flags & ERROR(j)))
  136. events |= POLLIN;
  137. if ((hdl->__in.flags & WFD(j)) &&
  138. !(hdl->__in.flags & WRITEABLE(j)) &&
  139. !(hdl->__in.flags & ERROR(j)))
  140. events |= POLLOUT;
  141. if (events && hdl->__in.fds[j] != PAL_IDX_POISON) {
  142. fds[nfds].fd = hdl->__in.fds[j];
  143. fds[nfds].events = events|POLLHUP|POLLERR;
  144. fds[nfds].revents = 0;
  145. hdls[nfds] = hdl;
  146. nfds++;
  147. }
  148. }
  149. }
  150. if (!nfds)
  151. return -PAL_ERROR_TRYAGAIN;
  152. ret = INLINE_SYSCALL(poll, 3, fds, nfds, timeout ? timeout : -1);
  153. if (IS_ERR(ret))
  154. switch (ERRNO(ret)) {
  155. case EINTR:
  156. return -PAL_ERROR_INTERRUPTED;
  157. default:
  158. return unix_to_pal_error(ERRNO(ret));
  159. }
  160. if (!ret)
  161. return -PAL_ERROR_TRYAGAIN;
  162. PAL_HANDLE polled_hdl = NULL;
  163. for (i = 0 ; i < nfds ; i++) {
  164. if (!fds[i].revents)
  165. continue;
  166. PAL_HANDLE hdl = hdls[i];
  167. if (polled_hdl) {
  168. if (hdl != polled_hdl)
  169. continue;
  170. } else {
  171. polled_hdl = hdl;
  172. }
  173. for (j = 0 ; j < MAX_FDS ; j++)
  174. if ((hdl->__in.flags & (RFD(j)|WFD(j))) &&
  175. hdl->__in.fds[j] == fds[i].fd)
  176. break;
  177. if (j == MAX_FDS)
  178. continue;
  179. if (fds[i].revents & POLLOUT)
  180. hdl->__in.flags |= WRITEABLE(j);
  181. if (fds[i].revents & (POLLHUP|POLLERR))
  182. hdl->__in.flags |= ERROR(j);
  183. }
  184. *polled = polled_hdl;
  185. return polled_hdl ? 0 : -PAL_ERROR_TRYAGAIN;
  186. }