4
* Copyright Ericsson AB 2009-2010. All Rights Reserved.
6
* The contents of this file are subject to the Erlang Public License,
7
* Version 1.1, (the "License"); you may not use this file except in
8
* compliance with the License. You should have received a copy of the
9
* Erlang Public License along with this software. If not, it can be
10
* retrieved online at http://www.erlang.org/.
12
* Software distributed under the License is distributed on an "AS IS"
13
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
* the License for the specific language governing rights and limitations
21
* Author: Rickard Green
24
#define ETHR_INLINE_FUNC_NAME_(X) X ## __
25
#define ETHR_EVENT_IMPL__
29
/* --- Windows implementation of thread events ------------------------------ */
31
#pragma intrinsic(_InterlockedExchangeAdd)
32
#pragma intrinsic(_InterlockedCompareExchange)
35
ethr_event_init(ethr_event *e)
37
e->state = ETHR_EVENT_OFF__;
38
e->handle = CreateEvent(NULL, FALSE, FALSE, NULL);
39
if (e->handle == INVALID_HANDLE_VALUE)
40
return ethr_win_get_errno__();
45
ethr_event_destroy(ethr_event *e)
47
BOOL res = CloseHandle(e->handle);
48
return res == 0 ? ethr_win_get_errno__() : 0;
52
ethr_event_set(ethr_event *e)
58
ethr_event_reset(ethr_event *e)
60
ethr_event_reset__(e);
63
static ETHR_INLINE int
64
wait(ethr_event *e, int spincount)
68
int sc, res, until_yield = ETHR_YIELD_AFTER_BUSY_LOOPS;
71
ETHR_FATAL_ERROR__(EINVAL);
78
#if ETHR_READ_AND_SET_WITHOUT_INTERLOCKED_OP__
81
state = _InterlockedExchangeAdd(&e->state, (LONG) 0);
83
if (state == ETHR_EVENT_ON__)
89
if (--until_yield == 0) {
90
until_yield = ETHR_YIELD_AFTER_BUSY_LOOPS;
93
ETHR_FATAL_ERROR__(res);
97
if (state != ETHR_EVENT_OFF_WAITER__) {
98
state = _InterlockedCompareExchange(&e->state,
99
ETHR_EVENT_OFF_WAITER__,
101
if (state == ETHR_EVENT_ON__)
103
ETHR_ASSERT(state == ETHR_EVENT_OFF__);
106
code = WaitForSingleObject(e->handle, INFINITE);
107
if (code != WAIT_OBJECT_0)
108
ETHR_FATAL_ERROR__(ethr_win_get_errno__());
114
ethr_event_wait(ethr_event *e)
120
ethr_event_swait(ethr_event *e, int spincount)
122
return wait(e, spincount);