db_object.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #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, uint64_t 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 & WRITEABLE(i)) &&
  51. !(HANDLE_HDR(handle)->flags & ERROR(i)))
  52. events |= POLLOUT;
  53. if (events) {
  54. fds[nfds].fd = HANDLE_HDR(handle)->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. uint64_t waittime = timeout;
  64. int ret = ocall_poll(fds, nfds, timeout >= 0 ? &waittime : NULL);
  65. if (ret < 0)
  66. return 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 |= WRITEABLE(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->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, uint64_t timeout,
  87. PAL_HANDLE * polled)
  88. {
  89. if (count <= 0)
  90. return 0;
  91. if (count == 1) {
  92. *polled = handleArray[0];
  93. return _DkObjectWaitOne(handleArray[0], timeout);
  94. }
  95. int i, j, ret, maxfds = 0, nfds = 0;
  96. /* we are not gonna to allow any polling on muliple synchronous
  97. objects, doing this is simply violating the division of
  98. labor between PAL and library OS */
  99. for (i = 0 ; i < count ; i++) {
  100. PAL_HANDLE hdl = handleArray[i];
  101. if (!hdl)
  102. continue;
  103. if (!(HANDLE_HDR(hdl)->flags & HAS_FDS))
  104. return -PAL_ERROR_NOTSUPPORT;
  105. /* eliminate repeated entries */
  106. for (j = 0 ; j < i ; j++)
  107. if (hdl == handleArray[j])
  108. break;
  109. if (j == i) {
  110. for (j = 0 ; j < MAX_FDS ; j++)
  111. if (HANDLE_HDR(hdl)->flags & (RFD(j)|WFD(j)))
  112. maxfds++;
  113. }
  114. }
  115. struct pollfd * fds = __alloca(sizeof(struct pollfd) * maxfds);
  116. PAL_HANDLE * hdls = __alloca(sizeof(PAL_HANDLE) * maxfds);
  117. for (i = 0 ; i < count ; i++) {
  118. PAL_HANDLE hdl = handleArray[i];
  119. if (!hdl)
  120. continue;
  121. for (j = 0 ; j < i ; j++)
  122. if (hdl == handleArray[j])
  123. break;
  124. if (j < i)
  125. continue;
  126. for (j = 0 ; j < MAX_FDS ; j++) {
  127. int events = 0;
  128. if ((HANDLE_HDR(hdl)->flags & RFD(j)) &&
  129. !(HANDLE_HDR(hdl)->flags & ERROR(j)))
  130. events |= POLLIN;
  131. if ((HANDLE_HDR(hdl)->flags & WFD(j)) &&
  132. !(HANDLE_HDR(hdl)->flags & WRITEABLE(j)) &&
  133. !(HANDLE_HDR(hdl)->flags & ERROR(j)))
  134. events |= POLLOUT;
  135. if (events && HANDLE_HDR(hdl)->fds[j] != PAL_IDX_POISON) {
  136. fds[nfds].fd = HANDLE_HDR(hdl)->fds[j];
  137. fds[nfds].events = events|POLLHUP|POLLERR;
  138. fds[nfds].revents = 0;
  139. hdls[nfds] = hdl;
  140. nfds++;
  141. }
  142. }
  143. }
  144. if (!nfds)
  145. return -PAL_ERROR_TRYAGAIN;
  146. uint64_t waittime = timeout;
  147. ret = ocall_poll(fds, nfds, timeout >= 0 ? &waittime : NULL);
  148. if (ret < 0)
  149. return ret;
  150. if (!ret)
  151. return -PAL_ERROR_TRYAGAIN;
  152. PAL_HANDLE polled_hdl = NULL;
  153. for (i = 0 ; i < nfds ; i++) {
  154. if (!fds[i].revents)
  155. continue;
  156. PAL_HANDLE hdl = hdls[i];
  157. if (polled_hdl) {
  158. if (hdl != polled_hdl)
  159. continue;
  160. } else {
  161. polled_hdl = hdl;
  162. }
  163. for (j = 0 ; j < MAX_FDS ; j++)
  164. if ((HANDLE_HDR(hdl)->flags & (RFD(j)|WFD(j))) &&
  165. HANDLE_HDR(hdl)->fds[j] == fds[i].fd)
  166. break;
  167. if (j == MAX_FDS)
  168. continue;
  169. if (fds[i].revents & POLLOUT)
  170. HANDLE_HDR(hdl)->flags |= WRITEABLE(j);
  171. if (fds[i].revents & (POLLHUP|POLLERR))
  172. HANDLE_HDR(hdl)->flags |= ERROR(j);
  173. }
  174. *polled = polled_hdl;
  175. return polled_hdl ? 0 : -PAL_ERROR_TRYAGAIN;
  176. }