- 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

@@ -50,7 +50,7 @@ namespace Poco {
class PooledThread: public Runnable
{
public:
PooledThread(const std::string& name);
PooledThread(const std::string& name, int stackSize = POCO_THREAD_STACK_SIZE);
~PooledThread();
void start();
@@ -66,17 +66,17 @@ public:
private:
volatile bool _idle;
volatile std::time_t _idleTime;
Runnable* _pTarget;
std::string _name;
Thread _thread;
Event _targetReady;
Event _targetCompleted;
Event _started;
FastMutex _mutex;
Runnable* _pTarget;
std::string _name;
Thread _thread;
Event _targetReady;
Event _targetCompleted;
Event _started;
FastMutex _mutex;
};
PooledThread::PooledThread(const std::string& name):
PooledThread::PooledThread(const std::string& name, int stackSize):
_idle(true),
_idleTime(0),
_pTarget(0),
@@ -84,6 +84,8 @@ PooledThread::PooledThread(const std::string& name):
_thread(name),
_targetCompleted(false)
{
poco_assert_dbg (stackSize >= 0);
_thread.setStackSize(stackSize);
_idleTime = time(NULL);
}
@@ -255,7 +257,8 @@ ThreadPool::ThreadPool(const std::string& name, int minCapacity, int maxCapacity
_maxCapacity(maxCapacity),
_idleTime(idleTime),
_serial(0),
_age(0)
_age(0),
_stackSize(0)
{
poco_assert (minCapacity >= 1 && maxCapacity >= minCapacity && idleTime > 0);
@@ -452,7 +455,7 @@ PooledThread* ThreadPool::createThread()
{
std::ostringstream name;
name << _name << "[#" << ++_serial << "]";
return new PooledThread(name.str());
return new PooledThread(name.str(), _stackSize);
}
@@ -474,6 +477,8 @@ public:
if (!_pPool)
{
_pPool = new ThreadPool("default");
if (POCO_THREAD_STACK_SIZE > 0)
_pPool->setStackSize(POCO_THREAD_STACK_SIZE);
}
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)
{
if (_pData->pRunnableTarget) throw SystemException("thread already running");
if (_pData->pRunnableTarget)
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 != pthread_attr_setstacksize(&attributes, _pData->stackSize))
throw SystemException("cannot set thread stack size");
}
_pData->pRunnableTarget = &target;
if (pthread_create(&_pData->thread, &attributes, runnableEntry, this))
@@ -180,7 +193,10 @@ void ThreadImpl::startImpl(Callback target, void* pData)
pthread_attr_init(&attributes);
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())
_pData->pCallbackTarget = new CallbackData;

View File

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