diff options
author | Bernard Normier <bernard@zeroc.com> | 2006-01-31 01:51:15 +0000 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2006-01-31 01:51:15 +0000 |
commit | 4d7448e622a41c5a6e5fd5f05a20118036139fae (patch) | |
tree | 52aa4020efa36cf3b7e63ecbddb41ddc2146f65c /cpp/src | |
parent | Disabled unicode test (diff) | |
download | ice-4d7448e622a41c5a6e5fd5f05a20118036139fae.tar.bz2 ice-4d7448e622a41c5a6e5fd5f05a20118036139fae.tar.xz ice-4d7448e622a41c5a6e5fd5f05a20118036139fae.zip |
Moved isAlive from ThreadControl to Thread
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/Ice/ConnectionI.cpp | 2 | ||||
-rw-r--r-- | cpp/src/Ice/ThreadPool.cpp | 28 | ||||
-rw-r--r-- | cpp/src/Ice/ThreadPool.h | 2 | ||||
-rw-r--r-- | cpp/src/IceUtil/Thread.cpp | 86 |
4 files changed, 66 insertions, 52 deletions
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, ¶m); -#ifdef __APPLE__ - if (ret == 0) - { - ret = pthread_setschedparam(_thread, policy, ¶m); - } -#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 |