92 lines
2.0 KiB
C++
Executable File
92 lines
2.0 KiB
C++
Executable File
/*
|
|
* This file is a part of Winix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2010, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#ifndef headerfilecmslucorebasethread
|
|
#define headerfilecmslucorebasethread
|
|
|
|
#include <pthread.h>
|
|
#include "synchro.h"
|
|
|
|
|
|
|
|
class BaseThread
|
|
{
|
|
public:
|
|
|
|
BaseThread();
|
|
|
|
// these method must be called before starting the thread
|
|
void SetSynchro(Synchro * psynchro);
|
|
void SetSignal(pthread_cond_t * psignal);
|
|
|
|
// starting the thread
|
|
bool StartThread();
|
|
|
|
// waiting until the thread exits
|
|
void WaitForThread();
|
|
|
|
|
|
|
|
// virtual methods which should/can be inherited by your class
|
|
// the methods will be called from the other thread
|
|
|
|
// initialize the thread
|
|
// (global objects are locked)
|
|
// if it returns false then the thread immediately exists
|
|
virtual bool Init() { return false; }
|
|
|
|
// uninitialize the thread
|
|
// this is called before the thread is prepare to detach
|
|
// (global objects are locked)
|
|
virtual void Uninit() {}
|
|
|
|
// signal came
|
|
// check specific job and return true to call Do() next
|
|
// (global objects are locked -- copy some global objects to local variables)
|
|
virtual bool SignalReceived() { return false; };
|
|
|
|
// if SignalReceived() returned true then this method is called
|
|
// global object are *not* locked -- use only your local variables
|
|
// if you have to do something on global objects use synchro->Lock() and synchro->Unlock()
|
|
virtual void Do() {}
|
|
|
|
|
|
protected:
|
|
|
|
// this pointers must be set
|
|
Synchro * synchro;
|
|
pthread_cond_t * thread_signal;
|
|
|
|
pthread_t thread_id; // thread id - set automatically
|
|
|
|
void StartRoutine();
|
|
static void * StartRoutine(void *);
|
|
bool BaseInit();
|
|
void BaseUninit();
|
|
bool BaseSignalReceived();
|
|
void BaseDo();
|
|
bool WaitForSignal();
|
|
bool WaitForSignalSleep(time_t second);
|
|
|
|
bool Lock();
|
|
void Unlock();
|
|
|
|
// if the work done by Do() is long time consuming you should periodically check
|
|
// wheter there was a signal for exiting, and if it was just simply return from Do()
|
|
// (it's checking with locking and unlocking)
|
|
bool IsExitSignal();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|