mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-28 19:51:58 +01:00
SF 1939071 and 1928786
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Threading
|
||||
// Module: Thread
|
||||
// Module: RunnableAdapter
|
||||
//
|
||||
// Definition of the RunnableAdapter template class.
|
||||
//
|
||||
@@ -59,11 +59,15 @@ class RunnableAdapter: public Runnable
|
||||
public:
|
||||
typedef void (C::*Callback)();
|
||||
|
||||
RunnableAdapter(C& object, Callback method): _pObject(&object), _method(method)
|
||||
RunnableAdapter(C& object, Callback method):
|
||||
_pObject(&object),
|
||||
_method(method)
|
||||
{
|
||||
}
|
||||
|
||||
RunnableAdapter(const RunnableAdapter& ra): _pObject(ra._pObject), _method(ra._method)
|
||||
RunnableAdapter(const RunnableAdapter& ra):
|
||||
_pObject(ra._pObject),
|
||||
_method(ra._method)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -68,6 +68,8 @@ class Foundation_API Thread: private ThreadImpl
|
||||
/// The name of a thread can be changed at any time.
|
||||
{
|
||||
public:
|
||||
using ThreadImpl::Callback;
|
||||
|
||||
enum Priority
|
||||
/// Thread priorities.
|
||||
{
|
||||
@@ -108,9 +110,22 @@ public:
|
||||
Priority getPriority() const;
|
||||
/// Returns the thread's priority.
|
||||
|
||||
void setStackSize(std::size_t size);
|
||||
/// Sets the thread's stack size in bytes.
|
||||
/// Setting the stack size to 0 will use the default stack size.
|
||||
/// Typically, the real stack size is rounded up to the nearest
|
||||
/// page size multiple.
|
||||
|
||||
std::size_t getStackSize() const;
|
||||
/// Returns the thread's stack size in bytes.
|
||||
/// If the default stack size is used, 0 is returned.
|
||||
|
||||
void start(Runnable& target);
|
||||
/// Starts the thread with the given target.
|
||||
|
||||
void start(Callback target, void* pData = 0);
|
||||
/// Starts the thread with the given target and parameter.
|
||||
|
||||
void join();
|
||||
/// Waits until the thread completes execution.
|
||||
/// If multiple threads try to join the same
|
||||
@@ -216,6 +231,18 @@ inline Thread* Thread::current()
|
||||
}
|
||||
|
||||
|
||||
inline void Thread::setStackSize(std::size_t size)
|
||||
{
|
||||
setStackSizeImpl(size);
|
||||
}
|
||||
|
||||
|
||||
inline std::size_t Thread::getStackSize() const
|
||||
{
|
||||
return getStackSizeImpl();
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
|
||||
104
Foundation/include/Poco/ThreadTarget.h
Normal file
104
Foundation/include/Poco/ThreadTarget.h
Normal file
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// ThreadTarget.h
|
||||
//
|
||||
// $Id: //poco/svn/Foundation/include/Poco/ThreadTarget.h#2 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Threading
|
||||
// Module: ThreadTarget
|
||||
//
|
||||
// Definition of the ThreadTarget class.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
|
||||
#ifndef Foundation_ThreadTarget_INCLUDED
|
||||
#define Foundation_ThreadTarget_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/Runnable.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
class Foundation_API ThreadTarget: public Runnable
|
||||
/// This adapter simplifies using static member functions as well as
|
||||
/// standalone functions as targets for threads.
|
||||
/// Note that it is possible to pass those entities directly to Thread::start().
|
||||
/// This adapter is provided as a convenience for higher abstraction level
|
||||
/// scenarios where Runnable abstract class is used.
|
||||
///
|
||||
/// Usage:
|
||||
/// class MyObject
|
||||
/// {
|
||||
/// static void doSomething() {}
|
||||
/// };
|
||||
/// ThreadTarget ra(&MyObject::doSomething));
|
||||
/// Thread thr;
|
||||
/// thr.start(ra);
|
||||
///
|
||||
/// or:
|
||||
///
|
||||
/// void doSomething() {}
|
||||
///
|
||||
/// ThreadTarget ra(doSomething));
|
||||
/// Thread thr;
|
||||
/// thr.start(ra);
|
||||
|
||||
{
|
||||
public:
|
||||
typedef void (*Callback)();
|
||||
|
||||
ThreadTarget(Callback method);
|
||||
|
||||
ThreadTarget(const ThreadTarget& te);
|
||||
|
||||
~ThreadTarget();
|
||||
|
||||
ThreadTarget& operator = (const ThreadTarget& te);
|
||||
|
||||
void run();
|
||||
|
||||
private:
|
||||
ThreadTarget();
|
||||
|
||||
Callback _method;
|
||||
};
|
||||
|
||||
|
||||
inline void ThreadTarget::run()
|
||||
{
|
||||
_method();
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
#endif // Foundation_ThreadTarget_INCLUDED
|
||||
@@ -59,6 +59,8 @@ namespace Poco {
|
||||
class Foundation_API ThreadImpl
|
||||
{
|
||||
public:
|
||||
typedef void (*Callback)(void*);
|
||||
|
||||
enum Priority
|
||||
{
|
||||
PRIO_LOWEST_IMPL,
|
||||
@@ -68,13 +70,25 @@ public:
|
||||
PRIO_HIGHEST_IMPL
|
||||
};
|
||||
|
||||
struct CallbackData: public RefCountedObject
|
||||
{
|
||||
CallbackData(): callback(0), pData(0)
|
||||
{
|
||||
}
|
||||
|
||||
Callback callback;
|
||||
void* pData;
|
||||
};
|
||||
|
||||
ThreadImpl();
|
||||
~ThreadImpl();
|
||||
|
||||
Runnable& targetImpl() const;
|
||||
void setPriorityImpl(int prio);
|
||||
int getPriorityImpl() const;
|
||||
void setStackSizeImpl(std::size_t size);
|
||||
std::size_t getStackSizeImpl() const;
|
||||
void startImpl(Runnable& target);
|
||||
void startImpl(Callback target, void* pData = 0);
|
||||
|
||||
void joinImpl();
|
||||
bool joinImpl(long milliseconds);
|
||||
@@ -91,19 +105,23 @@ private:
|
||||
struct ThreadData: public RefCountedObject
|
||||
{
|
||||
ThreadData():
|
||||
pTarget(0),
|
||||
pRunnableTarget(0),
|
||||
pCallbackTarget(0),
|
||||
thread(0),
|
||||
prio(PRIO_NORMAL_IMPL),
|
||||
done(false)
|
||||
done(false),
|
||||
stackSize(0)
|
||||
{
|
||||
}
|
||||
|
||||
Runnable* pTarget;
|
||||
pthread_t thread;
|
||||
int prio;
|
||||
Event done;
|
||||
Runnable* pRunnableTarget;
|
||||
AutoPtr<CallbackData> pCallbackTarget;
|
||||
pthread_t thread;
|
||||
int prio;
|
||||
Event done;
|
||||
std::size_t stackSize;
|
||||
};
|
||||
|
||||
|
||||
AutoPtr<ThreadData> _pData;
|
||||
|
||||
static pthread_key_t _currentKey;
|
||||
@@ -142,12 +160,31 @@ inline void ThreadImpl::sleepImpl(long milliseconds)
|
||||
}
|
||||
|
||||
|
||||
inline bool ThreadImpl::isRunningImpl() const
|
||||
{
|
||||
return _pData->pRunnableTarget != 0 ||
|
||||
(_pData->pCallbackTarget.get() != 0 && _pData->pCallbackTarget->callback != 0);
|
||||
}
|
||||
|
||||
|
||||
inline void ThreadImpl::yieldImpl()
|
||||
{
|
||||
sched_yield();
|
||||
}
|
||||
|
||||
|
||||
inline void ThreadImpl::setStackSizeImpl(std::size_t size)
|
||||
{
|
||||
_pData->stackSize = size;
|
||||
}
|
||||
|
||||
|
||||
inline std::size_t ThreadImpl::getStackSizeImpl() const
|
||||
{
|
||||
return _pData->stackSize;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
|
||||
@@ -51,6 +51,24 @@ namespace Poco {
|
||||
class Foundation_API ThreadImpl
|
||||
{
|
||||
public:
|
||||
typedef void (*Callback)(void*);
|
||||
|
||||
#if defined(_DLL)
|
||||
typedef DWORD (WINAPI *Entry)(LPVOID);
|
||||
#else
|
||||
typedef unsigned (__stdcall *Entry)(void*);
|
||||
#endif
|
||||
|
||||
struct CallbackData
|
||||
{
|
||||
CallbackData(): callback(0), pData(0)
|
||||
{
|
||||
}
|
||||
|
||||
Callback callback;
|
||||
void* pData;
|
||||
};
|
||||
|
||||
enum Priority
|
||||
{
|
||||
PRIO_LOWEST_IMPL = THREAD_PRIORITY_LOWEST,
|
||||
@@ -65,7 +83,10 @@ public:
|
||||
|
||||
void setPriorityImpl(int prio);
|
||||
int getPriorityImpl() const;
|
||||
void setStackSizeImpl(std::size_t size);
|
||||
std::size_t getStackSizeImpl() const;
|
||||
void startImpl(Runnable& target);
|
||||
void startImpl(Callback target, void* pData = 0);
|
||||
|
||||
void joinImpl();
|
||||
bool joinImpl(long milliseconds);
|
||||
@@ -76,15 +97,26 @@ public:
|
||||
|
||||
protected:
|
||||
#if defined(_DLL)
|
||||
static DWORD WINAPI entry(LPVOID pThread);
|
||||
static DWORD WINAPI runnableEntry(LPVOID pThread);
|
||||
#else
|
||||
static unsigned __stdcall entry(void* pThread);
|
||||
static unsigned __stdcall runnableEntry(void* pThread);
|
||||
#endif
|
||||
|
||||
#if defined(_DLL)
|
||||
static DWORD WINAPI functionEntry(LPVOID pThread);
|
||||
#else
|
||||
static unsigned __stdcall functionEntry(void* pThread);
|
||||
#endif
|
||||
|
||||
void createImpl(Entry ent, void* pData);
|
||||
void threadCleanup();
|
||||
|
||||
private:
|
||||
Runnable* _pTarget;
|
||||
HANDLE _thread;
|
||||
int _prio;
|
||||
Runnable* _pRunnableTarget;
|
||||
CallbackData _callbackTarget;
|
||||
HANDLE _thread;
|
||||
int _prio;
|
||||
std::size_t _stackSize;
|
||||
|
||||
static DWORD _currentKey;
|
||||
};
|
||||
@@ -111,6 +143,18 @@ inline void ThreadImpl::yieldImpl()
|
||||
}
|
||||
|
||||
|
||||
inline void ThreadImpl::setStackSizeImpl(std::size_t size)
|
||||
{
|
||||
_stackSize = size;
|
||||
}
|
||||
|
||||
|
||||
inline std::size_t ThreadImpl::getStackSizeImpl() const
|
||||
{
|
||||
return _stackSize;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user