db_object.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_linux_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_linux.h"
  25. #include "pal_linux_error.h"
  26. #include "pal_error.h"
  27. #include "pal_debug.h"
  28. #include "api.h"
  29. #include <linux/time.h>
  30. #include <linux/poll.h>
  31. #include <linux/wait.h>
  32. #include <atomic.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, PAL_NUM 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(handle)->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(handle)->flags & RFD(i)) &&
  47. !(HANDLE_HDR(handle)->flags & ERROR(i)))
  48. events |= POLLIN;
  49. if ((HANDLE_HDR(handle)->flags & WFD(i)) &&
  50. !(HANDLE_HDR(handle)->flags & WRITABLE(i)) &&
  51. !(HANDLE_HDR(handle)->flags & ERROR(i)))
  52. events |= POLLOUT;
  53. if (events) {
  54. fds[nfds].fd = handle->generic.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. int64_t waittime = timeout;
  64. int ret = ocall_poll(fds, nfds, timeout != NO_TIMEOUT ? &waittime : NULL);
  65. if (IS_ERR(ret))
  66. return unix_to_pal_error(ERRNO(ret));
  67. if (!ret)
  68. return -PAL_ERROR_TRYAGAIN;
  69. for (int i = 0 ; i < nfds ; i++) {
  70. if (!fds[i].revents)
  71. continue;
  72. if (fds[i].revents & POLLOUT)
  73. HANDLE_HDR(handle)->flags |= WRITABLE(off[i]);
  74. if (fds[i].revents & (POLLHUP|POLLERR))
  75. HANDLE_HDR(handle)->flags |= ERROR(off[i]);
  76. }
  77. return 0;
  78. }
  79. const struct handle_ops * ops = HANDLE_OPS(handle);
  80. if (!ops || !ops->wait)
  81. return -PAL_ERROR_NOTSUPPORT;
  82. return ops->wait(handle, timeout);
  83. }
  84. /* _DkObjectsWaitAny for internal use. The function wait for any of the handle
  85. in the handle array. timeout can be set for the wait. */
  86. int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, PAL_NUM timeout,
  87. PAL_HANDLE * polled)
  88. {
  89. if (count <= 0)
  90. return 0;
  91. if (count == 1) {
  92. // It is possible to have NULL pointers in the handle array.
  93. // In this case, assume nothing is polled.
  94. if (!handleArray[0])
  95. return -PAL_ERROR_TRYAGAIN;
  96. int rv = _DkObjectWaitOne(handleArray[0], timeout);
  97. if (rv == 0)
  98. *polled = handleArray[0];
  99. return rv;
  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 (!(HANDLE_HDR(hdl)->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 (HANDLE_HDR(hdl)->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 ((HANDLE_HDR(hdl)->flags & RFD(j)) &&
  135. !(HANDLE_HDR(hdl)->flags & ERROR(j)))
  136. events |= POLLIN;
  137. if ((HANDLE_HDR(hdl)->flags & WFD(j)) &&
  138. !(HANDLE_HDR(hdl)->flags & WRITABLE(j)) &&
  139. !(HANDLE_HDR(hdl)->flags & ERROR(j)))
  140. events |= POLLOUT;
  141. if (events && hdl->generic.fds[j] != PAL_IDX_POISON) {
  142. fds[nfds].fd = hdl->generic.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. int64_t waittime = timeout;
  153. ret = ocall_poll(fds, nfds, timeout != NO_TIMEOUT ? &waittime : NULL);
  154. if (IS_ERR(ret))
  155. return unix_to_pal_error(ERRNO(ret));
  156. if (!ret)
  157. return -PAL_ERROR_TRYAGAIN;
  158. PAL_HANDLE polled_hdl = NULL;
  159. for (i = 0 ; i < nfds ; i++) {
  160. if (!fds[i].revents)
  161. continue;
  162. PAL_HANDLE hdl = hdls[i];
  163. if (polled_hdl) {
  164. if (hdl != polled_hdl)
  165. continue;
  166. } else {
  167. polled_hdl = hdl;
  168. }
  169. for (j = 0 ; j < MAX_FDS ; j++)
  170. if ((HANDLE_HDR(hdl)->flags & (RFD(j)|WFD(j))) &&
  171. hdl->generic.fds[j] == (PAL_IDX)fds[i].fd)
  172. break;
  173. if (j == MAX_FDS)
  174. continue;
  175. if (fds[i].revents & POLLOUT)
  176. HANDLE_HDR(hdl)->flags |= WRITABLE(j);
  177. if (fds[i].revents & (POLLHUP|POLLERR))
  178. HANDLE_HDR(hdl)->flags |= ERROR(j);
  179. }
  180. *polled = polled_hdl;
  181. return polled_hdl ? 0 : -PAL_ERROR_TRYAGAIN;
  182. }