diff --git a/Foundation/src/Thread.cpp b/Foundation/src/Thread.cpp index ba8c22e99..a2df4961b 100644 --- a/Foundation/src/Thread.cpp +++ b/Foundation/src/Thread.cpp @@ -42,7 +42,11 @@ #if defined(POCO_OS_FAMILY_WINDOWS) +#if defined(_WIN32_WCE) +#include "Thread_WINCE.cpp" +#else #include "Thread_WIN32.cpp" +#endif #else #include "Thread_POSIX.cpp" #endif @@ -142,10 +146,15 @@ std::string Thread::makeName() } +namespace +{ + static FastMutex uniqueIdMutex; +} + + int Thread::uniqueId() { - static FastMutex mtx; - FastMutex::ScopedLock lock(mtx); + FastMutex::ScopedLock lock(uniqueIdMutex); static unsigned count = 0; ++count; diff --git a/Foundation/src/ThreadLocal.cpp b/Foundation/src/ThreadLocal.cpp index 97ee37c55..4c4bec3cc 100644 --- a/Foundation/src/ThreadLocal.cpp +++ b/Foundation/src/ThreadLocal.cpp @@ -70,12 +70,18 @@ TLSAbstractSlot*& ThreadLocalStorage::get(const void* key) { TLSMap::iterator it = _map.find(key); if (it == _map.end()) - return _map.insert(TLSMap::value_type(key, 0)).first->second; + return _map.insert(TLSMap::value_type(key, reinterpret_cast(0))).first->second; else return it->second; } +namespace +{ + static SingletonHolder sh; +} + + ThreadLocalStorage& ThreadLocalStorage::current() { Thread* pThread = Thread::current(); @@ -85,7 +91,6 @@ ThreadLocalStorage& ThreadLocalStorage::current() } else { - static SingletonHolder sh; return *sh.get(); } } diff --git a/Foundation/src/ThreadPool.cpp b/Foundation/src/ThreadPool.cpp index d0a21d3e8..ed675279a 100644 --- a/Foundation/src/ThreadPool.cpp +++ b/Foundation/src/ThreadPool.cpp @@ -42,6 +42,9 @@ #include "Poco/ErrorHandler.h" #include #include +#if defined(_WIN32_WCE) +#include "wce_time.h" +#endif namespace Poco { @@ -86,7 +89,11 @@ PooledThread::PooledThread(const std::string& name, int stackSize): { poco_assert_dbg (stackSize >= 0); _thread.setStackSize(stackSize); - _idleTime = time(NULL); +#if defined(_WIN32_WCE) + _idleTime = wceex_time(NULL); +#else + _idleTime = std::time(NULL); +#endif } @@ -149,7 +156,11 @@ int PooledThread::idleTime() { FastMutex::ScopedLock lock(_mutex); +#if defined(_WIN32_WCE) + return (int) (wceex_time(NULL) - _idleTime); +#else return (int) (time(NULL) - _idleTime); +#endif } @@ -175,6 +186,8 @@ void PooledThread::activate() void PooledThread::release() { + const long JOIN_TIMEOUT = 10000; + _mutex.lock(); _pTarget = 0; _mutex.unlock(); @@ -183,8 +196,11 @@ void PooledThread::release() // terminated the thread before we got here. if (_thread.isRunning()) _targetReady.set(); - else - delete this; + + if (_thread.tryJoin(JOIN_TIMEOUT)) + { + delete this; + } } @@ -216,7 +232,11 @@ void PooledThread::run() } FastMutex::ScopedLock lock(_mutex); _pTarget = 0; +#if defined(_WIN32_WCE) + _idleTime = wceex_time(NULL); +#else _idleTime = time(NULL); +#endif _idle = true; _targetCompleted.set(); ThreadLocalStorage::clear(); @@ -229,7 +249,6 @@ void PooledThread::run() break; } } - delete this; } @@ -505,9 +524,14 @@ private: }; -ThreadPool& ThreadPool::defaultPool() +namespace { static ThreadPoolSingletonHolder sh; +} + + +ThreadPool& ThreadPool::defaultPool() +{ return *sh.pool(); } diff --git a/Foundation/src/ThreadTarget.cpp b/Foundation/src/ThreadTarget.cpp index 7b1751219..83e16a745 100644 --- a/Foundation/src/ThreadTarget.cpp +++ b/Foundation/src/ThreadTarget.cpp @@ -1,66 +1,65 @@ -// -// ThreadTarget.cpp -// -// $Id: //poco/svn/Foundation/src/ThreadTarget.cpp#2 $ -// -// Library: Foundation -// Package: Threading -// Module: ThreadTarget -// -// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. -// and Contributors. -// -// Permission is hereby granted, free of charge, to any person or organization -// obtaining a copy of the software and accompanying documentation covered by -// this license (the "Software") to use, reproduce, display, distribute, -// execute, and transmit the Software, and to prepare derivative works of the -// Software, and to permit third-parties to whom the Software is furnished to -// do so, all subject to the following: -// -// The copyright notices in the Software and this entire statement, including -// the above license grant, this restriction and the following disclaimer, -// must be included in all copies of the Software, in whole or in part, and -// all derivative works of the Software, unless such copies or derivative -// works are solely in the form of machine-executable object code generated by -// a source language processor. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// - - -#include "Poco/ThreadTarget.h" - - - -namespace Poco { - - -ThreadTarget::ThreadTarget(Callback method): _method(method) -{ -} - - -ThreadTarget::ThreadTarget(const ThreadTarget& te): _method(te._method) -{ -} - - -ThreadTarget& ThreadTarget::operator = (const ThreadTarget& te) -{ - _method = te._method; - return *this; -} - - -ThreadTarget::~ThreadTarget() -{ -} - - -} // namespace Poco +// +// ThreadTarget.cpp +// +// $Id: ThreadTarget.cpp 762 2008-09-16 19:04:32Z obiltschnig $ +// +// Library: Foundation +// Package: Threading +// Module: ThreadTarget +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/ThreadTarget.h" + + +namespace Poco { + + +ThreadTarget::ThreadTarget(Callback method): _method(method) +{ +} + + +ThreadTarget::ThreadTarget(const ThreadTarget& te): _method(te._method) +{ +} + + +ThreadTarget& ThreadTarget::operator = (const ThreadTarget& te) +{ + _method = te._method; + return *this; +} + + +ThreadTarget::~ThreadTarget() +{ +} + + +} // namespace Poco diff --git a/Foundation/src/Thread_POSIX.cpp b/Foundation/src/Thread_POSIX.cpp index 09aea2873..2d0deaf83 100644 --- a/Foundation/src/Thread_POSIX.cpp +++ b/Foundation/src/Thread_POSIX.cpp @@ -37,24 +37,23 @@ #include "Poco/Thread_POSIX.h" #include "Poco/Exception.h" #include "Poco/ErrorHandler.h" -#include "Poco/Timestamp.h" #include "Poco/Timespan.h" +#include "Poco/Timestamp.h" #include #if defined(__sun) && defined(__SVR4) # if !defined(__EXTENSIONS__) # define __EXTENSIONS__ # endif - // must be limits.h for PTHREAD_STACK_MIN on Solaris -# include -#else -# include +#endif +#if POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_MAC_OS_X || POCO_OS == POCO_OS_QNX +# include #endif // // Block SIGPIPE in main thread. // -#if defined(POCO_OS_FAMILY_UNIX) +#if defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_VXWORKS) namespace { class SignalBlocker @@ -131,7 +130,9 @@ void ThreadImpl::setOSPriorityImpl(int prio) int ThreadImpl::getMinOSPriorityImpl() { -#if defined(__VMS) || defined(__digital__) +#if defined(POCO_THREAD_PRIORITY_MIN) + return POCO_THREAD_PRIORITY_MIN; +#elif defined(__VMS) || defined(__digital__) return PRI_OTHER_MIN; #else return sched_get_priority_min(SCHED_OTHER); @@ -141,7 +142,9 @@ int ThreadImpl::getMinOSPriorityImpl() int ThreadImpl::getMaxOSPriorityImpl() { -#if defined(__VMS) || defined(__digital__) +#if defined(POCO_THREAD_PRIORITY_MAX) + return POCO_THREAD_PRIORITY_MAX; +#elif defined(__VMS) || defined(__digital__) return PRI_OTHER_MAX; #else return sched_get_priority_max(SCHED_OTHER); @@ -265,6 +268,12 @@ ThreadImpl* ThreadImpl::currentImpl() } +ThreadImpl::TIDImpl ThreadImpl::currentTidImpl() +{ + return pthread_self(); +} + + void ThreadImpl::sleepImpl(long milliseconds) { #if defined(__VMS) || defined(__digital__) @@ -273,6 +282,28 @@ void ThreadImpl::sleepImpl(long milliseconds) interval.tv_sec = milliseconds / 1000; interval.tv_nsec = (milliseconds % 1000)*1000000; pthread_delay_np(&interval); +#elif POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_MAC_OS_X || POCO_OS == POCO_OS_QNX || POCO_OS == POCO_OS_VXWORKS + Poco::Timespan remainingTime(1000*Poco::Timespan::TimeDiff(milliseconds)); + int rc; + do + { + struct timespec ts; + ts.tv_sec = (long) remainingTime.totalSeconds(); + ts.tv_nsec = (long) remainingTime.useconds()*1000; + Poco::Timestamp start; + rc = ::nanosleep(&ts, 0); + if (rc < 0 && errno == EINTR) + { + Poco::Timestamp end; + Poco::Timespan waited = start.elapsed(); + if (waited < remainingTime) + remainingTime -= waited; + else + remainingTime = 0; + } + } + while (remainingTime > 0 && rc < 0 && errno == EINTR); + if (rc < 0 && remainingTime > 0) throw Poco::SystemException("Thread::sleep(): nanosleep() failed"); #else Poco::Timespan remainingTime(1000*Poco::Timespan::TimeDiff(milliseconds)); int rc; diff --git a/Foundation/src/Thread_WIN32.cpp b/Foundation/src/Thread_WIN32.cpp index 0da380b7a..2f377dc66 100644 --- a/Foundation/src/Thread_WIN32.cpp +++ b/Foundation/src/Thread_WIN32.cpp @@ -40,6 +40,52 @@ #include +#if defined(_DEBUG) && defined(POCO_WIN32_DEBUGGER_THREAD_NAMES) + + +namespace +{ + /// See + /// and for + /// more information on the code below. + + const DWORD MS_VC_EXCEPTION = 0x406D1388; + + #pragma pack(push,8) + typedef struct tagTHREADNAME_INFO + { + DWORD dwType; // Must be 0x1000. + LPCSTR szName; // Pointer to name (in user addr space). + DWORD dwThreadID; // Thread ID (-1=caller thread). + DWORD dwFlags; // Reserved for future use, must be zero. + } THREADNAME_INFO; + #pragma pack(pop) + + void setThreadName(DWORD dwThreadID, const char* threadName) + { + if (IsDebuggerPresent()) + { + THREADNAME_INFO info; + info.dwType = 0x1000; + info.szName = threadName; + info.dwThreadID = dwThreadID; + info.dwFlags = 0; + + __try + { + RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info); + } + __except (EXCEPTION_CONTINUE_EXECUTION) + { + } + } + } +} + + +#endif + + namespace Poco { @@ -49,6 +95,7 @@ ThreadImpl::CurrentThreadHolder ThreadImpl::_currentThreadHolder; ThreadImpl::ThreadImpl(): _pRunnableTarget(0), _thread(0), + _threadId(0), _prio(PRIO_NORMAL_IMPL), _stackSize(POCO_THREAD_STACK_SIZE) { @@ -107,11 +154,11 @@ void ThreadImpl::startImpl(Callable target, void* pData) void ThreadImpl::createImpl(Entry ent, void* pData) { #if defined(_DLL) - DWORD threadId; - _thread = CreateThread(NULL, _stackSize, ent, pData, 0, &threadId); + _thread = CreateThread(NULL, _stackSize, ent, pData, 0, &_threadId); #else unsigned threadId; _thread = (HANDLE) _beginthreadex(NULL, _stackSize, ent, this, 0, &threadId); + _threadId = static_cast(threadId); #endif if (!_thread) throw SystemException("cannot create thread"); @@ -176,6 +223,12 @@ ThreadImpl* ThreadImpl::currentImpl() } +ThreadImpl::TIDImpl ThreadImpl::currentTidImpl() +{ + return GetCurrentThreadId(); +} + + #if defined(_DLL) DWORD WINAPI ThreadImpl::runnableEntry(LPVOID pThread) #else @@ -183,6 +236,9 @@ unsigned __stdcall ThreadImpl::runnableEntry(void* pThread) #endif { _currentThreadHolder.set(reinterpret_cast(pThread)); +#if defined(_DEBUG) && defined(POCO_WIN32_DEBUGGER_THREAD_NAMES) + setThreadName(-1, reinterpret_cast(pThread)->getName().c_str()); +#endif try { reinterpret_cast(pThread)->_pRunnableTarget->run(); @@ -210,6 +266,9 @@ unsigned __stdcall ThreadImpl::callableEntry(void* pThread) #endif { _currentThreadHolder.set(reinterpret_cast(pThread)); +#if defined(_DEBUG) && defined(POCO_WIN32_DEBUGGER_THREAD_NAMES) + setThreadName(-1, reinterpret_cast(pThread)->getName().c_str()); +#endif try { ThreadImpl* pTI = reinterpret_cast(pThread);