SASC
DVBSoftCAM

cThread Class Reference

#include <thread.h>

Inheritance diagram for cThread:

List of all members.

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

Detailed Description

Definition at line 77 of file thread.h.


Constructor & Destructor Documentation

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]

Definition at line 209 of file thread.cpp.

References Cancel(), and free.

        {
          Cancel(); // just in case the derived class didn't call it
          free(description);
        }

Member Function Documentation

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 cLogStats, cCiAdapter, cRecordings, cLogStats, cPlainKeys, cAction, cNetWatcher, cScHousekeeper, cSmartCardSlot, cCardClientCCcam, cCardClientCCcam2, 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; }
bool cThread::Running ( void  ) [inline, protected]
void cThread::SetDescription ( const char *  Description,
  ... 
)

Definition at line 221 of file thread.cpp.

References free, and NULL.

Referenced by cAction::cAction(), cCam::cCam(), cDeCsaTSBuffer::cDeCsaTSBuffer(), cDevice::cDevice(), cLogStats::cLogStats(), cThread(), and cSmartCardSlot::Setup().

        {
          free(description);
          description = NULL;
          if (Description) {
             va_list ap;
             va_start(ap, Description);
             vasprintf(&description, Description, ap);
             va_end(ap);
             }
        }
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);
        }

Friends And Related Function Documentation

friend class cThreadLock [friend]

Definition at line 78 of file thread.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines