//==============================================================================
// Copyright (c) 2006 Ryan Winter
//==============================================================================
#include "thread.h"

#include <illuminate/threads/lock/scoped_lock.h>

#include <SDL_thread.h>

#include <cassert>

namespace Illuminate
{

//------------------------------------------------------------------------------
// public:
//------------------------------------------------------------------------------
Thread::Thread()
    :
    m_running(false),
    m_thread(0)
{
}

//------------------------------------------------------------------------------
Thread::~Thread()
{
    assert(!m_running && "destructing a running thread, bad bad bad");
}

//------------------------------------------------------------------------------
void 
Thread::start()
{
    if (!m_running)
    {
        m_running = true;
        m_thread = SDL_CreateThread(runThread, this);
    }
}

//------------------------------------------------------------------------------
void 
Thread::join()
{
    if (m_running)
    {
        SDL_WaitThread(m_thread, NULL);
        m_running = false;
    }
}

//------------------------------------------------------------------------------
void 
Thread::terminate()
{
    SDL_KillThread(m_thread);
}

//------------------------------------------------------------------------------
bool 
Thread::isRunning() const
{
    return m_running;
}
    
//------------------------------------------------------------------------------
// private:
//------------------------------------------------------------------------------
int
Thread::runThread(void *data)
{
    Illuminate::Thread *thread = static_cast<Thread *>(data);

    thread->run();

    return true;
}


}
