db_events.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_event.c
  17. *
  18. * This file contains implementation of Drawbridge event synchronization APIs.
  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 <atomic.h>
  29. #include <linux/futex.h>
  30. #include <asm/errno.h>
  31. #include <linux/time.h>
  32. int _DkEventCreate (PAL_HANDLE * event, bool initialState, bool isnotification)
  33. {
  34. PAL_HANDLE ev = malloc(HANDLE_SIZE(event));
  35. SET_HANDLE_TYPE(ev, event);
  36. ev->event.isnotification = isnotification;
  37. atomic_set(&ev->event.signaled, initialState ? 1 : 0);
  38. atomic_set(&ev->event.nwaiters, 0);
  39. *event = ev;
  40. return 0;
  41. }
  42. int _DkEventSet (PAL_HANDLE event, int wakeup)
  43. {
  44. int ret = 0;
  45. if (event->event.isnotification) {
  46. // Leave it signaled, wake all
  47. if (atomic_cmpxchg(&event->event.signaled, 0, 1) == 0) {
  48. int nwaiters = atomic_read(&event->event.nwaiters);
  49. if (nwaiters) {
  50. if (wakeup != -1 && nwaiters > wakeup)
  51. nwaiters = wakeup;
  52. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled,
  53. FUTEX_WAKE, nwaiters, NULL, NULL, 0);
  54. if (IS_ERR(ret))
  55. atomic_set(&event->event.signaled, 0);
  56. }
  57. }
  58. } else {
  59. // Only one thread wakes up, leave unsignaled
  60. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAKE, 1,
  61. NULL, NULL, 0);
  62. }
  63. return IS_ERR(ret) ? PAL_ERROR_TRYAGAIN : ret;
  64. }
  65. int _DkEventWaitTimeout (PAL_HANDLE event, uint64_t timeout)
  66. {
  67. int ret = 0;
  68. if (!event->event.isnotification || !atomic_read(&event->event.signaled)) {
  69. struct timespec waittime;
  70. unsigned long sec = timeout / 1000000UL;
  71. unsigned long microsec = timeout - (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. {
  94. int ret = 0;
  95. if (!event->event.isnotification || !atomic_read(&event->event.signaled)) {
  96. atomic_inc(&event->event.nwaiters);
  97. do {
  98. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAIT,
  99. 0, NULL, NULL, 0);
  100. if (IS_ERR(ret)) {
  101. if (ERRNO(ret) == EWOULDBLOCK) {
  102. ret = 0;
  103. } else {
  104. ret = unix_to_pal_error(ERRNO(ret));
  105. break;
  106. }
  107. }
  108. } while (event->event.isnotification &&
  109. !atomic_read(&event->event.signaled));
  110. atomic_dec(&event->event.nwaiters);
  111. }
  112. return ret;
  113. }
  114. int _DkEventClear (PAL_HANDLE event)
  115. {
  116. atomic_set(&event->event.signaled, 0);
  117. return 0;
  118. }
  119. static int event_close (PAL_HANDLE handle)
  120. {
  121. _DkEventSet(handle, -1);
  122. return 0;
  123. }
  124. static int event_wait (PAL_HANDLE handle, uint64_t timeout)
  125. {
  126. return timeout == NO_TIMEOUT ? _DkEventWait(handle) :
  127. _DkEventWaitTimeout(handle, timeout);
  128. }
  129. struct handle_ops event_ops = {
  130. .close = &event_close,
  131. .wait = &event_wait,
  132. };