db_object.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 waiting on PAL handles (polling).
  17. */
  18. #include <linux/poll.h>
  19. #include <linux/time.h>
  20. #include <linux/wait.h>
  21. #include "api.h"
  22. #include "pal.h"
  23. #include "pal_debug.h"
  24. #include "pal_defs.h"
  25. #include "pal_error.h"
  26. #include "pal_internal.h"
  27. #include "pal_linux.h"
  28. #include "pal_linux_defs.h"
  29. #include "pal_linux_error.h"
  30. /* Wait on a synchronization handle and return 0 if this handle's event was triggered or error
  31. * code otherwise (e.g., due to timeout). */
  32. int _DkSynchronizationObjectWait(PAL_HANDLE handle, int64_t timeout_us) {
  33. assert(IS_HANDLE_TYPE(handle, mutex) || IS_HANDLE_TYPE(handle, event));
  34. const struct handle_ops* ops = HANDLE_OPS(handle);
  35. if (!ops || !ops->wait)
  36. return -PAL_ERROR_NOTIMPLEMENTED;
  37. return ops->wait(handle, timeout_us);
  38. }
  39. /* Wait for specific events on all handles in the handle array and return multiple events
  40. * (including errors) reported by the host. Return 0 on success, PAL error on failure. */
  41. int _DkStreamsWaitEvents(size_t count, PAL_HANDLE* handle_array, PAL_FLG* events, PAL_FLG* ret_events,
  42. int64_t timeout_us) {
  43. int ret;
  44. if (count == 0)
  45. return 0;
  46. struct pollfd* fds = malloc(count * MAX_FDS * sizeof(*fds));
  47. if (!fds) {
  48. return -PAL_ERROR_NOMEM;
  49. }
  50. size_t* offsets = malloc(count * MAX_FDS * sizeof(*offsets));
  51. if (!offsets) {
  52. free(fds);
  53. return -PAL_ERROR_NOMEM;
  54. }
  55. /* collect all FDs of all PAL handles that may report read/write events */
  56. size_t nfds = 0;
  57. for (size_t i = 0; i < count; i++) {
  58. ret_events[i] = 0;
  59. PAL_HANDLE hdl = handle_array[i];
  60. if (!hdl)
  61. continue;
  62. /* collect all internal-handle FDs (only those which are readable/writable) */
  63. for (size_t j = 0; j < MAX_FDS; j++) {
  64. PAL_FLG flags = HANDLE_HDR(hdl)->flags;
  65. /* hdl might be a mutex/event/non-pollable object, simply ignore it */
  66. if (hdl->generic.fds[j] == PAL_IDX_POISON)
  67. continue;
  68. if (flags & ERROR(j))
  69. continue;
  70. int fdevents = 0;
  71. fdevents |= ((flags & RFD(j)) && (events[i] & PAL_WAIT_READ)) ? POLLIN : 0;
  72. fdevents |= ((flags & WFD(j)) && (events[i] & PAL_WAIT_WRITE)) ? POLLOUT : 0;
  73. if (fdevents) {
  74. fds[nfds].fd = hdl->generic.fds[j];
  75. fds[nfds].events = fdevents;
  76. fds[nfds].revents = 0;
  77. offsets[nfds] = i;
  78. nfds++;
  79. }
  80. }
  81. }
  82. if (!nfds) {
  83. /* did not find any waitable FDs (LibOS supplied closed/errored FDs or empty events) */
  84. ret = -PAL_ERROR_TRYAGAIN;
  85. goto out;
  86. }
  87. ret = ocall_poll(fds, nfds, timeout_us);
  88. if (IS_ERR(ret)) {
  89. switch (ERRNO(ret)) {
  90. case EINTR:
  91. case ERESTART:
  92. ret = -PAL_ERROR_INTERRUPTED;
  93. break;
  94. default:
  95. ret = unix_to_pal_error(ERRNO(ret));
  96. break;
  97. }
  98. goto out;
  99. }
  100. if (!ret) {
  101. /* timed out */
  102. ret = -PAL_ERROR_TRYAGAIN;
  103. goto out;
  104. }
  105. for (size_t i = 0; i < nfds; i++) {
  106. if (!fds[i].revents)
  107. continue;
  108. size_t j = offsets[i];
  109. /* update revents */
  110. if (fds[i].revents & POLLIN)
  111. ret_events[j] |= PAL_WAIT_READ;
  112. if (fds[i].revents & POLLOUT)
  113. ret_events[j] |= PAL_WAIT_WRITE;
  114. if (fds[i].revents & (POLLHUP | POLLERR | POLLNVAL))
  115. ret_events[j] |= PAL_WAIT_ERROR;
  116. /* update handle's internal fields (flags) */
  117. PAL_HANDLE hdl = handle_array[j];
  118. assert(hdl);
  119. for (size_t k = 0; k < MAX_FDS; k++) {
  120. if (hdl->generic.fds[k] != (PAL_IDX)fds[i].fd)
  121. continue;
  122. if (fds[i].revents & (POLLHUP|POLLERR|POLLNVAL))
  123. HANDLE_HDR(hdl)->flags |= ERROR(k);
  124. }
  125. }
  126. ret = 0;
  127. out:
  128. free(fds);
  129. free(offsets);
  130. return ret;
  131. }