trunk/branch integration: VxWorks & Wince

This commit is contained in:
Marian Krivos 2011-08-22 18:03:37 +00:00
parent 68306c2960
commit 74019e51a9
6 changed files with 212 additions and 85 deletions

View File

@ -42,7 +42,11 @@
#if defined(POCO_OS_FAMILY_WINDOWS) #if defined(POCO_OS_FAMILY_WINDOWS)
#if defined(_WIN32_WCE)
#include "Thread_WINCE.cpp"
#else
#include "Thread_WIN32.cpp" #include "Thread_WIN32.cpp"
#endif
#else #else
#include "Thread_POSIX.cpp" #include "Thread_POSIX.cpp"
#endif #endif
@ -142,10 +146,15 @@ std::string Thread::makeName()
} }
namespace
{
static FastMutex uniqueIdMutex;
}
int Thread::uniqueId() int Thread::uniqueId()
{ {
static FastMutex mtx; FastMutex::ScopedLock lock(uniqueIdMutex);
FastMutex::ScopedLock lock(mtx);
static unsigned count = 0; static unsigned count = 0;
++count; ++count;

View File

@ -70,12 +70,18 @@ TLSAbstractSlot*& ThreadLocalStorage::get(const void* key)
{ {
TLSMap::iterator it = _map.find(key); TLSMap::iterator it = _map.find(key);
if (it == _map.end()) if (it == _map.end())
return _map.insert(TLSMap::value_type(key, 0)).first->second; return _map.insert(TLSMap::value_type(key, reinterpret_cast<Poco::TLSAbstractSlot*>(0))).first->second;
else else
return it->second; return it->second;
} }
namespace
{
static SingletonHolder<ThreadLocalStorage> sh;
}
ThreadLocalStorage& ThreadLocalStorage::current() ThreadLocalStorage& ThreadLocalStorage::current()
{ {
Thread* pThread = Thread::current(); Thread* pThread = Thread::current();
@ -85,7 +91,6 @@ ThreadLocalStorage& ThreadLocalStorage::current()
} }
else else
{ {
static SingletonHolder<ThreadLocalStorage> sh;
return *sh.get(); return *sh.get();
} }
} }

View File

@ -42,6 +42,9 @@
#include "Poco/ErrorHandler.h" #include "Poco/ErrorHandler.h"
#include <sstream> #include <sstream>
#include <ctime> #include <ctime>
#if defined(_WIN32_WCE)
#include "wce_time.h"
#endif
namespace Poco { namespace Poco {
@ -86,7 +89,11 @@ PooledThread::PooledThread(const std::string& name, int stackSize):
{ {
poco_assert_dbg (stackSize >= 0); poco_assert_dbg (stackSize >= 0);
_thread.setStackSize(stackSize); _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); FastMutex::ScopedLock lock(_mutex);
#if defined(_WIN32_WCE)
return (int) (wceex_time(NULL) - _idleTime);
#else
return (int) (time(NULL) - _idleTime); return (int) (time(NULL) - _idleTime);
#endif
} }
@ -175,6 +186,8 @@ void PooledThread::activate()
void PooledThread::release() void PooledThread::release()
{ {
const long JOIN_TIMEOUT = 10000;
_mutex.lock(); _mutex.lock();
_pTarget = 0; _pTarget = 0;
_mutex.unlock(); _mutex.unlock();
@ -183,8 +196,11 @@ void PooledThread::release()
// terminated the thread before we got here. // terminated the thread before we got here.
if (_thread.isRunning()) if (_thread.isRunning())
_targetReady.set(); _targetReady.set();
else
delete this; if (_thread.tryJoin(JOIN_TIMEOUT))
{
delete this;
}
} }
@ -216,7 +232,11 @@ void PooledThread::run()
} }
FastMutex::ScopedLock lock(_mutex); FastMutex::ScopedLock lock(_mutex);
_pTarget = 0; _pTarget = 0;
#if defined(_WIN32_WCE)
_idleTime = wceex_time(NULL);
#else
_idleTime = time(NULL); _idleTime = time(NULL);
#endif
_idle = true; _idle = true;
_targetCompleted.set(); _targetCompleted.set();
ThreadLocalStorage::clear(); ThreadLocalStorage::clear();
@ -229,7 +249,6 @@ void PooledThread::run()
break; break;
} }
} }
delete this;
} }
@ -505,9 +524,14 @@ private:
}; };
ThreadPool& ThreadPool::defaultPool() namespace
{ {
static ThreadPoolSingletonHolder sh; static ThreadPoolSingletonHolder sh;
}
ThreadPool& ThreadPool::defaultPool()
{
return *sh.pool(); return *sh.pool();
} }

