db_events.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_event.c
  15. *
  16. * This file contains implementation of Drawbridge event synchronization APIs.
  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_error.h"
  24. #include "pal_debug.h"
  25. #include "api.h"
  26. #include <atomic.h>
  27. #include <linux/futex.h>
  28. #include <asm/errno.h>
  29. #include <linux/time.h>
  30. int _DkEventCreate (PAL_HANDLE * event, bool initialState, bool isnotification)
  31. {
  32. PAL_HANDLE ev = malloc(HANDLE_SIZE(event));
  33. SET_HANDLE_TYPE(ev, event);
  34. ev->event.isnotification = isnotification;
  35. atomic_set(&ev->event.signaled, initialState ? 1 : 0);
  36. atomic_set(&ev->event.nwaiters, 0);
  37. *event = ev;
  38. return 0;
  39. }
  40. int _DkEventSet (PAL_HANDLE event, int wakeup)
  41. {
  42. int ret = 0;
  43. if (event->event.isnotification) {
  44. // Leave it signaled, wake all
  45. if (atomic_cmpxchg(&event->event.signaled, 0, 1) == 0) {
  46. int nwaiters = atomic_read(&event->event.nwaiters);
  47. if (nwaiters) {
  48. if (wakeup != -1 && nwaiters > wakeup)
  49. nwaiters = wakeup;
  50. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled,
  51. FUTEX_WAKE, nwaiters, NULL, NULL, 0);
  52. if (IS_ERR(ret))
  53. atomic_set(&event->event.signaled, 0);
  54. }
  55. }
  56. } else {
  57. // Only one thread wakes up, leave unsignaled
  58. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAKE, 1,
  59. NULL, NULL, 0);
  60. }
  61. return IS_ERR(ret) ? -PAL_ERROR_TRYAGAIN : ret;
  62. }
  63. int _DkEventWaitTimeout(PAL_HANDLE event, int64_t timeout_us)
  64. {
  65. int ret = 0;
  66. if (timeout_us < 0)
  67. return _DkEventWait(event);
  68. if (!event->event.isnotification || !atomic_read(&event->event.signaled)) {
  69. struct timespec waittime;
  70. int64_t sec = timeout_us / 1000000UL;
  71. int64_t microsec = timeout_us - (sec * 1000000UL);
  72. waittime.tv_sec = sec;
  73. waittime.tv_nsec = microsec * 1000;
  74. atomic_inc(&event->event.nwaiters);
  75. do {
  76. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAIT,
  77. 0, &waittime, NULL, 0);
  78. if (IS_ERR(ret)) {
  79. if (ERRNO(ret) == EWOULDBLOCK) {
  80. ret = 0;
  81. } else {
  82. ret = unix_to_pal_error(ERRNO(ret));
  83. break;
  84. }
  85. }
  86. } while (event->event.isnotification &&
  87. !atomic_read(&event->event.signaled));
  88. atomic_dec(&event->event.nwaiters);
  89. }
  90. return ret;
  91. }
  92. int _DkEventWait(PAL_HANDLE event) {
  93. int ret = 0;
  94. if (!event->event.isnotification || !atomic_read(&event->event.signaled)) {
  95. atomic_inc(&event->event.nwaiters);
  96. do {
  97. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAIT,
  98. 0, NULL, NULL, 0);
  99. if (IS_ERR(ret)) {
  100. if (ERRNO(ret) == EWOULDBLOCK) {
  101. ret = 0;
  102. } else {
  103. ret = unix_to_pal_error(ERRNO(ret));
  104. break;
  105. }
  106. }
  107. } while (event->event.isnotification &&
  108. !atomic_read(&event->event.signaled));
  109. atomic_dec(&event->event.nwaiters);
  110. }
  111. return ret;
  112. }
  113. int _DkEventClear (PAL_HANDLE event)
  114. {
  115. atomic_set(&event->event.signaled, 0);
  116. return 0;
  117. }
  118. static int event_close (PAL_HANDLE handle)
  119. {
  120. _DkEventSet(handle, -1);
  121. return 0;
  122. }
  123. static int event_wait(PAL_HANDLE handle, int64_t timeout_us) {
  124. return _DkEventWaitTimeout(handle, timeout_us);
  125. }
  126. struct handle_ops event_ops = {
  127. .close = &event_close,
  128. .wait = &event_wait,
  129. };