diff options
author | Bernard Normier <bernard@zeroc.com> | 2004-04-19 20:55:36 +0000 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2004-04-19 20:55:36 +0000 |
commit | 8e39b811b7b71c2e5cf2f623b5d108a62d882d5e (patch) | |
tree | 16df76b8ecdd90dae6557b447dcd3847df381757 /cpp/src/IceUtil/CountDownLatch.cpp | |
parent | Changed looking detection policy to DB_LOCK_YOUNGEST (diff) | |
download | ice-8e39b811b7b71c2e5cf2f623b5d108a62d882d5e.tar.bz2 ice-8e39b811b7b71c2e5cf2f623b5d108a62d882d5e.tar.xz ice-8e39b811b7b71c2e5cf2f623b5d108a62d882d5e.zip |
Added keep, keepFacet, remove and removeFacet to Evictor; implemented
IceUtil::Cache properly
Diffstat (limited to 'cpp/src/IceUtil/CountDownLatch.cpp')
-rw-r--r-- | cpp/src/IceUtil/CountDownLatch.cpp | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/cpp/src/IceUtil/CountDownLatch.cpp b/cpp/src/IceUtil/CountDownLatch.cpp new file mode 100644 index 00000000000..6e1f37d8646 --- /dev/null +++ b/cpp/src/IceUtil/CountDownLatch.cpp @@ -0,0 +1,148 @@ +// ********************************************************************** +// +// 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 + int* countPtr = const_cast<int*>(&_count); + while(InterlockedCompareExchange(countPtr, 0, 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* countPtr = const_cast<int*>(&_count); + int count = InterlockedCompareExchange(countPtr, 0, 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 |