db_events.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 "api.h"
  27. #include <atomic.h>
  28. #include <linux/futex.h>
  29. #include <asm/errno.h>
  30. #include <linux/time.h>
  31. int _DkEventCreate (PAL_HANDLE * event, bool initialState, bool isnotification)
  32. {
  33. PAL_HANDLE ev = malloc(HANDLE_SIZE(event));
  34. SET_HANDLE_TYPE(ev, event);
  35. ev->event.isnotification = isnotification;
  36. atomic_set(&ev->event.signaled, initialState ? 1 : 0);
  37. atomic_set(&ev->event.nwaiters, 0);
  38. *event = ev;
  39. return 0;
  40. }
  41. void _DkEventDestroy (PAL_HANDLE handle)
  42. {
  43. free(handle);
  44. }
  45. int _DkEventSet (PAL_HANDLE event, int wakeup)
  46. {
  47. int ret = 0;
  48. if (event->event.isnotification) {
  49. // Leave it signaled, wake all
  50. if (atomic_cmpxchg(&event->event.signaled, 0, 1) == 0) {
  51. int nwaiters = atomic_read(&event->event.nwaiters);
  52. if (nwaiters) {
  53. if (wakeup != -1 && nwaiters > wakeup)
  54. nwaiters = wakeup;
  55. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled,
  56. FUTEX_WAKE, nwaiters, NULL, NULL, 0);
  57. if (IS_ERR(ret))
  58. atomic_set(&event->event.signaled, 0);
  59. }
  60. }
  61. } else {
  62. // Only one thread wakes up, leave unsignaled
  63. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAKE, 1,
  64. NULL, NULL, 0);
  65. }
  66. return IS_ERR(ret) ? PAL_ERROR_TRYAGAIN : ret;
  67. }
  68. int _DkEventWaitTimeout (PAL_HANDLE event, int timeout)
  69. {
  70. int ret = 0;
  71. if (!event->event.isnotification || !atomic_read(&event->event.signaled)) {
  72. struct timespec waittime;
  73. unsigned long sec = timeout / 1000000UL;
  74. unsigned long microsec = timeout - (sec * 1000000UL);
  75. waittime.tv_sec = sec;
  76. waittime.tv_nsec = microsec * 1000;
  77. atomic_inc(&event->event.nwaiters);
  78. do {
  79. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAIT,
  80. 0, &waittime, NULL, 0);
  81. if (IS_ERR(ret)) {
  82. if (ERRNO(ret) == EWOULDBLOCK) {
  83. ret = 0;
  84. } else {
  85. ret = unix_to_pal_error(ERRNO(ret));
  86. break;
  87. }
  88. }
  89. } while (event->event.isnotification &&
  90. !atomic_read(&event->event.signaled));
  91. atomic_dec(&event->event.nwaiters);
  92. }
  93. return ret;
  94. }
  95. int _DkEventWait (PAL_HANDLE event)
  96. {
  97. int ret = 0;
  98. if (!event->event.isnotification || !atomic_read(&event->event.signaled)) {
  99. atomic_inc(&event->event.nwaiters);
  100. do {
  101. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAIT,
  102. 0, NULL, NULL, 0);
  103. if (IS_ERR(ret)) {
  104. if (ERRNO(ret) == EWOULDBLOCK) {
  105. ret = 0;
  106. } else {
  107. ret = unix_to_pal_error(ERRNO(ret));
  108. break;
  109. }
  110. }
  111. } while (event->event.isnotification &&
  112. !atomic_read(&event->event.signaled));
  113. atomic_dec(&event->event.nwaiters);
  114. }
  115. return ret;
  116. }
  117. int _DkEventClear (PAL_HANDLE event)
  118. {
  119. atomic_set(&event->event.signaled, 0);
  120. return 0;
  121. }