db_object.c 6.4 KB

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