db_events.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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)
  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. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled,
  54. FUTEX_WAKE, nwaiters, NULL, NULL, 0);
  55. if (IS_ERR(ret))
  56. atomic_set(&event->event.signaled, 0);
  57. }
  58. }
  59. } else {
  60. // Only one thread wakes up, leave unsignaled
  61. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAKE, 1,
  62. NULL, NULL, 0);
  63. }
  64. return IS_ERR(ret) ? PAL_ERROR_TRYAGAIN : 0;
  65. }
  66. int _DkEventWaitTimeout (PAL_HANDLE event, int timeout)
  67. {
  68. int ret = 0;
  69. if (!atomic_read(&event->event.signaled)) {
  70. struct timespec waittime;
  71. unsigned long sec = timeout / 1000000UL;
  72. unsigned long microsec = timeout - (sec * 1000000UL);
  73. waittime.tv_sec = sec;
  74. waittime.tv_nsec = microsec * 1000;
  75. atomic_inc(&event->event.nwaiters);
  76. do {
  77. ret = INLINE_SYSCALL(futex, 6, &event->event.signaled, FUTEX_WAIT,
  78. 0, &waittime, NULL, 0);
  79. if (IS_ERR(ret)) {
  80. if (ERRNO(ret) == EWOULDBLOCK) {
  81. ret = 0;
  82. } else {
  83. ret = unix_to_pal_error(ERRNO(ret));
  84. break;
  85. }
  86. }
  87. } while (!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 (!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 (!atomic_read(&event->event.signaled));
  109. atomic_dec(&event->event.nwaiters);
  110. }
  111. return ret;
  112. }
  113. int _DkEventClear (PAL_HANDLE event)
  114. {
  115. atomic_set(&event->event.signaled, 0);
  116. return 0;
  117. }