#include <MThread.h>

#include <pthread.h>
#include <sys/resource.h>  // PRIO_PROCESS

// ----------------------------------------------------------------------
//
// Constructor
//
// Starts the derived thread if you don't specify false.
//
MThread::MThread(bool start) : fIsRunning(false), fIsDetached(false), fThread(NULL), fReturn(NULL), fPriority(0)
{
    if (start)
        Start();
}

// ----------------------------------------------------------------------
//
// Destructor
//
// Stops the derived thread if it is still running.
//
MThread::~MThread()
{
    if (fIsRunning)
        Stop();
}

// ----------------------------------------------------------------------
//
// Detach the derived thread. This means, that if the thread ends the
// needed resources (eg. for storing the return code) are freed.
// In other word you cannot get any value back from the Thread.
//
void MThread::Detach()
{
    if (fIsRunning)
        pthread_detach(*fThread);

    fIsDetached = true;
}

// ----------------------------------------------------------------------
//
// Sets the priority of the thread.
//  -20 highest priority
//    0 standard
//  +20 lowest priority
// This can only be done before the thread is started.
//
bool MThread::SetPriority(int prio)
{
    if (fIsRunning)
        return false;

    fPriority = prio;
    return true;
}

// ----------------------------------------------------------------------
//
// Now we are back in a class instance, but running in new thread.
// All class members can be accessed like before.
// Set the flag for a running thread. Check if the thread should get
// detached. Set the priority of the thread. Now reset the stop flag and
// execute the thread. After the thread stopped it's execution reset the
// running flag and exit.
//
void *MThread::RunThread()
{
    fIsRunning = true;

    if (fIsDetached)
        pthread_detach(pthread_self());

    setpriority(PRIO_PROCESS, 0, fPriority); //lowest priority

    fStop = false;

    void *rc = Thread();

    fIsRunning = false;

    return rc;
}

// ----------------------------------------------------------------------
//
// Get the Instance back from the thread argument and call the
// RunThread member function, which handles all MThread bspecific stuff.
//
void *MThread::MapThread(void *arg)
{
    MThread *thread = (MThread*)arg;

    return thread->RunThread();
}

// ----------------------------------------------------------------------
//
// A thread is created and started (MapThread).
// As an argument the actual instance is used.
//
void MThread::Start()
{
    fThread = new pthread_t;
    pthread_create(fThread, NULL, MapThread, this);
}

// ----------------------------------------------------------------------
//
// Check if a thread is existing and running.
// If the thread is detached, cancel the thread. Otherwise set the stop
// flag and wait for the thread to exit.
//
void MThread::Stop()
{
    if (!fThread || !fIsRunning)
        return;

    if (fIsDetached)
    {
        pthread_cancel(*fThread);
        fIsRunning = false;
    }
    else
    {
        fStop = true;
        pthread_join(*fThread, &fReturn);
    }

    delete fThread;
    fThread = NULL;
}
