- POCO_THREAD_STACK_SIZE macro

- few Thread modifications
- ThreadPool configurable stack size
This commit is contained in:
Aleksandar Fabijanic 2008-04-20 23:28:41 +00:00
parent 537ec8aca3
commit 9fd70bade2
11 changed files with 90 additions and 40 deletions

View File

@ -60,4 +60,9 @@
// #define POCO_NO_SHAREDMEMORY // #define POCO_NO_SHAREDMEMORY
// Define to desired default thread stack size
// Zero means OS default
#define POCO_THREAD_STACK_SIZE 0
#endif // Foundation_Config_INCLUDED #endif // Foundation_Config_INCLUDED

View File

@ -127,13 +127,13 @@ public:
/// Returns the maximum operating system-specific priority value, /// Returns the maximum operating system-specific priority value,
/// which can be passed to setOSPriority(). /// which can be passed to setOSPriority().
void setStackSize(std::size_t size); void setStackSize(int size);
/// Sets the thread's stack size in bytes. /// Sets the thread's stack size in bytes.
/// Setting the stack size to 0 will use the default stack size. /// Setting the stack size to 0 will use the default stack size.
/// Typically, the real stack size is rounded up to the nearest /// Typically, the real stack size is rounded up to the nearest
/// page size multiple. /// page size multiple.
std::size_t getStackSize() const; int getStackSize() const;
/// Returns the thread's stack size in bytes. /// Returns the thread's stack size in bytes.
/// If the default stack size is used, 0 is returned. /// If the default stack size is used, 0 is returned.
@ -272,13 +272,13 @@ inline int Thread::getMaxOSPriority()
} }
inline void Thread::setStackSize(std::size_t size) inline void Thread::setStackSize(int size)
{ {
setStackSizeImpl(size); setStackSizeImpl(size);
} }
inline std::size_t Thread::getStackSize() const inline int Thread::getStackSize() const
{ {
return getStackSizeImpl(); return getStackSizeImpl();
} }

View File

@ -89,6 +89,13 @@ public:
int capacity() const; int capacity() const;
/// Returns the maximum capacity of threads. /// Returns the maximum capacity of threads.
void setStackSize(int stackSize);
/// Sets the stack size for threads.
/// New stack size applies only for newly created threads.
int getStackSize() const;
/// Returns the stack size used to create new threads.
int used() const; int used() const;
/// Returns the number of currently used threads. /// Returns the number of currently used threads.
@ -159,11 +166,26 @@ private:
int _idleTime; int _idleTime;
int _serial; int _serial;
int _age; int _age;
int _stackSize;
ThreadVec _threads; ThreadVec _threads;
mutable FastMutex _mutex; mutable FastMutex _mutex;
}; };
// inlines
inline void ThreadPool::setStackSize(int stackSize)
{
_stackSize = stackSize;
}
inline int ThreadPool::getStackSize() const
{
return _stackSize;
}
} // namespace Poco } // namespace Poco

View File

@ -89,8 +89,8 @@ public:
int getOSPriorityImpl() const; int getOSPriorityImpl() const;
static int getMinOSPriorityImpl(); static int getMinOSPriorityImpl();
static int getMaxOSPriorityImpl(); static int getMaxOSPriorityImpl();
void setStackSizeImpl(std::size_t size); void setStackSizeImpl(int size);
std::size_t getStackSizeImpl() const; int getStackSizeImpl() const;
void startImpl(Runnable& target); void startImpl(Runnable& target);
void startImpl(Callback target, void* pData = 0); void startImpl(Callback target, void* pData = 0);
@ -116,7 +116,7 @@ private:
thread(0), thread(0),
prio(PRIO_NORMAL_IMPL), prio(PRIO_NORMAL_IMPL),
done(false), done(false),
stackSize(0) stackSize(POCO_THREAD_STACK_SIZE)
{ {
} }
@ -186,13 +186,7 @@ inline void ThreadImpl::yieldImpl()
} }
inline void ThreadImpl::setStackSizeImpl(std::size_t size) inline int ThreadImpl::getStackSizeImpl() const
{
_pData->stackSize = size;
}
inline std::size_t ThreadImpl::getStackSizeImpl() const
{ {
return _pData->stackSize; return _pData->stackSize;
} }

