2
* WinPR: Windows Portable Runtime
5
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7
* Licensed under the Apache License, Version 2.0 (the "License");
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
28
#include <winpr/crt.h>
30
#include <winpr/collections.h>
33
* C equivalent of the C# CountdownEvent Class
34
* http://msdn.microsoft.com/en-us/library/dd235708/
42
* Gets the number of remaining signals required to set the event.
45
DWORD CountdownEvent_CurrentCount(wCountdownEvent* countdown)
47
return countdown->count;
51
* Gets the numbers of signals initially required to set the event.
54
DWORD CountdownEvent_InitialCount(wCountdownEvent* countdown)
56
return countdown->initialCount;
60
* Determines whether the event is set.
63
BOOL CountdownEvent_IsSet(wCountdownEvent* countdown)
67
if (WaitForSingleObject(countdown->event, 0) == WAIT_OBJECT_0)
74
* Gets a WaitHandle that is used to wait for the event to be set.
77
HANDLE CountdownEvent_WaitHandle(wCountdownEvent* countdown)
79
return countdown->event;
87
* Increments the CountdownEvent's current count by a specified value.
90
void CountdownEvent_AddCount(wCountdownEvent* countdown, DWORD signalCount)
92
EnterCriticalSection(&countdown->lock);
94
countdown->count += signalCount;
96
if (countdown->count > 0)
97
ResetEvent(countdown->event);
99
LeaveCriticalSection(&countdown->lock);
103
* Registers multiple signals with the CountdownEvent, decrementing the value of CurrentCount by the specified amount.
106
BOOL CountdownEvent_Signal(wCountdownEvent* countdown, DWORD signalCount)
112
status = newStatus = oldStatus = FALSE;
114
EnterCriticalSection(&countdown->lock);
116
if (WaitForSingleObject(countdown->event, 0) == WAIT_OBJECT_0)
119
if (signalCount <= countdown->count)
120
countdown->count -= signalCount;
122
countdown->count = 0;
124
if (countdown->count == 0)
127
if (newStatus && (!oldStatus))
129
SetEvent(countdown->event);
133
LeaveCriticalSection(&countdown->lock);
139
* Resets the InitialCount property to a specified value.
142
void CountdownEvent_Reset(wCountdownEvent* countdown, DWORD count)
144
countdown->initialCount = count;
148
* Construction, Destruction
151
wCountdownEvent* CountdownEvent_New(DWORD initialCount)
153
wCountdownEvent* countdown = NULL;
155
countdown = (wCountdownEvent*) malloc(sizeof(wCountdownEvent));
159
countdown->count = initialCount;
160
countdown->initialCount = initialCount;
161
InitializeCriticalSectionAndSpinCount(&countdown->lock, 4000);
162
countdown->event = CreateEvent(NULL, TRUE, FALSE, NULL);
164
if (countdown->count == 0)
165
SetEvent(countdown->event);
171
void CountdownEvent_Free(wCountdownEvent* countdown)
173
DeleteCriticalSection(&countdown->lock);
174
CloseHandle(countdown->event);