mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 02:18:04 +01:00
SF 1939071 and 1928786
This commit is contained in:
@@ -91,6 +91,12 @@ void Thread::start(Runnable& target)
|
||||
}
|
||||
|
||||
|
||||
void Thread::start(Callback target, void* pData)
|
||||
{
|
||||
startImpl(target, pData);
|
||||
}
|
||||
|
||||
|
||||
void Thread::join()
|
||||
{
|
||||
joinImpl();
|
||||
|
||||
66
Foundation/src/ThreadTarget.cpp
Normal file
66
Foundation/src/ThreadTarget.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// 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
|
||||
@@ -39,7 +39,6 @@
|
||||
#include "Poco/ErrorHandler.h"
|
||||
#include <signal.h>
|
||||
|
||||
|
||||
//
|
||||
// Block SIGPIPE in main thread.
|
||||
//
|
||||
@@ -87,7 +86,7 @@ ThreadImpl::ThreadImpl():
|
||||
|
||||
ThreadImpl::~ThreadImpl()
|
||||
{
|
||||
if (_pData->pTarget)
|
||||
if (isRunningImpl())
|
||||
pthread_detach(_pData->thread);
|
||||
}
|
||||
|
||||
@@ -97,7 +96,7 @@ void ThreadImpl::setPriorityImpl(int prio)
|
||||
if (prio != _pData->prio)
|
||||
{
|
||||
_pData->prio = prio;
|
||||
if (_pData->pTarget)
|
||||
if (isRunningImpl())
|
||||
{
|
||||
struct sched_param par;
|
||||
par.sched_priority = mapPrio(_pData->prio);
|
||||
@@ -110,12 +109,52 @@ void ThreadImpl::setPriorityImpl(int prio)
|
||||
|
||||
void ThreadImpl::startImpl(Runnable& target)
|
||||
{
|
||||
if (_pData->pTarget) throw SystemException("thread already running");
|
||||
if (_pData->pRunnableTarget) throw SystemException("thread already running");
|
||||
|
||||
_pData->pTarget = ⌖
|
||||
if (pthread_create(&_pData->thread, NULL, entry, this))
|
||||
pthread_attr_t attributes;
|
||||
pthread_attr_init(&attributes);
|
||||
|
||||
if (_pData->stackSize != 0)
|
||||
pthread_attr_setstacksize(&attributes, _pData->stackSize);
|
||||
|
||||
_pData->pRunnableTarget = ⌖
|
||||
if (pthread_create(&_pData->thread, &attributes, entry, this))
|
||||
{
|
||||
_pData->pTarget = 0;
|
||||
_pData->pRunnableTarget = 0;
|
||||
throw SystemException("cannot start thread");
|
||||
}
|
||||
|
||||
if (_pData->prio != PRIO_NORMAL_IMPL)
|
||||
{
|
||||
struct sched_param par;
|
||||
par.sched_priority = mapPrio(_pData->prio);
|
||||
if (pthread_setschedparam(_pData->thread, SCHED_OTHER, &par))
|
||||
throw SystemException("cannot set thread priority");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ThreadImpl::startImpl(Callback target, void* pData)
|
||||
{
|
||||
if (_pData->pCallbackTarget && _pData->pCallbackTarget->callback)
|
||||
throw SystemException("thread already running");
|
||||
|
||||
pthread_attr_t attributes;
|
||||
pthread_attr_init(&attributes);
|
||||
|
||||
if (_pData->stackSize != 0)
|
||||
pthread_attr_setstacksize(&attributes, _pData->stackSize);
|
||||
|
||||
if (0 == _pData->pCallbackTarget.get())
|
||||
_pData->pCallbackTarget = new CallbackData;
|
||||
|
||||
_pData->pCallbackTarget->callback = target;
|
||||
_pData->pCallbackTarget->pData = pData;
|
||||
|
||||
if (pthread_create(&_pData->thread, &attributes, entry, this))
|
||||
{
|
||||
_pData->pCallbackTarget->callback = 0;
|
||||
_pData->pCallbackTarget->pData = 0;
|
||||
throw SystemException("cannot start thread");
|
||||
}
|
||||
|
||||
@@ -151,12 +190,6 @@ bool ThreadImpl::joinImpl(long milliseconds)
|
||||
}
|
||||
|
||||
|
||||
bool ThreadImpl::isRunningImpl() const
|
||||
{
|
||||
return _pData->pTarget != 0;
|
||||
}
|
||||
|
||||
|
||||
ThreadImpl* ThreadImpl::currentImpl()
|
||||
{
|
||||
if (_haveCurrentKey)
|
||||
@@ -183,7 +216,10 @@ void* ThreadImpl::entry(void* pThread)
|
||||
AutoPtr<ThreadData> pData = pThreadImpl->_pData;
|
||||
try
|
||||
{
|
||||
pData->pTarget->run();
|
||||
if (pData->pRunnableTarget)
|
||||
pData->pRunnableTarget->run();
|
||||
else
|
||||
pData->pCallbackTarget->callback(pData->pCallbackTarget->pData);
|
||||
}
|
||||
catch (Exception& exc)
|
||||
{
|
||||
@@ -197,7 +233,14 @@ void* ThreadImpl::entry(void* pThread)
|
||||
{
|
||||
ErrorHandler::handle();
|
||||
}
|
||||
pData->pTarget = 0;
|
||||
if (pData->pRunnableTarget)
|
||||
pData->pRunnableTarget = 0;
|
||||
else
|
||||
{
|
||||
pData->pCallbackTarget->callback = 0;
|
||||
pData->pCallbackTarget->pData = 0;
|
||||
}
|
||||
|
||||
pData->done.set();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -46,7 +46,11 @@ namespace Poco {
|
||||
DWORD ThreadImpl::_currentKey = TLS_OUT_OF_INDEXES;
|
||||
|
||||
|
||||
ThreadImpl::ThreadImpl(): _pTarget(0), _thread(0), _prio(PRIO_NORMAL_IMPL)
|
||||
ThreadImpl::ThreadImpl():
|
||||
_pRunnableTarget(0),
|
||||
_thread(0),
|
||||
_prio(PRIO_NORMAL_IMPL),
|
||||
_stackSize(0)
|
||||
{
|
||||
if (_currentKey == TLS_OUT_OF_INDEXES)
|
||||
{
|
||||
@@ -79,15 +83,35 @@ void ThreadImpl::setPriorityImpl(int prio)
|
||||
|
||||
void ThreadImpl::startImpl(Runnable& target)
|
||||
{
|
||||
if (_thread) throw SystemException("thread already running");
|
||||
if (isRunningImpl())
|
||||
throw SystemException("thread already running");
|
||||
|
||||
_pTarget = ⌖
|
||||
_pRunnableTarget = ⌖
|
||||
|
||||
createImpl(runnableEntry, this);
|
||||
}
|
||||
|
||||
|
||||
void ThreadImpl::startImpl(Callback target, void* pData)
|
||||
{
|
||||
if (isRunningImpl())
|
||||
throw SystemException("thread already running");
|
||||
|
||||
_callbackTarget.callback = target;
|
||||
_callbackTarget.pData = pData;
|
||||
|
||||
createImpl(functionEntry, this);
|
||||
}
|
||||
|
||||
|
||||
void ThreadImpl::createImpl(Entry ent, void* pData)
|
||||
{
|
||||
#if defined(_DLL)
|
||||
DWORD threadId;
|
||||
_thread = CreateThread(NULL, 0, entry, this, 0, &threadId);
|
||||
_thread = CreateThread(NULL, _stackSize, ent, pData, 0, &threadId);
|
||||
#else
|
||||
unsigned threadId;
|
||||
_thread = (HANDLE) _beginthreadex(NULL, 0, entry, this, 0, &threadId);
|
||||
_thread = (HANDLE) _beginthreadex(NULL, _stackSize, runnableEntry, this, 0, &threadId);
|
||||
#endif
|
||||
if (!_thread)
|
||||
throw SystemException("cannot create thread");
|
||||
@@ -103,6 +127,7 @@ void ThreadImpl::joinImpl()
|
||||
switch (WaitForSingleObject(_thread, INFINITE))
|
||||
{
|
||||
case WAIT_OBJECT_0:
|
||||
threadCleanup();
|
||||
return;
|
||||
default:
|
||||
throw SystemException("cannot join thread");
|
||||
@@ -119,6 +144,7 @@ bool ThreadImpl::joinImpl(long milliseconds)
|
||||
case WAIT_TIMEOUT:
|
||||
return false;
|
||||
case WAIT_OBJECT_0:
|
||||
threadCleanup();
|
||||
return true;
|
||||
default:
|
||||
throw SystemException("cannot join thread");
|
||||
@@ -137,6 +163,13 @@ bool ThreadImpl::isRunningImpl() const
|
||||
}
|
||||
|
||||
|
||||
void ThreadImpl::threadCleanup()
|
||||
{
|
||||
if (!_thread) return;
|
||||
if (CloseHandle(_thread)) _thread = 0;
|
||||
}
|
||||
|
||||
|
||||
ThreadImpl* ThreadImpl::currentImpl()
|
||||
{
|
||||
if (_currentKey == TLS_OUT_OF_INDEXES)
|
||||
@@ -147,15 +180,43 @@ ThreadImpl* ThreadImpl::currentImpl()
|
||||
|
||||
|
||||
#if defined(_DLL)
|
||||
DWORD WINAPI ThreadImpl::entry(LPVOID pThread)
|
||||
DWORD WINAPI ThreadImpl::runnableEntry(LPVOID pThread)
|
||||
#else
|
||||
unsigned __stdcall ThreadImpl::entry(void* pThread)
|
||||
unsigned __stdcall ThreadImpl::runnableEntry(void* pThread)
|
||||
#endif
|
||||
{
|
||||
TlsSetValue(_currentKey, pThread);
|
||||
try
|
||||
{
|
||||
reinterpret_cast<ThreadImpl*>(pThread)->_pTarget->run();
|
||||
reinterpret_cast<ThreadImpl*>(pThread)->_pRunnableTarget->run();
|
||||
}
|
||||
catch (Exception& exc)
|
||||
{
|
||||
ErrorHandler::handle(exc);
|
||||
}
|
||||
catch (std::exception& exc)
|
||||
{
|
||||
ErrorHandler::handle(exc);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
ErrorHandler::handle();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#if defined(_DLL)
|
||||
DWORD WINAPI ThreadImpl::functionEntry(LPVOID pThread)
|
||||
#else
|
||||
unsigned __stdcall ThreadImpl::functionEntry(void* pThread)
|
||||
#endif
|
||||
{
|
||||
TlsSetValue(_currentKey, pThread);
|
||||
try
|
||||
{
|
||||
ThreadImpl* pTI = reinterpret_cast<ThreadImpl*>(pThread);
|
||||
pTI->_callbackTarget.callback(pTI->_callbackTarget.pData);
|
||||
}
|
||||
catch (Exception& exc)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user