trunk/branch integration: VxWorks & Wince

This commit is contained in:
Marian Krivos
2011-08-22 18:02:56 +00:00
parent f66cd72471
commit 68306c2960
5 changed files with 201 additions and 110 deletions

View File

@@ -45,7 +45,11 @@
#if defined(POCO_OS_FAMILY_WINDOWS) #if defined(POCO_OS_FAMILY_WINDOWS)
#if defined(_WIN32_WCE)
#include "Poco/Thread_WINCE.h"
#else
#include "Poco/Thread_WIN32.h" #include "Poco/Thread_WIN32.h"
#endif
#else #else
#include "Poco/Thread_POSIX.h" #include "Poco/Thread_POSIX.h"
#endif #endif
@@ -68,6 +72,8 @@ class Foundation_API Thread: private ThreadImpl
/// The name of a thread can be changed at any time. /// The name of a thread can be changed at any time.
{ {
public: public:
typedef ThreadImpl::TIDImpl TID;
using ThreadImpl::Callable; using ThreadImpl::Callable;
enum Priority enum Priority
@@ -92,6 +98,9 @@ public:
int id() const; int id() const;
/// Returns the unique thread ID of the thread. /// Returns the unique thread ID of the thread.
TID tid() const;
/// Returns the native thread ID of the thread.
std::string name() const; std::string name() const;
/// Returns the name of the thread. /// Returns the name of the thread.
@@ -118,6 +127,8 @@ public:
int getOSPriority() const; int getOSPriority() const;
/// Returns the thread's priority, expressed as an operating system /// Returns the thread's priority, expressed as an operating system
/// specific priority value. /// specific priority value.
///
/// May return 0 if the priority has not been explicitly set.
static int getMinOSPriority(); static int getMinOSPriority();
/// Returns the mininum operating system-specific priority value, /// Returns the mininum operating system-specific priority value,
@@ -172,6 +183,9 @@ public:
/// Returns the Thread object for the currently active thread. /// Returns the Thread object for the currently active thread.
/// If the current thread is the main thread, 0 is returned. /// If the current thread is the main thread, 0 is returned.
static TID currentTid();
/// Returns the native thread ID for the current thread.
protected: protected:
ThreadLocalStorage& tls(); ThreadLocalStorage& tls();
/// Returns a reference to the thread's local storage. /// Returns a reference to the thread's local storage.
@@ -202,6 +216,12 @@ private:
// //
// inlines // inlines
// //
inline Thread::TID Thread::tid() const
{
return tidImpl();
}
inline int Thread::id() const inline int Thread::id() const
{ {
return _id; return _id;
@@ -284,6 +304,12 @@ inline int Thread::getStackSize() const
} }
inline Thread::TID Thread::currentTid()
{
return currentTidImpl();
}
} // namespace Poco } // namespace Poco

View File

@@ -73,12 +73,19 @@ public:
int maxCapacity = 16, int maxCapacity = 16,
int idleTime = 60, int idleTime = 60,
int stackSize = POCO_THREAD_STACK_SIZE); int stackSize = POCO_THREAD_STACK_SIZE);
/// Creates a thread pool with minCapacity threads.
/// If required, up to maxCapacity threads are created
/// a NoThreadAvailableException exception is thrown.
/// If a thread is running idle for more than idleTime seconds,
/// and more than minCapacity threads are running, the thread
/// is killed. Threads are created with given stack size.
ThreadPool(const std::string& name, ThreadPool(const std::string& name,
int minCapacity = 2, int minCapacity = 2,
int maxCapacity = 16, int maxCapacity = 16,
int idleTime = 60, int idleTime = 60,
int stackSize = POCO_THREAD_STACK_SIZE); int stackSize = POCO_THREAD_STACK_SIZE);
/// Creates a thread pool with minCapacity threads. /// Creates a thread pool with the given name and minCapacity threads.
/// If required, up to maxCapacity threads are created /// If required, up to maxCapacity threads are created
/// a NoThreadAvailableException exception is thrown. /// a NoThreadAvailableException exception is thrown.
/// If a thread is running idle for more than idleTime seconds, /// If a thread is running idle for more than idleTime seconds,
@@ -135,13 +142,25 @@ public:
/// threads are available. /// threads are available.
void stopAll(); void stopAll();
/// Stops all running threads. /// Stops all running threads and waits for their completion.
///
/// Will also delete all thread objects. /// Will also delete all thread objects.
/// If used, this method should be the last action before /// If used, this method should be the last action before
/// the thread pool is deleted. /// the thread pool is deleted.
///
/// Note: If a thread fails to stop within 10 seconds
/// (due to a programming error, for example), the
/// underlying thread object will not be deleted and
/// this method will return anyway. This allows for a
/// more or less graceful shutdown in case of a misbehaving
/// thread.
void joinAll(); void joinAll();
/// Waits for all threads to complete. /// Waits for all threads to complete.
///
/// Note that this will not actually join() the underlying
/// thread, but rather wait for the thread's runnables
/// to finish.
void collect(); void collect();
/// Stops and removes no longer used threads from the /// Stops and removes no longer used threads from the
@@ -151,6 +170,11 @@ public:
/// as the thread pool is also implicitly managed in /// as the thread pool is also implicitly managed in
/// calls to start(), addCapacity() and joinAll(). /// calls to start(), addCapacity() and joinAll().
const std::string& name() const;
/// Returns the name of the thread pool,
/// or an empty string if no name has been
/// specified in the constructor.
static ThreadPool& defaultPool(); static ThreadPool& defaultPool();
/// Returns a reference to the default /// Returns a reference to the default
/// thread pool. /// thread pool.
@@ -194,6 +218,12 @@ inline int ThreadPool::getStackSize() const
} }
inline const std::string& ThreadPool::name() const
{
return _name;
}
} // namespace Poco } // namespace Poco

