db_events.c 4.3 KB

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