db_events.c 4.4 KB

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