View File

@@ -1,104 +1,109 @@
// //
// ThreadTarget.h // ThreadTarget.h
// //
// $Id: //poco/svn/Foundation/include/Poco/ThreadTarget.h#2 $ // $Id: ThreadTarget.h 1327 2010-02-09 13:56:31Z obiltschnig $
// //
// Library: Foundation // Library: Foundation
// Package: Threading // Package: Threading
// Module: ThreadTarget // Module: ThreadTarget
// //
// Definition of the ThreadTarget class. // Definition of the ThreadTarget class.
// //
// 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
// obtaining a copy of the software and accompanying documentation covered by // obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute, // this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the // 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 // Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following: // do so, all subject to the following:
// //
// The copyright notices in the Software and this entire statement, including // The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer, // 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 // 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 // all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by // works are solely in the form of machine-executable object code generated by
// a source language processor. // a source language processor.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, // 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 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
// //
#ifndef Foundation_ThreadTarget_INCLUDED #ifndef Foundation_ThreadTarget_INCLUDED
#define Foundation_ThreadTarget_INCLUDED #define Foundation_ThreadTarget_INCLUDED
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include "Poco/Runnable.h" #include "Poco/Runnable.h"
namespace Poco { namespace Poco {
class Foundation_API ThreadTarget: public Runnable class Foundation_API ThreadTarget: public Runnable
/// This adapter simplifies using static member functions as well as /// This adapter simplifies using static member functions as well as
/// standalone functions as targets for threads. /// standalone functions as targets for threads.
/// Note that it is possible to pass those entities directly to Thread::start(). /// Note that it is possible to pass those entities directly to Thread::start().
/// This adapter is provided as a convenience for higher abstraction level /// This adapter is provided as a convenience for higher abstraction level
/// scenarios where Runnable abstract class is used. /// scenarios where Runnable abstract class is used.
/// ///
/// Usage: /// For using a non-static member function as a thread target, please
/// class MyObject /// see the RunnableAdapter class.
/// { ///
/// static void doSomething() {} /// Usage:
/// }; /// class MyObject
/// ThreadTarget ra(&MyObject::doSomething)); /// {
/// Thread thr; /// static void doSomething() {}
/// thr.start(ra); /// };
/// /// ThreadTarget ra(&MyObject::doSomething);
/// or: /// Thread thr;
/// /// thr.start(ra);
/// void doSomething() {} ///
/// /// or:
/// ThreadTarget ra(doSomething)); ///
/// Thread thr; /// void doSomething() {}
/// thr.start(ra); ///
/// ThreadTarget ra(doSomething);
{ /// Thread thr;
public: /// thr.start(ra);
typedef void (*Callback)(); {
public:
ThreadTarget(Callback method); typedef void (*Callback)();
ThreadTarget(const ThreadTarget& te); ThreadTarget(Callback method);
~ThreadTarget(); ThreadTarget(const ThreadTarget& te);
ThreadTarget& operator = (const ThreadTarget& te); ~ThreadTarget();
void run(); ThreadTarget& operator = (const ThreadTarget& te);
private: void run();
ThreadTarget();
private:
Callback _method; ThreadTarget();
};
Callback _method;
};
inline void ThreadTarget::run()
{
_method(); //
} // inlines
//
inline void ThreadTarget::run()
} // namespace Poco {
_method();
}
#endif // Foundation_ThreadTarget_INCLUDED
} // namespace Poco
#endif // Foundation_ThreadTarget_INCLUDED

View File