View File

@ -1,13 +1,13 @@
// //
// ThreadTarget.cpp // ThreadTarget.cpp
// //
// $Id: //poco/svn/Foundation/src/ThreadTarget.cpp#2 $ // $Id: ThreadTarget.cpp 762 2008-09-16 19:04:32Z obiltschnig $
// //
// Library: Foundation // Library: Foundation
// Package: Threading // Package: Threading
// Module: ThreadTarget // Module: ThreadTarget
// //
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
// //
// Permission is hereby granted, free of charge, to any person or organization // Permission is hereby granted, free of charge, to any person or organization
@ -37,7 +37,6 @@
#include "Poco/ThreadTarget.h" #include "Poco/ThreadTarget.h"
namespace Poco { namespace Poco {

View File

@ -37,24 +37,23 @@
#include "Poco/Thread_POSIX.h" #include "Poco/Thread_POSIX.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include "Poco/ErrorHandler.h" #include "Poco/ErrorHandler.h"
#include "Poco/Timestamp.h"
#include "Poco/Timespan.h" #include "Poco/Timespan.h"
#include "Poco/Timestamp.h"
#include <signal.h> #include <signal.h>
#if defined(__sun) && defined(__SVR4) #if defined(__sun) && defined(__SVR4)
# if !defined(__EXTENSIONS__) # if !defined(__EXTENSIONS__)
# define __EXTENSIONS__ # define __EXTENSIONS__
# endif # endif
// must be limits.h for PTHREAD_STACK_MIN on Solaris #endif
# include <limits.h> #if POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_MAC_OS_X || POCO_OS == POCO_OS_QNX
#else # include <time.h>
# include <climits>
#endif #endif
// //
// Block SIGPIPE in main thread. // Block SIGPIPE in main thread.
// //
#if defined(POCO_OS_FAMILY_UNIX) #if defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_VXWORKS)
namespace namespace
{ {
class SignalBlocker class SignalBlocker
@ -131,7 +130,9 @@ void ThreadImpl::setOSPriorityImpl(int prio)
int ThreadImpl::getMinOSPriorityImpl() 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; return PRI_OTHER_MIN;
#else #else
return sched_get_priority_min(SCHED_OTHER); return sched_get_priority_min(SCHED_OTHER);
@ -141,7 +142,9 @@ int ThreadImpl::getMinOSPriorityImpl()
int ThreadImpl::getMaxOSPriorityImpl() 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; return PRI_OTHER_MAX;
#else #else
return sched_get_priority_max(SCHED_OTHER); 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) void ThreadImpl::sleepImpl(long milliseconds)
{ {
#if defined(__VMS) || defined(__digital__) #if defined(__VMS) || defined(__digital__)
@ -273,6 +282,28 @@ void ThreadImpl::sleepImpl(long milliseconds)
interval.tv_sec = milliseconds / 1000; interval.tv_sec = milliseconds / 1000;
interval.tv_nsec = (milliseconds % 1000)*1000000; interval.tv_nsec = (milliseconds % 1000)*1000000;
pthread_delay_np(&interval); 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 #else
Poco::Timespan remainingTime(1000*Poco::Timespan::TimeDiff(milliseconds)); Poco::Timespan remainingTime(1000*Poco::Timespan::TimeDiff(milliseconds));
int rc; int rc;

View File

@ -40,6 +40,52 @@
#include <process.h> #include <process.h>
#if defined(_DEBUG) && defined(POCO_WIN32_DEBUGGER_THREAD_NAMES)
namespace
{
/// See <http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx>
/// and <http://blogs.msdn.com/b/stevejs/archive/2005/12/19/505815.aspx> 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 { namespace Poco {
@ -49,6 +95,7 @@ ThreadImpl::CurrentThreadHolder ThreadImpl::_currentThreadHolder;
ThreadImpl::ThreadImpl(): ThreadImpl::ThreadImpl():
_pRunnableTarget(0), _pRunnableTarget(0),
_thread(0), _thread(0),
_threadId(0),
_prio(PRIO_NORMAL_IMPL), _prio(PRIO_NORMAL_IMPL),
_stackSize(POCO_THREAD_STACK_SIZE) _stackSize(POCO_THREAD_STACK_SIZE)
{ {
@ -107,11 +154,11 @@ void ThreadImpl::startImpl(Callable target, void* pData)
void ThreadImpl::createImpl(Entry ent, void* pData) void ThreadImpl::createImpl(Entry ent, void* pData)
{ {
#if defined(_DLL) #if defined(_DLL)
DWORD threadId; _thread = CreateThread(NULL, _stackSize, ent, pData, 0, &_threadId);
_thread = CreateThread(NULL, _stackSize, ent, pData, 0, &threadId);
#else #else
unsigned threadId; unsigned threadId;
_thread = (HANDLE) _beginthreadex(NULL, _stackSize, ent, this, 0, &threadId); _thread = (HANDLE) _beginthreadex(NULL, _stackSize, ent, this, 0, &threadId);
_threadId = static_cast<DWORD>(threadId);
#endif #endif
if (!_thread) if (!_thread)
throw SystemException("cannot create thread"); throw SystemException("cannot create thread");
@ -176,6 +223,12 @@ ThreadImpl* ThreadImpl::currentImpl()
} }
ThreadImpl::TIDImpl ThreadImpl::currentTidImpl()
{
return GetCurrentThreadId();
}
#if defined(_DLL) #if defined(_DLL)
DWORD WINAPI ThreadImpl::runnableEntry(LPVOID pThread) DWORD WINAPI ThreadImpl::runnableEntry(LPVOID pThread)
#else #else
@ -183,6 +236,9 @@ unsigned __stdcall ThreadImpl::runnableEntry(void* pThread)
#endif #endif
{ {
_currentThreadHolder.set(reinterpret_cast<ThreadImpl*>(pThread)); _currentThreadHolder.set(reinterpret_cast<ThreadImpl*>(pThread));
#if defined(_DEBUG) && defined(POCO_WIN32_DEBUGGER_THREAD_NAMES)
setThreadName(-1, reinterpret_cast<Thread*>(pThread)->getName().c_str());
#endif
try try
{ {
reinterpret_cast<ThreadImpl*>(pThread)->_pRunnableTarget->run(); reinterpret_cast<ThreadImpl*>(pThread)->_pRunnableTarget->run();
@ -210,6 +266,9 @@ unsigned __stdcall ThreadImpl::callableEntry(void* pThread)
#endif #endif
{ {
_currentThreadHolder.set(reinterpret_cast<ThreadImpl*>(pThread)); _currentThreadHolder.set(reinterpret_cast<ThreadImpl*>(pThread));
#if defined(_DEBUG) && defined(POCO_WIN32_DEBUGGER_THREAD_NAMES)
setThreadName(-1, reinterpret_cast<Thread*>(pThread)->getName().c_str());
#endif
try try
{ {
ThreadImpl* pTI = reinterpret_cast<ThreadImpl*>(pThread); ThreadImpl* pTI = reinterpret_cast<ThreadImpl*>(pThread);