db_events.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <linux/time.h>
  30. int _DkEventCreate (PAL_HANDLE * event, bool initialState, bool isnotification)
  31. {
  32. PAL_HANDLE ev = malloc_untrusted(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_untrusted(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 = ocall_futex((int *) &event->event.signaled.counter,
  55. FUTEX_WAKE, nwaiters, NULL);
  56. if (ret < 0)
  57. atomic_set(&event->event.signaled, 0);
  58. }
  59. }
  60. } else {
  61. // Only one thread wakes up, leave unsignaled
  62. ret = ocall_futex((int *) &event->event.signaled.counter,
  63. FUTEX_WAKE, 1, NULL);
  64. if (ret < 0)
  65. return ret;
  66. }
  67. return ret;
  68. }
  69. int _DkEventWaitTimeout (PAL_HANDLE event, uint64_t timeout)
  70. {
  71. int ret = 0;
  72. if (!event->event.isnotification || !atomic_read(&event->event.signaled)) {
  73. unsigned long waittime = timeout;
  74. atomic_inc(&event->event.nwaiters);
  75. do {
  76. ret = ocall_futex((int *) &event->event.signaled.counter,
  77. FUTEX_WAIT, 0, timeout ? &waittime : NULL);
  78. if (ret < 0) {
  79. if (ret == -PAL_ERROR_TRYAGAIN)
  80. ret = 0;
  81. else
  82. break;
  83. }
  84. } while (event->event.isnotification &&
  85. !atomic_read(&event->event.signaled));
  86. atomic_dec(&event->event.nwaiters);
  87. }
  88. return ret;
  89. }
  90. int _DkEventWait (PAL_HANDLE event)
  91. {
  92. int ret = 0;
  93. if (!event->event.isnotification || !atomic_read(&event->event.signaled)) {
  94. atomic_inc(&event->event.nwaiters);
  95. do {
  96. ret = ocall_futex((int *) &event->event.signaled.counter,
  97. FUTEX_WAIT, 0, NULL);
  98. if (ret < 0) {
  99. if (ret == -PAL_ERROR_TRYAGAIN)
  100. ret = 0;
  101. else
  102. break;
  103. }
  104. } while (event->event.isnotification &&
  105. !atomic_read(&event->event.signaled));
  106. atomic_dec(&event->event.nwaiters);
  107. }
  108. return ret;
  109. }
  110. int _DkEventClear (PAL_HANDLE event)
  111. {
  112. atomic_set(&event->event.signaled, 0);
  113. return 0;
  114. }