|
SASC-NG
DVBSoftCAM
|
#include <thread.h>
Inheritance diagram for cThread:Public Member Functions | |
| cThread (const char *Description=NULL) | |
| virtual | ~cThread () |
| void | SetDescription (const char *Description,...) __attribute__((format(printf |
| void bool | Start (void) |
| bool | Active (void) |
| Checks whether the thread is still alive. | |
Static Public Member Functions | |
| static tThreadId | ThreadId (void) |
| static tThreadId | IsMainThread (void) |
| static void | SetMainThreadId (void) |
Protected Member Functions | |
| void | SetPriority (int Priority) |
| void | Lock (void) |
| void | Unlock (void) |
| virtual void | Action (void)=0 |
| bool | Running (void) |
| void | Cancel (int WaitSeconds=0) |
Friends | |
| class | cThreadLock |
| cThread::cThread | ( | const char * | Description = NULL | ) |
Creates a new thread. If Description is present, a log file entry will be made when the thread starts and stops. The Start() function must be called to actually start the thread.
Definition at line 201 of file thread.cpp.
References NULL, and SetDescription().
{
active = running = false;
childTid = 0;
description = NULL;
SetDescription("%s",Description);
}
| cThread::~cThread | ( | ) | [virtual] |
| virtual void cThread::Action | ( | void | ) | [protected, pure virtual] |
A derived cThread class must implement the code it wants to execute as a separate thread in this function. If this is a loop, it must check Running() repeatedly to see whether it's time to stop.
Implemented in cCiAdapter, cRecordings, cLogStats, cPlainKeys, cAction, cNetWatcher, cScHousekeeper, cSmartCardSlot, cCardClientCCcam, and cCardClientCCcam2.
| bool cThread::Active | ( | void | ) |
Checks whether the thread is still alive.
Definition at line 273 of file thread.cpp.
References LOG_ERROR.
Referenced by Cancel(), cAction::NewFilter(), and cNetWatcher::Up().
{
if (active) {
//
// Single UNIX Spec v2 says:
//
// The pthread_kill() function is used to request
// that a signal be delivered to the specified thread.
//
// As in kill(), if sig is zero, error checking is
// performed but no signal is actually sent.
//
int err;
if ((err = pthread_kill(childTid, 0)) != 0) {
if (err != ESRCH)
LOG_ERROR;
childTid = 0;
active = running = false;
}
else
return true;
}
return false;
}
| void cThread::Cancel | ( | int | WaitSeconds = 0 | ) | [protected] |
Cancels the thread by first setting 'running' to false, so that the Action() loop can finish in an orderly fashion and then waiting up to WaitSeconds seconds for the thread to actually end. If the thread doesn't end by itself, it is killed. If WaitSeconds is -1, only 'running' is set to false and Cancel() returns immediately, without killing the thread.
Definition at line 303 of file thread.cpp.
References Active(), esyslog, NULL, and cCondWait::SleepMs().
Referenced by cCardClientCCcam2::Logout(), cAction::~cAction(), cCam::~cCam(), cCardClientCCcam::~cCardClientCCcam(), cDeCsaTSBuffer::~cDeCsaTSBuffer(), cLogStats::~cLogStats(), cNetWatcher::~cNetWatcher(), cScHousekeeper::~cScHousekeeper(), cSmartCardSlot::~cSmartCardSlot(), and ~cThread().
{
running = false;
if (active && WaitSeconds > -1) {
if (WaitSeconds > 0) {
for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) {
if (!Active())
return;
cCondWait::SleepMs(10);
}
esyslog("ERROR: %s thread %d won't end (waited %d seconds) - canceling it...", description ? description : "", (int)childTid, WaitSeconds);
}
pthread_cancel(childTid);
childTid = 0;
active = false;
}
}
| static tThreadId cThread::IsMainThread | ( | void | ) | [inline, static] |
Definition at line 121 of file thread.h.
References ThreadId().
{ return ThreadId() == mainThreadId; }
| void cThread::Lock | ( | void | ) | [inline, protected] |
Definition at line 90 of file thread.h.
References cMutex::Lock().
Referenced by cNetWatcher::Action(), cAction::Action(), cHookManager::AddHook(), cAction::DelAllFilter(), cAction::DelFilter(), cNetWatcher::Down(), cLogger::Down(), cHookManager::Down(), cLogger::EcmStatus(), cAction::IdleFilter(), cThreadLock::Lock(), cAction::NewFilter(), cLogger::PreScan(), cHookManager::TriggerHook(), cNetWatcher::Up(), cLogger::Up(), cEcmHandler::~cEcmHandler(), and cNetWatcher::~cNetWatcher().
{ mutex.Lock(); }
| bool cThread::Running | ( | void | ) | [inline, protected] |
Returns false if a derived cThread object shall leave its Action() function.
Definition at line 97 of file thread.h.
Referenced by cCardClientCCcam2::Action(), cCardClientCCcam::Action(), cSmartCardSlot::Action(), cScHousekeeper::Action(), cNetWatcher::Action(), cAction::Action(), cLogStats::Action(), cSmartCardSlot::HaveCard(), and cSmartCardSlot::LockCard().
| void cThread::SetDescription | ( | const char * | Description, |
| ... | |||
| ) |
Definition at line 221 of file thread.cpp.
Referenced by cAction::cAction(), cCam::cCam(), cDeCsaTSBuffer::cDeCsaTSBuffer(), cDevice::cDevice(), cLogStats::cLogStats(), cThread(), and cSmartCardSlot::Setup().
| static void cThread::SetMainThreadId | ( | void | ) | [static] |
| void cThread::SetPriority | ( | int | Priority | ) | [protected] |
Definition at line 215 of file thread.cpp.
References LOG_ERROR.
Referenced by cAction::Action().
{
if (setpriority(PRIO_PROCESS, 0, Priority) < 0)
LOG_ERROR;
}
| bool cThread::Start | ( | void | ) |
Actually starts the thread. If the thread is already running, nothing happens.
Definition at line 248 of file thread.cpp.
References cTimeMs::Elapsed(), LOG_ERROR, NULL, cCondWait::SleepMs(), THREAD_STOP_SLEEP, and THREAD_STOP_TIMEOUT.
Referenced by cCam::cCam(), cDeCsaTSBuffer::cDeCsaTSBuffer(), cLogStats::cLogStats(), cScHousekeeper::cScHousekeeper(), cCardClientCCcam2::Login(), cCardClientCCcam::Login(), cAction::NewFilter(), cSmartCardSlot::Setup(), and cNetWatcher::Up().
{
if (!running) {
if (active) {
// Wait until the previous incarnation of this thread has completely ended
// before starting it newly:
cTimeMs RestartTimeout;
while (!running && active && RestartTimeout.Elapsed() < THREAD_STOP_TIMEOUT)
cCondWait::SleepMs(THREAD_STOP_SLEEP);
}
if (!active) {
active = running = true;
if (pthread_create(&childTid, NULL, (void *(*) (void *))&StartThread, (void *)this) == 0) {
pthread_detach(childTid); // auto-reap
}
else {
LOG_ERROR;
active = running = false;
return false;
}
}
}
return true;
}
| tThreadId cThread::ThreadId | ( | void | ) | [static] |
Definition at line 298 of file thread.cpp.
Referenced by cCardClientCCcam2::Action(), IsMainThread(), cCardClientCCcam2::Logout(), and syslog_with_tid().
{
return syscall(__NR_gettid);
}
| void cThread::Unlock | ( | void | ) | [inline, protected] |
Definition at line 91 of file thread.h.
References cMutex::Unlock().
Referenced by cNetWatcher::Action(), cAction::Action(), cHookManager::AddHook(), cAction::DelAllFilter(), cAction::DelFilter(), cNetWatcher::Down(), cLogger::Down(), cHookManager::Down(), cLogger::EcmStatus(), cAction::IdleFilter(), cAction::NewFilter(), cLogger::PreScan(), cHookManager::TriggerHook(), cNetWatcher::Up(), cLogger::Up(), cEcmHandler::~cEcmHandler(), cNetWatcher::~cNetWatcher(), and cThreadLock::~cThreadLock().
{ mutex.Unlock(); }
friend class cThreadLock [friend] |