2010-12-02 02:02:02 +01:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2019-03-19 19:08:09 +01:00
|
|
|
* Copyright (c) 2010-2019, Tomasz Sowa
|
2010-12-02 02:02:02 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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 HOLDER 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.
|
|
|
|
*
|
2010-12-02 02:02:02 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-23 15:15:30 +01:00
|
|
|
#ifndef headerfile_winix_core_basethread
|
|
|
|
#define headerfile_winix_core_basethread
|
2010-12-02 02:02:02 +01:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include "synchro.h"
|
2021-06-16 18:07:44 +02:00
|
|
|
#include "winixmodeldeprecated.h"
|
2018-11-21 12:03:53 +01:00
|
|
|
|
2010-12-02 02:02:02 +01:00
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-02 02:02:02 +01:00
|
|
|
|
2021-06-16 18:07:44 +02:00
|
|
|
class BaseThread : public WinixModelDeprecated
|
2010-12-02 02:02:02 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
BaseThread();
|
2018-04-17 00:44:59 +02:00
|
|
|
virtual ~BaseThread();
|
2010-12-02 02:02:02 +01:00
|
|
|
|
2021-05-20 20:59:12 +02:00
|
|
|
void set_main_log_buffer(pt::WTextStream * log_buffer);
|
|
|
|
void set_main_file_log(pt::FileLog * file_log);
|
2019-03-19 19:08:09 +01:00
|
|
|
|
2010-12-02 02:02:02 +01:00
|
|
|
|
2010-12-07 13:52:52 +01:00
|
|
|
// work mode
|
|
|
|
// we have two modes:
|
|
|
|
// 0 - there is a loop with SignalReceived() and Do()
|
|
|
|
// if SignalReceived() returns true then Do() is called
|
|
|
|
// 1 - only Work() method is called
|
|
|
|
// the thread exits after Work() has finished
|
|
|
|
// default: 0
|
|
|
|
void Mode(int mode);
|
|
|
|
|
|
|
|
// starting the second thread
|
2010-12-02 02:02:02 +01:00
|
|
|
bool StartThread();
|
|
|
|
|
2012-05-01 00:53:54 +02:00
|
|
|
|
|
|
|
// virtual methods which should/can be inherited by your class
|
|
|
|
// the methods will be called from the other thread
|
|
|
|
|
2010-12-07 13:52:52 +01:00
|
|
|
// wake up the second thread
|
|
|
|
// (if it waits for the signal)
|
|
|
|
// you should use it with: synchro->Lock() and Unlock()
|
|
|
|
// if the thread doesn't wait on a signal then nothing is done
|
2012-05-01 00:53:54 +02:00
|
|
|
virtual void WakeUpThread();
|
2010-12-07 13:52:52 +01:00
|
|
|
|
2010-12-02 02:02:02 +01:00
|
|
|
// waiting until the thread exits
|
2010-12-07 13:52:52 +01:00
|
|
|
// you should call WakeUpThread() before
|
2012-05-01 00:53:54 +02:00
|
|
|
virtual void WaitForThread();
|
2010-12-02 02:02:02 +01:00
|
|
|
|
|
|
|
// initialize the thread
|
|
|
|
// (global objects are locked)
|
2010-12-07 13:52:52 +01:00
|
|
|
// if it returns false then the thread immediately exits
|
|
|
|
// default: true
|
|
|
|
virtual bool Init() { return true; }
|
2010-12-02 02:02:02 +01:00
|
|
|
|
|
|
|
// uninitialize the thread
|
|
|
|
// this is called before the thread is prepare to detach
|
|
|
|
// (global objects are locked)
|
2010-12-07 13:52:52 +01:00
|
|
|
// it's called only if Init() returned true
|
2010-12-02 02:02:02 +01:00
|
|
|
virtual void Uninit() {}
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
// returns the thread id
|
|
|
|
// this identifier is set by StartThread() metdhod
|
|
|
|
pthread_t ThreadId();
|
|
|
|
|
|
|
|
|
2019-03-19 19:08:09 +01:00
|
|
|
protected:
|
|
|
|
|
|
|
|
// logger for the main thread
|
|
|
|
Log main_log;
|
|
|
|
|
|
|
|
// log from WinixBase is meant to be used by the other thread
|
|
|
|
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
2010-12-07 13:52:52 +01:00
|
|
|
// signal came (work mode = 0 - default)
|
|
|
|
// signal comes when an other thread calls WakeUpThread() method
|
2010-12-02 02:02:02 +01:00
|
|
|
// 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
|
2010-12-07 13:52:52 +01:00
|
|
|
// global objects are *not* locked -- use only your local variables
|
2010-12-02 02:02:02 +01:00
|
|
|
// if you have to do something on global objects use synchro->Lock() and synchro->Unlock()
|
|
|
|
virtual void Do() {}
|
|
|
|
|
2010-12-07 13:52:52 +01:00
|
|
|
// this method is called after Init() when Mode(1) is used
|
|
|
|
// this is for long-time job
|
|
|
|
// this method is called only once
|
|
|
|
// global objects are *not* locked
|
|
|
|
virtual void Work() {}
|
|
|
|
|
2010-12-02 02:02:02 +01:00
|
|
|
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
|
2010-12-02 02:02:02 +01:00
|
|
|
protected:
|
|
|
|
|
2010-12-07 13:52:52 +01:00
|
|
|
pthread_t thread_id; // thread id - set by StartThread()
|
|
|
|
pthread_cond_t thread_signal;
|
|
|
|
int work_mode;
|
2010-12-11 23:55:48 +01:00
|
|
|
bool wake_up_was_called;
|
2010-12-02 02:02:02 +01:00
|
|
|
|
2010-12-07 13:52:52 +01:00
|
|
|
void SignalLoop();
|
2010-12-02 02:02:02 +01:00
|
|
|
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();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
} // namespace Winix
|
|
|
|
|
|
|
|
|
2010-12-02 02:02:02 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|