db_object.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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_linux_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_linux.h"
  25. #include "pal_error.h"
  26. #include "pal_debug.h"
  27. #include "api.h"
  28. #include <linux/time.h>
  29. #include <linux/poll.h>
  30. #include <linux/wait.h>
  31. #include <atomic.h>
  32. #include <cmpxchg.h>
  33. #include <asm/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 timespec timeout_ts;
  43. if (timeout >= 0) {
  44. long sec = (unsigned long) timeout / 1000000;
  45. long microsec = (unsigned long) timeout - (sec * 1000000);
  46. timeout_ts.tv_sec = sec;
  47. timeout_ts.tv_nsec = microsec * 1000;
  48. }
  49. struct pollfd fds[MAX_FDS];
  50. int off[MAX_FDS];
  51. int nfds = 0;
  52. for (int i = 0 ; i < MAX_FDS ; i++) {
  53. int events = 0;
  54. if ((handle->__in.flags & RFD(i)) &&
  55. !(handle->__in.flags & ERROR(i)))
  56. events |= POLLIN;
  57. if ((handle->__in.flags & WFD(i)) &&
  58. !(handle->__in.flags & WRITEABLE(i)) &&
  59. !(handle->__in.flags & ERROR(i)))
  60. events |= POLLOUT;
  61. if (events) {
  62. fds[nfds].fd = handle->__in.fds[i];
  63. fds[nfds].events = events|POLLHUP|POLLERR;
  64. fds[nfds].revents = 0;
  65. off[nfds] = i;
  66. nfds++;
  67. }
  68. }
  69. if (!nfds)
  70. return -PAL_ERROR_TRYAGAIN;
  71. int ret = INLINE_SYSCALL(ppoll, 5, &fds, nfds,
  72. timeout >= 0 ? &timeout_ts : NULL,
  73. NULL, 0);
  74. if (IS_ERR(ret))
  75. switch (ERRNO(ret)) {
  76. case EINTR:
  77. case ERESTART:
  78. return -PAL_ERROR_INTERRUPTED;
  79. default:
  80. return unix_to_pal_error(ERRNO(ret));
  81. }
  82. if (!ret)
  83. return -PAL_ERROR_TRYAGAIN;
  84. for (int i = 0 ; i < nfds ; i++) {
  85. if (!fds[i].revents)
  86. continue;
  87. if (fds[i].revents & POLLOUT)
  88. handle->__in.flags |= WRITEABLE(off[i]);
  89. if (fds[i].revents & (POLLHUP|POLLERR))
  90. handle->__in.flags |= ERROR(off[i]);
  91. }
  92. return 0;
  93. }
  94. const struct handle_ops * ops = HANDLE_OPS(handle);
  95. if (!ops->wait)
  96. return -PAL_ERROR_NOTSUPPORT;
  97. return ops->wait(handle, timeout);
  98. }
  99. /* _DkObjectsWaitAny for internal use. The function wait for any of the handle
  100. in the handle array. timeout can be set for the wait. */
  101. int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, int timeout,
  102. PAL_HANDLE * polled)
  103. {
  104. if (count <= 0)
  105. return 0;
  106. if (count == 1) {
  107. *polled = handleArray[0];
  108. return _DkObjectWaitOne(handleArray[0], timeout);
  109. }
  110. int i, j, ret, maxfds = 0, nfds = 0;
  111. /* we are not gonna to allow any polling on muliple synchronous
  112. objects, doing this is simply violating the division of
  113. labor between PAL and library OS */
  114. for (i = 0 ; i < count ; i++) {
  115. PAL_HANDLE hdl = handleArray[i];
  116. if (!hdl)
  117. continue;
  118. if (!(hdl->__in.flags & HAS_FDS))
  119. return -PAL_ERROR_NOTSUPPORT;
  120. /* eliminate repeated entries */
  121. for (j = 0 ; j < i ; j++)
  122. if (hdl == handleArray[j])
  123. break;
  124. if (j == i) {
  125. for (j = 0 ; j < MAX_FDS ; j++)
  126. if (hdl->__in.flags & (RFD(j)|WFD(j)))
  127. maxfds++;
  128. }
  129. }
  130. struct pollfd * fds = __alloca(sizeof(struct pollfd) * maxfds);
  131. PAL_HANDLE * hdls = __alloca(sizeof(PAL_HANDLE) * maxfds);
  132. for (i = 0 ; i < count ; i++) {
  133. PAL_HANDLE hdl = handleArray[i];
  134. if (!hdl)
  135. continue;
  136. for (j = 0 ; j < i ; j++)
  137. if (hdl == handleArray[j])
  138. break;
  139. if (j < i)
  140. continue;
  141. for (j = 0 ; j < MAX_FDS ; j++) {
  142. int events = 0;
  143. if ((hdl->__in.flags & RFD(j)) &&
  144. !(hdl->__in.flags & ERROR(j)))
  145. events |= POLLIN;
  146. if ((hdl->__in.flags & WFD(j)) &&
  147. !(hdl->__in.flags & WRITEABLE(j)) &&
  148. !(hdl->__in.flags & ERROR(j)))
  149. events |= POLLOUT;
  150. if (events && hdl->__in.fds[j] != PAL_IDX_POISON) {
  151. fds[nfds].fd = hdl->__in.fds[j];
  152. fds[nfds].events = events|POLLHUP|POLLERR;
  153. fds[nfds].revents = 0;
  154. hdls[nfds] = hdl;
  155. nfds++;
  156. }
  157. }
  158. }
  159. if (!nfds)
  160. return -PAL_ERROR_TRYAGAIN;
  161. struct timespec timeout_ts;
  162. if (timeout >= 0) {
  163. long sec = (unsigned long) timeout / 1000000;
  164. long microsec = (unsigned long) timeout - (sec * 1000000);
  165. timeout_ts.tv_sec = sec;
  166. timeout_ts.tv_nsec = microsec * 1000;
  167. }
  168. ret = INLINE_SYSCALL(ppoll, 5, fds, nfds,
  169. timeout >= 0 ? &timeout_ts : NULL,
  170. NULL, 0);
  171. if (IS_ERR(ret))
  172. switch (ERRNO(ret)) {
  173. case EINTR:
  174. case ERESTART:
  175. return -PAL_ERROR_INTERRUPTED;
  176. default:
  177. return unix_to_pal_error(ERRNO(ret));
  178. }
  179. if (!ret)
  180. return -PAL_ERROR_TRYAGAIN;
  181. PAL_HANDLE polled_hdl = NULL;
  182. for (i = 0 ; i < nfds ; i++) {
  183. if (!fds[i].revents)
  184. continue;
  185. PAL_HANDLE hdl = hdls[i];
  186. if (polled_hdl) {
  187. if (hdl != polled_hdl)
  188. continue;
  189. } else {
  190. polled_hdl = hdl;
  191. }
  192. for (j = 0 ; j < MAX_FDS ; j++)
  193. if ((hdl->__in.flags & (RFD(j)|WFD(j))) &&
  194. hdl->__in.fds[j] == fds[i].fd)
  195. break;
  196. if (j == MAX_FDS)
  197. continue;
  198. if (fds[i].revents & POLLOUT)
  199. hdl->__in.flags |= WRITEABLE(j);
  200. if (fds[i].revents & (POLLHUP|POLLERR))
  201. hdl->__in.flags |= ERROR(j);
  202. }
  203. *polled = polled_hdl;
  204. return polled_hdl ? 0 : -PAL_ERROR_TRYAGAIN;
  205. }