Differences From Artifact [848a636ef0]:
- File src/platform/windows/condition.m — part of check-in [109db78ecd] at 2020-02-29 14:49:23 on branch trunk — Move platform specific files into a subdirectory (user: js, size: 2753) [annotate] [blame] [check-ins using] [more...]
To Artifact [bcc658da38]:
- File
src/platform/windows/condition.m
— part of check-in
[5b37fbeb82]
at
2020-12-20 21:26:08
on branch trunk
— Return error instead of using errno for threading
errno is problematic for Amiga libraries and is also not thread-safe on
some systems, even though it should. (user: js, size: 2671) [annotate] [blame] [check-ins using] [more...]
︙ | ︙ | |||
19 20 21 22 23 24 25 | #include <errno.h> #import "condition.h" #include <windows.h> | < > | < | | < | < > < | | < > < | | < > > | | < | < > > | | < | < | < > | < | | < | | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | #include <errno.h> #import "condition.h" #include <windows.h> int of_condition_new(of_condition_t *condition) { condition->count = 0; if ((condition->event = CreateEvent(NULL, FALSE, 0, NULL)) == NULL) return EAGAIN; return 0; } int of_condition_signal(of_condition_t *condition) { if (!SetEvent(condition->event)) { switch (GetLastError()) { case ERROR_INVALID_HANDLE: return EINVAL; default: OF_ENSURE(0); } } return 0; } int of_condition_broadcast(of_condition_t *condition) { int count = condition->count; for (int i = 0; i < count; i++) { if (!SetEvent(condition->event)) { switch (GetLastError()) { case ERROR_INVALID_HANDLE: return EINVAL; default: OF_ENSURE(0); } } } return 0; } int of_condition_wait(of_condition_t *condition, of_mutex_t *mutex) { int error; DWORD status; if ((error = of_mutex_unlock(mutex)) != 0) return error; of_atomic_int_inc(&condition->count); status = WaitForSingleObject(condition->event, INFINITE); of_atomic_int_dec(&condition->count); switch (status) { case WAIT_OBJECT_0: return of_mutex_lock(mutex); case WAIT_FAILED: switch (GetLastError()) { case ERROR_INVALID_HANDLE: return EINVAL; default: OF_ENSURE(0); } default: OF_ENSURE(0); } } int of_condition_timed_wait(of_condition_t *condition, of_mutex_t *mutex, of_time_interval_t timeout) { int error; DWORD status; if ((error = of_mutex_unlock(mutex)) != 0) return error; of_atomic_int_inc(&condition->count); status = WaitForSingleObject(condition->event, timeout * 1000); of_atomic_int_dec(&condition->count); switch (status) { case WAIT_OBJECT_0: return of_mutex_lock(mutex); case WAIT_TIMEOUT: return ETIMEDOUT; case WAIT_FAILED: switch (GetLastError()) { case ERROR_INVALID_HANDLE: return EINVAL; default: OF_ENSURE(0); } default: OF_ENSURE(0); } } int of_condition_free(of_condition_t *condition) { if (condition->count != 0) return EBUSY; return (CloseHandle(condition->event) ? 0 : EINVAL); } |