📄 thread_posix.cpp
字号:
//
// Thread_POSIX.cpp
//
// $Id: //poco/Main/Foundation/src/Thread_POSIX.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "Foundation/Thread_POSIX.h"
#include "Foundation/Exception.h"
#include <signal.h>
Foundation_BEGIN
pthread_key_t ThreadImp::_currentKey;
bool ThreadImp::_haveCurrentKey = false;
ThreadImp::ThreadImp():
_pTarget(0),
_thread(0),
_prio(PRIO_NORMAL_IMP)
{
if (!_haveCurrentKey)
{
if (pthread_key_create(&_currentKey, NULL))
throw SystemException("cannot allocate thread context key");
_haveCurrentKey = true;
}
}
ThreadImp::~ThreadImp()
{
if (_pTarget)
pthread_detach(_thread);
}
void ThreadImp::setPriorityImp(int prio)
{
if (prio != _prio)
{
_prio = prio;
if (_pTarget)
{
struct sched_param par;
par.sched_priority = mapPrio(_prio);
if (pthread_setschedparam(_thread, SCHED_OTHER, &par))
throw SystemException("cannot set thread priority");
}
}
}
void ThreadImp::startImp(Runnable& target)
{
if (_pTarget) throw SystemException("thread already running");
_pTarget = ⌖
if (pthread_create(&_thread, NULL, entry, this))
{
_pTarget = 0;
throw SystemException("cannot start thread");
}
if (_prio != PRIO_NORMAL_IMP)
{
struct sched_param par;
par.sched_priority = mapPrio(_prio);
if (pthread_setschedparam(_thread, SCHED_OTHER, &par))
throw SystemException("cannot set thread priority");
}
}
void ThreadImp::joinImp()
{
if (!_pTarget) return;
void* result;
if (pthread_join(_thread, &result))
throw SystemException("cannot join thread");
_pTarget = 0;
}
bool ThreadImp::isRunningImp() const
{
return _pTarget != 0;
}
ThreadImp* ThreadImp::currentImp()
{
if (_haveCurrentKey)
return (ThreadImp*) pthread_getspecific(_currentKey);
else
return 0;
}
void* ThreadImp::entry(void* pThread)
{
pthread_setspecific(_currentKey, pThread);
#if defined(POCO_OS_FAMILY_UNIX)
sigset_t sset;
sigemptyset(&sset);
sigaddset(&sset, SIGQUIT);
sigaddset(&sset, SIGTERM);
sigaddset(&sset, SIGPIPE);
pthread_sigmask(SIG_BLOCK, &sset, 0);
#endif
try
{
reinterpret_cast<ThreadImp*>(pThread)->_pTarget->run();
}
catch (...)
{
}
return 0;
}
int ThreadImp::mapPrio(int prio)
{
#if defined(__VMS) || defined(__digital__)
static const int pmin = PRI_OTHER_MIN;
static const int pmax = PRI_OTHER_MAX;
#else
static const int pmin = sched_get_priority_min(SCHED_OTHER);
static const int pmax = sched_get_priority_max(SCHED_OTHER);
#endif
switch (prio)
{
case PRIO_LOWEST_IMP:
return pmin;
case PRIO_LOW_IMP:
return pmin + (pmax - pmin)/4;
case PRIO_NORMAL_IMP:
return pmin + (pmax - pmin)/2;
case PRIO_HIGH_IMP:
return pmin + 3*(pmax - pmin)/4;
case PRIO_HIGHEST_IMP:
return pmax;
default:
poco_bugcheck_msg("invalid thread priority");
}
return -1; // just to satisfy compiler - we'll never get here anyway
}
Foundation_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -