PageRenderTime 191ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mono/utils/os-event-win32.c

https://github.com/kumpera/mono
C | 120 lines | 89 code | 22 blank | 9 comment | 32 complexity | 7fb16999562017ab87b8fad669175ad9 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, Unlicense
  1. /**
  2. * \file
  3. * MonoOSEvent on Win32
  4. *
  5. * Author:
  6. * Ludovic Henry (luhenry@microsoft.com)
  7. *
  8. * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  9. */
  10. #include "os-event.h"
  11. #include <windows.h>
  12. #include <winbase.h>
  13. #include "atomic.h"
  14. #include "mono-os-wait.h"
  15. void
  16. mono_os_event_init (MonoOSEvent *event, gboolean initial)
  17. {
  18. g_assert (event);
  19. event->handle = CreateEvent (NULL, TRUE, initial, NULL);
  20. if (G_UNLIKELY (!event->handle))
  21. g_error ("%s: CreateEvent failed with error %d", __func__, GetLastError ());
  22. }
  23. void
  24. mono_os_event_destroy (MonoOSEvent *event)
  25. {
  26. BOOL res;
  27. g_assert (event);
  28. g_assert (event->handle);
  29. res = CloseHandle (event->handle);
  30. if (G_UNLIKELY (res == 0))
  31. g_error ("%s: CloseHandle failed with error %d", __func__, GetLastError ());
  32. }
  33. void
  34. mono_os_event_set (MonoOSEvent *event)
  35. {
  36. BOOL res;
  37. g_assert (event);
  38. g_assert (event->handle);
  39. res = SetEvent (event->handle);
  40. if (G_UNLIKELY (res == 0))
  41. g_error ("%s: SetEvent failed with error %d", __func__, GetLastError ());
  42. }
  43. void
  44. mono_os_event_reset (MonoOSEvent *event)
  45. {
  46. BOOL res;
  47. g_assert (event);
  48. g_assert (event->handle);
  49. res = ResetEvent (event->handle);
  50. if (G_UNLIKELY (res == 0))
  51. g_error ("%s: ResetEvent failed with error %d", __func__, GetLastError ());
  52. }
  53. MonoOSEventWaitRet
  54. mono_os_event_wait_one (MonoOSEvent *event, guint32 timeout, gboolean alertable)
  55. {
  56. DWORD res;
  57. g_assert (event);
  58. g_assert (event->handle);
  59. res = mono_win32_wait_for_single_object_ex (event->handle, timeout, alertable);
  60. if (res == WAIT_OBJECT_0)
  61. return MONO_OS_EVENT_WAIT_RET_SUCCESS_0;
  62. else if (res == WAIT_IO_COMPLETION)
  63. return MONO_OS_EVENT_WAIT_RET_ALERTED;
  64. else if (res == WAIT_TIMEOUT)
  65. return MONO_OS_EVENT_WAIT_RET_TIMEOUT;
  66. else if (res == WAIT_FAILED)
  67. g_error ("%s: mono_thread_win32_wait_one_handle failed with error %d", __func__, GetLastError ());
  68. else
  69. g_error ("%s: unknown res value %d", __func__, res);
  70. }
  71. MonoOSEventWaitRet
  72. mono_os_event_wait_multiple (MonoOSEvent **events, gsize nevents, gboolean waitall, guint32 timeout, gboolean alertable)
  73. {
  74. DWORD res;
  75. gpointer handles [MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS];
  76. gint i;
  77. g_assert (events);
  78. g_assert (nevents > 0);
  79. g_assert (nevents <= MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS);
  80. if (nevents == 1)
  81. return mono_os_event_wait_one (events [0], timeout, alertable);
  82. for (i = 0; i < nevents; ++i) {
  83. g_assert (events [i]);
  84. g_assert (events [i]->handle);
  85. handles [i] = events [i]->handle;
  86. }
  87. res = mono_win32_wait_for_multiple_objects_ex ((DWORD)nevents, handles, waitall, timeout, alertable);
  88. if (res >= WAIT_OBJECT_0 && res < WAIT_OBJECT_0 + MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS)
  89. return MONO_OS_EVENT_WAIT_RET_SUCCESS_0 + (res - WAIT_OBJECT_0);
  90. else if (res == WAIT_IO_COMPLETION)
  91. return MONO_OS_EVENT_WAIT_RET_ALERTED;
  92. else if (res == WAIT_TIMEOUT)
  93. return MONO_OS_EVENT_WAIT_RET_TIMEOUT;
  94. else if (res == WAIT_FAILED)
  95. g_error ("%s: mono_thread_win32_wait_multiple_handle failed with error %d", __func__, GetLastError ());
  96. else
  97. g_error ("%s: unknown res value %d", __func__, res);
  98. }