db_events.c 4.4 KB

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