View File

@ -87,8 +87,8 @@ public:
int getOSPriorityImpl() const; int getOSPriorityImpl() const;
static int getMinOSPriorityImpl(); static int getMinOSPriorityImpl();
static int getMaxOSPriorityImpl(); static int getMaxOSPriorityImpl();
void setStackSizeImpl(std::size_t size); void setStackSizeImpl(int size);
std::size_t getStackSizeImpl() const; int getStackSizeImpl() const;
void startImpl(Runnable& target); void startImpl(Runnable& target);
void startImpl(Callback target, void* pData = 0); void startImpl(Callback target, void* pData = 0);
@ -120,7 +120,7 @@ private:
CallbackData _callbackTarget; CallbackData _callbackTarget;
HANDLE _thread; HANDLE _thread;
int _prio; int _prio;
std::size_t _stackSize; int _stackSize;
static DWORD _currentKey; static DWORD _currentKey;
}; };
@ -165,13 +165,13 @@ inline void ThreadImpl::yieldImpl()
} }
inline void ThreadImpl::setStackSizeImpl(std::size_t size) inline void ThreadImpl::setStackSizeImpl(int size)
{ {
_stackSize = size; _stackSize = size;
} }
inline std::size_t ThreadImpl::getStackSizeImpl() const inline int ThreadImpl::getStackSizeImpl() const
{ {
return _stackSize; return _stackSize;
} }

View File

