summaryrefslogtreecommitdiff
path: root/cpp/src/IceUtil/CountDownLatch.cpp
blob: 41a9d60abe6ef9e49d89a2d121f1f1bdfe47ae0d (plain)
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
// **********************************************************************
//
// Copyright (c) 2004
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#include <IceUtil/CountDownLatch.h>
#include <IceUtil/ThreadException.h>

IceUtil::CountDownLatch::CountDownLatch(int count) :
    _count(count)
{
    if(count < 0)
    {
	throw Exception(__FILE__, __LINE__);
    }

#ifdef _WIN32
    _event = CreateEvent(NULL, TRUE, FALSE, NULL);
    if(_event == NULL)
    {
	throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
    }
#else
    int rc = pthread_mutex_init(&_mutex, 0);
    if(rc != 0)
    {
	throw ThreadSyscallException(__FILE__, __LINE__, rc);
    }
    
    rc = pthread_cond_init(&_cond, 0);
    if(rc != 0)
    {
	throw ThreadSyscallException(__FILE__, __LINE__, rc);
    }   
#endif
}


void 
IceUtil::CountDownLatch::await() const
{
#ifdef _WIN32
    while(InterlockedExchangeAdd(&_count, 0) > 0)
    {
	DWORD rc = WaitForSingleObject(_event, INFINITE);
	assert(rc == WAIT_OBJECT_0 || rc == WAIT_FAILED);
	
	if(rc == WAIT_FAILED)
	{
	    throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
	}
    }
#else
    lock();
    while(_count > 0)
    {
	int rc = pthread_cond_wait(&_cond, &_mutex);
	if(rc != 0)
	{
	    pthread_mutex_unlock(&_mutex);
	    throw ThreadSyscallException(__FILE__, __LINE__, rc);
	}
    }
    unlock();
    
#endif
}

void 
IceUtil::CountDownLatch::countDown()
{
#ifdef _WIN32
    if(InterlockedDecrement(&_count) == 0)
    {
	if(SetEvent(_event) == 0)
	{
	    throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
	}
    }
#else
    bool broadcast = false;

    lock();
    if(_count > 0 && --_count == 0)
    {
	broadcast = true;
    }
    unlock();
    
    if(broadcast)
    {
	int rc = pthread_cond_broadcast(&_cond);
	if(rc != 0)
	{
	    pthread_mutex_unlock(&_mutex);
	    throw ThreadSyscallException(__FILE__, __LINE__, rc);
	}
    }
#endif
}

int 
IceUtil::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
IceUtil::CountDownLatch::lock() const
{
    int rc = pthread_mutex_lock(&_mutex);
    if(rc != 0)
    {
	throw ThreadSyscallException(__FILE__, __LINE__, rc);
    }
}

void
IceUtil::CountDownLatch::unlock() const
{
    int rc = pthread_mutex_unlock(&_mutex);
    if(rc != 0)
    {
	throw ThreadSyscallException(__FILE__, __LINE__, rc);
    }
}

#endif