summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorBernard Normier <bernard@zeroc.com>2006-01-31 01:51:15 +0000
committerBernard Normier <bernard@zeroc.com>2006-01-31 01:51:15 +0000
commit4d7448e622a41c5a6e5fd5f05a20118036139fae (patch)
tree52aa4020efa36cf3b7e63ecbddb41ddc2146f65c /cpp
parentDisabled unicode test (diff)
downloadice-4d7448e622a41c5a6e5fd5f05a20118036139fae.tar.bz2
ice-4d7448e622a41c5a6e5fd5f05a20118036139fae.tar.xz
ice-4d7448e622a41c5a6e5fd5f05a20118036139fae.zip
Moved isAlive from ThreadControl to Thread
Diffstat (limited to 'cpp')
-rw-r--r--cpp/include/IceUtil/Thread.h14
-rw-r--r--cpp/src/Ice/ConnectionI.cpp2
-rw-r--r--cpp/src/Ice/ThreadPool.cpp28
-rw-r--r--cpp/src/Ice/ThreadPool.h2
-rw-r--r--cpp/src/IceUtil/Thread.cpp86
-rw-r--r--cpp/test/IceUtil/thread/AliveTest.cpp4
-rw-r--r--cpp/test/IceUtil/thread/CountDownLatchTest.cpp4
7 files changed, 83 insertions, 57 deletions
diff --git a/cpp/include/IceUtil/Thread.h b/cpp/include/IceUtil/Thread.h
index bd7fbcfee7d..47d71dd9942 100644
--- a/cpp/include/IceUtil/Thread.h
+++ b/cpp/include/IceUtil/Thread.h
@@ -98,10 +98,22 @@ public:
bool operator!=(const Thread&) const;
bool operator<(const Thread&) const;
+ //
+ // Is this thread still running?
+ //
+ bool isAlive() const;
+
+ //
+ // This function is an implementation detail;
+ // do not call it.
+ //
+ void done();
+
protected:
Mutex _stateMutex;
-
bool _started;
+ bool _running;
+
#ifdef _WIN32
HANDLE _handle;
DWORD _id;
diff --git a/cpp/src/Ice/ConnectionI.cpp b/cpp/src/Ice/ConnectionI.cpp
index 2eddb307408..7cd959f8500 100644
--- a/cpp/src/Ice/ConnectionI.cpp
+++ b/cpp/src/Ice/ConnectionI.cpp
@@ -317,7 +317,7 @@ Ice::ConnectionI::isFinished() const
}
if(_transceiver || _dispatchCount != 0 ||
- (_threadPerConnection && _threadPerConnection->getThreadControl().isAlive()))
+ (_threadPerConnection && _threadPerConnection->isAlive()))
{
return false;
}
diff --git a/cpp/src/Ice/ThreadPool.cpp b/cpp/src/Ice/ThreadPool.cpp
index 4ea369ed0b0..7b9f6fbbe8e 100644
--- a/cpp/src/Ice/ThreadPool.cpp
+++ b/cpp/src/Ice/ThreadPool.cpp
@@ -95,7 +95,8 @@ IceInternal::ThreadPool::ThreadPool(const InstancePtr& instance, const string& p
for(int i = 0 ; i < _size ; ++i)
{
IceUtil::ThreadPtr thread = new EventHandlerThread(this);
- _threads.push_back(thread->start(_stackSize));
+ thread->start(_stackSize);
+ _threads.push_back(thread);
++_running;
}
}
@@ -202,7 +203,8 @@ IceInternal::ThreadPool::promoteFollower()
try
{
IceUtil::ThreadPtr thread = new EventHandlerThread(this);
- _threads.push_back(thread->start(_stackSize));
+ thread->start(_stackSize);
+ _threads.push_back(thread);
++_running;
}
catch(const IceUtil::Exception& ex)
@@ -225,14 +227,10 @@ IceInternal::ThreadPool::joinWithAllThreads()
// threads would never terminate.)
//
assert(_destroyed);
-#if defined(_MSC_VER) && _MSC_VER <= 1200 // The mem_fun_ref below does not work with VC++ 6.0
- for(vector<IceUtil::ThreadControl>::iterator p = _threads.begin(); p != _threads.end(); ++p)
+ for(vector<IceUtil::ThreadPtr>::iterator p = _threads.begin(); p != _threads.end(); ++p)
{
- p->join();
+ (*p)->getThreadControl().join();
}
-#else
- for_each(_threads.begin(), _threads.end(), mem_fun_ref(&IceUtil::ThreadControl::join));
-#endif
}
string
@@ -667,16 +665,14 @@ IceInternal::ThreadPool::run()
assert(_running <= sz);
if(_running < sz)
{
- vector<IceUtil::ThreadControl>::iterator start =
- partition(_threads.begin(), _threads.end(), mem_fun_ref(&IceUtil::ThreadControl::isAlive));
-#if defined(_MSC_VER) && _MSC_VER <= 1200 // The mem_fun_ref below does not work with VC++ 6.0
- for(vector<IceUtil::ThreadControl>::iterator p = start; p != _threads.end(); ++p)
+ vector<IceUtil::ThreadPtr>::iterator start =
+ partition(_threads.begin(), _threads.end(), IceUtil::constMemFun(&IceUtil::Thread::isAlive));
+
+ for(vector<IceUtil::ThreadPtr>::iterator p = start; p != _threads.end(); ++p)
{
- p->join();
+ (*p)->getThreadControl().join();
}
-#else
- for_each(start, _threads.end(), mem_fun_ref(&IceUtil::ThreadControl::join));
-#endif
+
_threads.erase(start, _threads.end());
}
diff --git a/cpp/src/Ice/ThreadPool.h b/cpp/src/Ice/ThreadPool.h
index 41578b77250..de4cd4fa2e9 100644
--- a/cpp/src/Ice/ThreadPool.h
+++ b/cpp/src/Ice/ThreadPool.h
@@ -93,7 +93,7 @@ private:
const size_t _stackSize;
- std::vector<IceUtil::ThreadControl> _threads; // Control for all threads, running or not.
+ std::vector<IceUtil::ThreadPtr> _threads; // All threads, running or not.
int _running; // Number of running threads.
int _inUse; // Number of threads that are currently in use.
double _load; // Current load in number of threads.
diff --git a/cpp/src/IceUtil/Thread.cpp b/cpp/src/IceUtil/Thread.cpp
index 8c10fbe8475..933d543d6be 100644
--- a/cpp/src/IceUtil/Thread.cpp
+++ b/cpp/src/IceUtil/Thread.cpp
@@ -120,16 +120,6 @@ IceUtil::ThreadControl::detach()
// is closed.
}
-bool
-IceUtil::ThreadControl::isAlive() const
-{
- DWORD rc;
- if(GetExitCodeThread(_handle, &rc) == 0)
- {
- return false;
- }
- return rc == STILL_ACTIVE;
-}
void
IceUtil::ThreadControl::sleep(const Time& timeout)
@@ -150,6 +140,7 @@ IceUtil::ThreadControl::yield()
IceUtil::Thread::Thread() :
_started(false),
+ _running(false)
_handle(0),
_id(0)
{
@@ -167,6 +158,11 @@ IceUtil::Thread::~Thread()
static unsigned int
WINAPI startHook(void* arg)
{
+ // Ensure that the thread doesn't go away until run() has
+ // completed.
+ //
+ IceUtil::ThreadPtr thread;
+
try
{
IceUtil::Thread* rawThread = static_cast<IceUtil::Thread*>(arg);
@@ -181,7 +177,7 @@ WINAPI startHook(void* arg)
// Ensure that the thread doesn't go away until run() has
// completed.
//
- IceUtil::ThreadPtr thread = rawThread;
+ thread = rawThread;
//
// See the comment in IceUtil::Thread::start() for details.
@@ -193,7 +189,9 @@ WINAPI startHook(void* arg)
{
cerr << "IceUtil::Thread::run(): uncaught exception: ";
cerr << e << endl;
- }
+ }
+ thread->done();
+
return 0;
}
@@ -241,6 +239,7 @@ IceUtil::Thread::start(size_t stackSize)
}
_started = true;
+ _running = true;
return ThreadControl(_handle, _id);
}
@@ -274,6 +273,21 @@ IceUtil::Thread::operator<(const Thread& rhs) const
return this < &rhs;
}
+bool
+IceUtil::Thread::isAlive() const
+{
+ IceUtil::Mutex::Lock lock(_stateMutex);
+ return _running;
+}
+
+void
+IceUtil::Thread::done()
+{
+ IceUtil::Mutex::Lock lock(_stateMutex);
+ _running = false;
+}
+
+
#else
IceUtil::ThreadControl::ThreadControl(pthread_t thread) :
@@ -338,22 +352,6 @@ IceUtil::ThreadControl::detach()
}
}
-bool
-IceUtil::ThreadControl::isAlive() const
-{
- int policy;
- int ret;
- struct sched_param param;
- ret = pthread_getschedparam(_thread, &policy, &param);
-#ifdef __APPLE__
- if (ret == 0)
- {
- ret = pthread_setschedparam(_thread, policy, &param);
- }
-#endif
- return (ret == 0);
-}
-
void
IceUtil::ThreadControl::sleep(const Time& timeout)
{
@@ -371,7 +369,8 @@ IceUtil::ThreadControl::yield()
}
IceUtil::Thread::Thread() :
- _started(false)
+ _started(false),
+ _running(false)
{
}
@@ -385,15 +384,17 @@ extern "C"
static void*
startHook(void* arg)
{
+ //
+ // Ensure that the thread doesn't go away until run() has
+ // completed.
+ //
+ IceUtil::ThreadPtr thread;
+
try
{
IceUtil::Thread* rawThread = static_cast<IceUtil::Thread*>(arg);
- //
- // Ensure that the thread doesn't go away until run() has
- // completed.
- //
- IceUtil::ThreadPtr thread = rawThread;
+ thread = rawThread;
//
// See the comment in IceUtil::Thread::start() for details.
@@ -410,6 +411,8 @@ startHook(void* arg)
{
cerr << "IceUtil::Thread::run(): uncaught exception" << endl;
}
+ thread->done();
+
return 0;
}
}
@@ -473,6 +476,7 @@ IceUtil::Thread::start(size_t stackSize)
}
_started = true;
+ _running = true;
return ThreadControl(_thread);
}
@@ -506,4 +510,18 @@ IceUtil::Thread::operator<(const Thread& rhs) const
return this < &rhs;
}
+bool
+IceUtil::Thread::isAlive() const
+{
+ IceUtil::Mutex::Lock lock(_stateMutex);
+ return _running;
+}
+
+void
+IceUtil::Thread::done()
+{
+ IceUtil::Mutex::Lock lock(_stateMutex);
+ _running = false;
+}
+
#endif
diff --git a/cpp/test/IceUtil/thread/AliveTest.cpp b/cpp/test/IceUtil/thread/AliveTest.cpp
index 7a262d50a4e..50af56a70b4 100644
--- a/cpp/test/IceUtil/thread/AliveTest.cpp
+++ b/cpp/test/IceUtil/thread/AliveTest.cpp
@@ -92,8 +92,8 @@ AliveTest::run()
AliveTestThreadPtr t = new AliveTestThread(childCreated, parentReady);
IceUtil::ThreadControl c = t->start();
childCreated.waitForSignal();
- test(c.isAlive());
+ test(t->isAlive());
parentReady.signal();
c.join();
- test(!c.isAlive());
+ test(!t->isAlive());
}
diff --git a/cpp/test/IceUtil/thread/CountDownLatchTest.cpp b/cpp/test/IceUtil/thread/CountDownLatchTest.cpp
index 67189ab6420..1ae42098638 100644
--- a/cpp/test/IceUtil/thread/CountDownLatchTest.cpp
+++ b/cpp/test/IceUtil/thread/CountDownLatchTest.cpp
@@ -105,12 +105,12 @@ CountDownLatchTest::run()
for(i = 0; i < wave1Count; i++)
{
- test(t1[i]->getThreadControl().isAlive());
+ test(t1[i]->isAlive());
}
for(i = 0; i < fullCount - 1; i++)
{
- test(t2[i]->getThreadControl().isAlive());
+ test(t2[i]->isAlive());
}
} while(latch.getCount() > 1);