@ -50,7 +50,7 @@ namespace Poco {
class PooledThread: public Runnable class PooledThread: public Runnable
{ {
public: public:
PooledThread(const std::string& name); PooledThread(const std::string& name, int stackSize = POCO_THREAD_STACK_SIZE);
~PooledThread(); ~PooledThread();
void start(); void start();
@ -76,7 +76,7 @@ private:
}; };
PooledThread::PooledThread(const std::string& name): PooledThread::PooledThread(const std::string& name, int stackSize):
_idle(true), _idle(true),
_idleTime(0), _idleTime(0),
_pTarget(0), _pTarget(0),
@ -84,6 +84,8 @@ PooledThread::PooledThread(const std::string& name):
_thread(name), _thread(name),
_targetCompleted(false) _targetCompleted(false)
{ {
poco_assert_dbg (stackSize >= 0);
_thread.setStackSize(stackSize);
_idleTime = time(NULL); _idleTime = time(NULL);
} }
@ -255,7 +257,8 @@ ThreadPool::ThreadPool(const std::string& name, int minCapacity, int maxCapacity
_maxCapacity(maxCapacity), _maxCapacity(maxCapacity),
_idleTime(idleTime), _idleTime(idleTime),
_serial(0), _serial(0),
_age(0) _age(0),
_stackSize(0)
{ {
poco_assert (minCapacity >= 1 && maxCapacity >= minCapacity && idleTime > 0); poco_assert (minCapacity >= 1 && maxCapacity >= minCapacity && idleTime > 0);
@ -452,7 +455,7 @@ PooledThread* ThreadPool::createThread()
{ {
std::ostringstream name; std::ostringstream name;
name << _name << "[#" << ++_serial << "]"; name << _name << "[#" << ++_serial << "]";
return new PooledThread(name.str()); return new PooledThread(name.str(), _stackSize);
} }
@ -474,6 +477,8 @@ public:
if (!_pPool) if (!_pPool)
{ {
_pPool = new ThreadPool("default"); _pPool = new ThreadPool("default");
if (POCO_THREAD_STACK_SIZE > 0)
_pPool->setStackSize(POCO_THREAD_STACK_SIZE);
} }
return _pPool; return _pPool;
} }

View File

@ -144,15 +144,28 @@ int ThreadImpl::getMaxOSPriorityImpl()
} }
void ThreadImpl::setStackSizeImpl(int size)
{
if (size !=0 && size < PTHREAD_STACK_MIN)
size = PTHREAD_STACK_MIN;
_pData->stackSize = size;
}
void ThreadImpl::startImpl(Runnable& target) void ThreadImpl::startImpl(Runnable& target)
{ {
if (_pData->pRunnableTarget) throw SystemException("thread already running"); if (_pData->pRunnableTarget)
throw SystemException("thread already running");
pthread_attr_t attributes; pthread_attr_t attributes;
pthread_attr_init(&attributes); pthread_attr_init(&attributes);
if (_pData->stackSize != 0) if (_pData->stackSize != 0)
pthread_attr_setstacksize(&attributes, _pData->stackSize); {
if (0 != pthread_attr_setstacksize(&attributes, _pData->stackSize))
throw SystemException("cannot set thread stack size");
}
_pData->pRunnableTarget = &target; _pData->pRunnableTarget = &target;
if (pthread_create(&_pData->thread, &attributes, runnableEntry, this)) if (pthread_create(&_pData->thread, &attributes, runnableEntry, this))
@ -180,7 +193,10 @@ void ThreadImpl::startImpl(Callback target, void* pData)
pthread_attr_init(&attributes); pthread_attr_init(&attributes);
if (_pData->stackSize != 0) if (_pData->stackSize != 0)
pthread_attr_setstacksize(&attributes, _pData->stackSize); {
if (0 != pthread_attr_setstacksize(&attributes, _pData->stackSize))
throw SystemException("can not set thread stack size");
}
if (0 == _pData->pCallbackTarget.get()) if (0 == _pData->pCallbackTarget.get())
_pData->pCallbackTarget = new CallbackData; _pData->pCallbackTarget = new CallbackData;

View File

@ -50,7 +50,7 @@ ThreadImpl::ThreadImpl():
_pRunnableTarget(0), _pRunnableTarget(0),
_thread(0), _thread(0),
_prio(PRIO_NORMAL_IMPL), _prio(PRIO_NORMAL_IMPL),
_stackSize(0) _stackSize(POCO_THREAD_STACK_SIZE)
{ {
if (_currentKey == TLS_OUT_OF_INDEXES) if (_currentKey == TLS_OUT_OF_INDEXES)
{ {

View File

@ -57,6 +57,7 @@ ThreadPoolTest::~ThreadPoolTest()
void ThreadPoolTest::testThreadPool() void ThreadPoolTest::testThreadPool()
{ {
ThreadPool pool(2, 3, 3); ThreadPool pool(2, 3, 3);
pool.setStackSize(1);
assert (pool.allocated() == 2); assert (pool.allocated() == 2);
assert (pool.used() == 0); assert (pool.used() == 0);

View File

@ -263,17 +263,24 @@ void ThreadTest::testThreadFunction()
void ThreadTest::testThreadStackSize() void ThreadTest::testThreadStackSize()
{ {
int stackSize = 50000000;
Thread thread; Thread thread;
assert (0 == thread.getStackSize()); assert (0 == thread.getStackSize());
thread.setStackSize(50000000); thread.setStackSize(stackSize);
assert (50000000 == thread.getStackSize()); assert (stackSize == thread.getStackSize());
int tmp = MyRunnable::_staticVar; int tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp); thread.start(freeFunc, &tmp);
thread.join(); thread.join();
assert (tmp * 2 == MyRunnable::_staticVar); assert (tmp * 2 == MyRunnable::_staticVar);
thread.setStackSize(1); stackSize = 1;
assert (1 == thread.getStackSize()); thread.setStackSize(stackSize);
#ifdef POCO_OS_FAMILY_UNIX
assert (PTHREAD_STACK_MIN == thread.getStackSize());
#else
assert (stackSize == thread.getStackSize());
#endif
tmp = MyRunnable::_staticVar; tmp = MyRunnable::_staticVar;
thread.start(freeFunc, &tmp); thread.start(freeFunc, &tmp);
thread.join(); thread.join();