@@ -47,10 +47,15 @@
#include "Poco/RefCountedObject.h" #include "Poco/RefCountedObject.h"
#include "Poco/AutoPtr.h" #include "Poco/AutoPtr.h"
#include <pthread.h> #include <pthread.h>
// must be limits.h (not <climits>) for PTHREAD_STACK_MIN on Solaris
#include <limits.h>
#if !defined(POCO_NO_SYS_SELECT_H) #if !defined(POCO_NO_SYS_SELECT_H)
#include <sys/select.h> #include <sys/select.h>
#endif #endif
#include <errno.h> #include <errno.h>
#if defined(POCO_VXWORKS)
#include <cstring>
#endif
namespace Poco { namespace Poco {
@@ -59,6 +64,7 @@ namespace Poco {
class Foundation_API ThreadImpl class Foundation_API ThreadImpl
{ {
public: public:
typedef pthread_t TIDImpl;
typedef void (*Callable)(void*); typedef void (*Callable)(void*);
enum Priority enum Priority
@@ -82,7 +88,8 @@ public:
ThreadImpl(); ThreadImpl();
~ThreadImpl(); ~ThreadImpl();
TIDImpl tidImpl() const;
void setPriorityImpl(int prio); void setPriorityImpl(int prio);
int getPriorityImpl() const; int getPriorityImpl() const;
void setOSPriorityImpl(int prio); void setOSPriorityImpl(int prio);
@@ -100,6 +107,7 @@ public:
static void sleepImpl(long milliseconds); static void sleepImpl(long milliseconds);
static void yieldImpl(); static void yieldImpl();
static ThreadImpl* currentImpl(); static ThreadImpl* currentImpl();
static TIDImpl currentTidImpl();
protected: protected:
static void* runnableEntry(void* pThread); static void* runnableEntry(void* pThread);
@@ -140,9 +148,15 @@ private:
pCallbackTarget(0), pCallbackTarget(0),
thread(0), thread(0),
prio(PRIO_NORMAL_IMPL), prio(PRIO_NORMAL_IMPL),
osPrio(0),
done(false), done(false),
stackSize(POCO_THREAD_STACK_SIZE) stackSize(POCO_THREAD_STACK_SIZE)
{ {
#if defined(POCO_VXWORKS)
// This workaround is for VxWorks 5.x where
// pthread_init() won't properly initialize the thread.
std::memset(&thread, 0, sizeof(thread));
#endif
} }
Runnable* pRunnableTarget; Runnable* pRunnableTarget;
@@ -158,7 +172,7 @@ private:
static CurrentThreadHolder _currentThreadHolder; static CurrentThreadHolder _currentThreadHolder;
#if defined(POCO_OS_FAMILY_UNIX) #if defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_VXWORKS)
SignalHandler::JumpBufferVec _jumpBufferVec; SignalHandler::JumpBufferVec _jumpBufferVec;
friend class SignalHandler; friend class SignalHandler;
#endif #endif
@@ -199,6 +213,12 @@ inline int ThreadImpl::getStackSizeImpl() const
} }
inline ThreadImpl::TIDImpl ThreadImpl::tidImpl() const
{
return _pData->thread;
}
} // namespace Poco } // namespace Poco

View File

@@ -1,7 +1,7 @@
// //
// Thread_WIN32.h // Thread_WIN32.h
// //
// $Id: //poco/Main/Foundation/include/Poco/Thread_WIN32.h#8 $ // $Id: //poco/1.4/Foundation/include/Poco/Thread_WIN32.h#1 $
// //
// Library: Foundation // Library: Foundation
// Package: Threading // Package: Threading
@@ -51,6 +51,7 @@ namespace Poco {
class Foundation_API ThreadImpl class Foundation_API ThreadImpl
{ {
public: public:
typedef DWORD TIDImpl;
typedef void (*Callable)(void*); typedef void (*Callable)(void*);
#if defined(_DLL) #if defined(_DLL)
@@ -81,6 +82,7 @@ public:
ThreadImpl(); ThreadImpl();
~ThreadImpl(); ~ThreadImpl();
TIDImpl tidImpl() const;
void setPriorityImpl(int prio); void setPriorityImpl(int prio);
int getPriorityImpl() const; int getPriorityImpl() const;
void setOSPriorityImpl(int prio); void setOSPriorityImpl(int prio);
@@ -98,7 +100,8 @@ public:
static void sleepImpl(long milliseconds); static void sleepImpl(long milliseconds);
static void yieldImpl(); static void yieldImpl();
static ThreadImpl* currentImpl(); static ThreadImpl* currentImpl();
static TIDImpl currentTidImpl();
protected: protected:
#if defined(_DLL) #if defined(_DLL)
static DWORD WINAPI runnableEntry(LPVOID pThread); static DWORD WINAPI runnableEntry(LPVOID pThread);
@@ -144,6 +147,7 @@ private:
Runnable* _pRunnableTarget; Runnable* _pRunnableTarget;
CallbackData _callbackTarget; CallbackData _callbackTarget;
HANDLE _thread; HANDLE _thread;
DWORD _threadId;
int _prio; int _prio;
int _stackSize; int _stackSize;
@@ -202,6 +206,12 @@ inline int ThreadImpl::getStackSizeImpl() const
} }
inline ThreadImpl::TIDImpl ThreadImpl::tidImpl() const
{
return _threadId;
}
} // namespace Poco } // namespace Poco