//================ Copyright (c) 2020, PG, All rights reserved. =================//
//
// Purpose:		thread wrapper
//
// $NoKeywords: $thread $os
//===============================================================================//

#include "Thread.h"

#include <pthread.h>

#include "ConVar.h"
#include "Engine.h"

// pthread implementation of Thread
class PosixThread : public BaseThread {
   public:
    PosixThread(McThread::START_ROUTINE start_routine, void *arg) : BaseThread() {
        m_thread = 0;

        const int ret = pthread_create(&m_thread, NULL, start_routine, arg);
        m_bReady = (ret == 0);

        if(ret != 0) debugLog("PosixThread Error: pthread_create() returned %i!\n", ret);
    }

    virtual ~PosixThread() {
        if(!m_bReady) return;

        m_bReady = false;

        pthread_join(m_thread, NULL);

        m_thread = 0;
    }

    bool isReady() { return m_bReady; }

   private:
    pthread_t m_thread;

    bool m_bReady;
};

ConVar debug_thread("debug_thread", false, FCVAR_DEFAULT);

ConVar *McThread::debug = &debug_thread;

McThread::McThread(START_ROUTINE start_routine, void *arg) {
    m_baseThread = NULL;
    m_baseThread = new PosixThread(start_routine, arg);
}

McThread::~McThread() { SAFE_DELETE(m_baseThread); }

bool McThread::isReady() { return (m_baseThread != NULL && m_baseThread->isReady()); }
