db_object.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_object.c
  15. *
  16. * This file contains APIs for closing or polling PAL handles.
  17. */
  18. #include "pal_defs.h"
  19. #include "pal_linux_defs.h"
  20. #include "pal.h"
  21. #include "pal_internal.h"
  22. #include "pal_linux.h"
  23. #include "pal_linux_error.h"
  24. #include "pal_error.h"
  25. #include "pal_debug.h"
  26. #include "api.h"
  27. #include <linux/time.h>
  28. #include <linux/poll.h>
  29. #include <linux/wait.h>
  30. #include <atomic.h>
  31. #define DEFAULT_QUANTUM 500
  32. /* internally to wait for one object. Also used as a shortcut to wait
  33. on events and semaphores */
  34. static int _DkObjectWaitOne(PAL_HANDLE handle, int64_t timeout_us) {
  35. /* only for all these handle which has a file descriptor, or
  36. a eventfd. events and semaphores will skip this part */
  37. if (HANDLE_HDR(handle)->flags & HAS_FDS) {
  38. struct pollfd fds[MAX_FDS];
  39. int off[MAX_FDS];
  40. int nfds = 0;
  41. for (int i = 0 ; i < MAX_FDS ; i++) {
  42. int events = 0;
  43. if ((HANDLE_HDR(handle)->flags & RFD(i)) &&
  44. !(HANDLE_HDR(handle)->flags & ERROR(i)))
  45. events |= POLLIN;
  46. if ((HANDLE_HDR(handle)->flags & WFD(i)) &&
  47. !(HANDLE_HDR(handle)->flags & WRITABLE(i)) &&
  48. !(HANDLE_HDR(handle)->flags & ERROR(i)))
  49. events |= POLLOUT;
  50. if (events) {
  51. fds[nfds].fd = handle->generic.fds[i];
  52. fds[nfds].events = events|POLLHUP|POLLERR;
  53. fds[nfds].revents = 0;
  54. off[nfds] = i;
  55. nfds++;
  56. }
  57. }
  58. if (!nfds)
  59. return -PAL_ERROR_TRYAGAIN;
  60. int ret = ocall_poll(fds, nfds, timeout_us);
  61. if (IS_ERR(ret))
  62. return unix_to_pal_error(ERRNO(ret));
  63. if (!ret)
  64. return -PAL_ERROR_TRYAGAIN;
  65. for (int i = 0 ; i < nfds ; i++) {
  66. if (!fds[i].revents)
  67. continue;
  68. if (fds[i].revents & POLLOUT)
  69. HANDLE_HDR(handle)->flags |= WRITABLE(off[i]);
  70. if (fds[i].revents & (POLLHUP|POLLERR))
  71. HANDLE_HDR(handle)->flags |= ERROR(off[i]);
  72. }
  73. return 0;
  74. }
  75. const struct handle_ops * ops = HANDLE_OPS(handle);
  76. if (!ops || !ops->wait)
  77. return -PAL_ERROR_NOTSUPPORT;
  78. return ops->wait(handle, timeout_us);
  79. }
  80. /* _DkObjectsWaitAny for internal use. The function wait for any of the handle
  81. in the handle array. timeout_us can be set for the wait. */
  82. int _DkObjectsWaitAny(int count, PAL_HANDLE* handleArray, int64_t timeout_us,
  83. PAL_HANDLE* polled) {
  84. if (count <= 0)
  85. return 0;
  86. if (count == 1) {
  87. // It is possible to have NULL pointers in the handle array.
  88. // In this case, assume nothing is polled.
  89. if (!handleArray[0])
  90. return -PAL_ERROR_TRYAGAIN;
  91. int rv = _DkObjectWaitOne(handleArray[0], timeout_us);
  92. if (rv == 0)
  93. *polled = handleArray[0];
  94. return rv;
  95. }
  96. int i, j, ret, maxfds = 0, nfds = 0;
  97. /* we are not gonna to allow any polling on muliple synchronous
  98. objects, doing this is simply violating the division of
  99. labor between PAL and library OS */
  100. for (i = 0 ; i < count ; i++) {
  101. PAL_HANDLE hdl = handleArray[i];
  102. if (!hdl)
  103. continue;
  104. if (!(HANDLE_HDR(hdl)->flags & HAS_FDS))
  105. return -PAL_ERROR_NOTSUPPORT;
  106. /* eliminate repeated entries */
  107. for (j = 0 ; j < i ; j++)
  108. if (hdl == handleArray[j])
  109. break;
  110. if (j == i) {
  111. for (j = 0 ; j < MAX_FDS ; j++)
  112. if (HANDLE_HDR(hdl)->flags & (RFD(j)|WFD(j)))
  113. maxfds++;
  114. }
  115. }
  116. struct pollfd * fds = __alloca(sizeof(struct pollfd) * maxfds);
  117. PAL_HANDLE * hdls = __alloca(sizeof(PAL_HANDLE) * maxfds);
  118. for (i = 0 ; i < count ; i++) {
  119. PAL_HANDLE hdl = handleArray[i];
  120. if (!hdl)
  121. continue;
  122. for (j = 0 ; j < i ; j++)
  123. if (hdl == handleArray[j])
  124. break;
  125. if (j < i)
  126. continue;
  127. for (j = 0 ; j < MAX_FDS ; j++) {
  128. int events = 0;
  129. if ((HANDLE_HDR(hdl)->flags & RFD(j)) &&
  130. !(HANDLE_HDR(hdl)->flags & ERROR(j)))
  131. events |= POLLIN;
  132. if ((HANDLE_HDR(hdl)->flags & WFD(j)) &&
  133. !(HANDLE_HDR(hdl)->flags & WRITABLE(j)) &&
  134. !(HANDLE_HDR(hdl)->flags & ERROR(j)))
  135. events |= POLLOUT;
  136. if (events && hdl->generic.fds[j] != PAL_IDX_POISON) {
  137. fds[nfds].fd = hdl->generic.fds[j];
  138. fds[nfds].events = events|POLLHUP|POLLERR;
  139. fds[nfds].revents = 0;
  140. hdls[nfds] = hdl;
  141. nfds++;
  142. }
  143. }
  144. }
  145. if (!nfds)
  146. return -PAL_ERROR_TRYAGAIN;
  147. ret = ocall_poll(fds, nfds, timeout_us);
  148. if (IS_ERR(ret))
  149. return unix_to_pal_error(ERRNO(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. hdl->generic.fds[j] == (PAL_IDX)fds[i].fd)
  166. break;
  167. if (j == MAX_FDS)
  168. continue;
  169. if (fds[i].revents & POLLOUT)
  170. HANDLE_HDR(hdl)->flags |= WRITABLE(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. }