1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
// **********************************************************************
//
// Copyright (c) 2003-present ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceUtil/CountDownLatch.h>
#include <IceUtil/ThreadException.h>
IceUtilInternal::CountDownLatch::CountDownLatch(int count) :
_count(count)
{
if(count < 0)
{
throw IceUtil::IllegalArgumentException(__FILE__, __LINE__, "count must be greather than 0");
}
#ifdef _WIN32
# ifndef ICE_OS_UWP
_event = CreateEvent(0, TRUE, FALSE, 0);
# else
_event = CreateEventExW(0, 0, CREATE_EVENT_MANUAL_RESET, SEMAPHORE_ALL_ACCESS);
# endif
if(_event == 0)
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
#else
int rc = pthread_mutex_init(&_mutex, 0);
if(rc != 0)
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, rc);
}
rc = pthread_cond_init(&_cond, 0);
if(rc != 0)
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, rc);
}
#endif
}
IceUtilInternal::CountDownLatch::~CountDownLatch()
{
#ifdef _WIN32
CloseHandle(_event);
#else
# ifndef NDEBUG
int rc = pthread_mutex_destroy(&_mutex);
assert(rc == 0);
rc = pthread_cond_destroy(&_cond);
assert(rc == 0);
# else
pthread_mutex_destroy(&_mutex);
pthread_cond_destroy(&_cond);
# endif
#endif
}
void
IceUtilInternal::CountDownLatch::await() const
{
#ifdef _WIN32
while(InterlockedExchangeAdd(&_count, 0) > 0)
{
# ifndef ICE_OS_UWP
DWORD rc = WaitForSingleObject(_event, INFINITE);
# else
DWORD rc = WaitForSingleObjectEx(_event, INFINITE, false);
# endif
assert(rc == WAIT_OBJECT_0 || rc == WAIT_FAILED);
if(rc == WAIT_FAILED)
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
}
#else
lock();
while(_count > 0)
{
int rc = pthread_cond_wait(&_cond, &_mutex);
if(rc != 0)
{
pthread_mutex_unlock(&_mutex);
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, rc);
}
}
unlock();
#endif
}
void
IceUtilInternal::CountDownLatch::countDown()
{
#ifdef _WIN32
if(InterlockedDecrement(&_count) == 0)
{
if(SetEvent(_event) == 0)
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, GetLastError());
}
}
#else
bool broadcast = false;
lock();
if(_count > 0 && --_count == 0)
{
broadcast = true;
}
#if defined(__APPLE__)
//
// On macOS we do the broadcast with the mutex held. This seems to
// be necessary to prevent the broadcast call to hang (spinning in
// an infinite loop).
//
if(broadcast)
{
int rc = pthread_cond_broadcast(&_cond);
if(rc != 0)
{
pthread_mutex_unlock(&_mutex);
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, rc);
}
}
unlock();
#else
unlock();
if(broadcast)
{
int rc = pthread_cond_broadcast(&_cond);
if(rc != 0)
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, rc);
}
}
#endif
#endif
}
int
IceUtilInternal::CountDownLatch::getCount() const
{
#ifdef _WIN32
int count = InterlockedExchangeAdd(&_count, 0);
return count > 0 ? count : 0;
#else
lock();
int result = _count;
unlock();
return result;
#endif
}
#ifndef _WIN32
void
IceUtilInternal::CountDownLatch::lock() const
{
int rc = pthread_mutex_lock(&_mutex);
if(rc != 0)
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, rc);
}
}
void
IceUtilInternal::CountDownLatch::unlock() const
{
int rc = pthread_mutex_unlock(&_mutex);
if(rc != 0)
{
throw IceUtil::ThreadSyscallException(__FILE__, __LINE__, rc);
}
}
#endif
|