Supermodel/Src/OSD/SDL/Thread.cpp

186 lines
3.3 KiB
C++
Raw Permalink Normal View History

/**
** Supermodel
** A Sega Model 3 Arcade Emulator.
** Copyright 2011 Bart Trzynadlowski, Nik Henson
**
** This file is part of Supermodel.
**
** Supermodel is free software: you can redistribute it and/or modify it under
2020-07-27 10:28:48 +00:00
** the terms of the GNU General Public License as published by the Free
** Software Foundation, either version 3 of the License, or (at your option)
** any later version.
**
** Supermodel is distributed in the hope that it will be useful, but WITHOUT
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
** more details.
**
** You should have received a copy of the GNU General Public License along
** with Supermodel. If not, see <http://www.gnu.org/licenses/>.
**/
2020-07-27 10:28:48 +00:00
/*
* Thread.cpp
2020-07-27 10:28:48 +00:00
*
* SDL-based implementation of threading primitives.
*/
#include "Thread.h"
2020-07-27 10:28:48 +00:00
#include "Supermodel.h"
#include "SDLIncludes.h"
void CThread::Sleep(UINT32 ms)
{
SDL_Delay(ms);
}
UINT32 CThread::GetTicks()
{
return SDL_GetTicks();
}
2020-04-19 08:34:58 +00:00
CThread *CThread::CreateThread(const std::string &name, ThreadStart start, void *startParam)
{
2020-04-19 08:34:58 +00:00
SDL_Thread *impl = SDL_CreateThread(start, name.c_str(), startParam);
if (impl == NULL)
return NULL;
2020-04-19 08:34:58 +00:00
return new CThread(name, impl);
}
CSemaphore *CThread::CreateSemaphore(UINT32 initVal)
{
SDL_sem *impl = SDL_CreateSemaphore(initVal);
if (impl == NULL)
return NULL;
return new CSemaphore(impl);
}
CCondVar *CThread::CreateCondVar()
{
SDL_cond *impl = SDL_CreateCond();
if (impl == NULL)
return NULL;
return new CCondVar(impl);
}
CMutex *CThread::CreateMutex()
{
SDL_mutex *impl = SDL_CreateMutex();
if (impl == NULL)
return NULL;
return new CMutex(impl);
}
const char *CThread::GetLastError()
{
return SDL_GetError();
}
2020-04-19 08:34:58 +00:00
CThread::CThread(const std::string &name, void *impl)
: m_name(name),
m_impl(impl)
2020-07-27 10:28:48 +00:00
{
//
}
CThread::~CThread()
{
2020-04-19 08:34:58 +00:00
// User should have called Wait() before thread object is destroyed
if (nullptr != m_impl)
{
2020-07-27 10:28:48 +00:00
ErrorLog("Runaway thread error. A thread was not properly halted: %s\n", GetName().c_str());
2020-04-19 08:34:58 +00:00
}
}
2020-04-19 08:34:58 +00:00
const std::string &CThread::GetName() const
{
2020-04-19 08:34:58 +00:00
return m_name;
}
2020-04-19 08:34:58 +00:00
UINT32 CThread::GetId()
{
2020-04-19 08:34:58 +00:00
return SDL_GetThreadID((SDL_Thread*)m_impl);
}
int CThread::Wait()
{
int status;
if (m_impl == NULL)
return -1;
SDL_WaitThread((SDL_Thread*)m_impl, &status);
m_impl = NULL;
return status;
}
CSemaphore::CSemaphore(void *impl) : m_impl(impl)
{
//
}
CSemaphore::~CSemaphore()
{
SDL_DestroySemaphore((SDL_sem*)m_impl);
}
UINT32 CSemaphore::GetValue()
{
return SDL_SemValue((SDL_sem*)m_impl);
}
bool CSemaphore::Wait()
{
return SDL_SemWait((SDL_sem*)m_impl) == 0;
}
bool CSemaphore::Post()
{
return SDL_SemPost((SDL_sem*)m_impl) == 0;
}
CCondVar::CCondVar(void *impl) : m_impl(impl)
{
//
}
CCondVar::~CCondVar()
{
SDL_DestroyCond((SDL_cond*)m_impl);
}
bool CCondVar::Wait(CMutex *mutex)
{
return SDL_CondWait((SDL_cond*)m_impl, (SDL_mutex*)mutex->m_impl) == 0;
}
bool CCondVar::Signal()
{
return SDL_CondSignal((SDL_cond*)m_impl) == 0;
}
bool CCondVar::SignalAll()
{
return SDL_CondBroadcast((SDL_cond*)m_impl) == 0;
}
CMutex::CMutex(void *impl) : m_impl(impl)
{
//
}
CMutex::~CMutex()
{
SDL_DestroyMutex((SDL_mutex*)m_impl);
}
bool CMutex::Lock()
{
return SDL_mutexP((SDL_mutex*)m_impl) == 0;
}
bool CMutex::Unlock()
{
return SDL_mutexV((SDL_mutex*)m_impl) == 0;
}