2010-04-25 00:31:27 +00:00
|
|
|
/*
|
2010-04-24 21:37:39 +00:00
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
* http://www.gabest.org
|
|
|
|
*
|
|
|
|
* This Program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2010-04-24 21:37:39 +00:00
|
|
|
* This Program 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.
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2010-04-24 21:37:39 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
2010-04-25 00:31:27 +00:00
|
|
|
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-04-24 21:37:39 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
|
2010-04-24 21:37:39 +00:00
|
|
|
class GSThread
|
|
|
|
{
|
|
|
|
DWORD m_ThreadId;
|
|
|
|
HANDLE m_hThread;
|
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
static DWORD WINAPI StaticThreadProc(void* lpParam);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void ThreadProc() = 0;
|
|
|
|
|
|
|
|
void CreateThread();
|
|
|
|
void CloseThread();
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSThread();
|
|
|
|
virtual ~GSThread();
|
|
|
|
};
|
|
|
|
|
|
|
|
class GSCritSec
|
|
|
|
{
|
|
|
|
CRITICAL_SECTION m_cs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSCritSec() {InitializeCriticalSection(&m_cs);}
|
|
|
|
~GSCritSec() {DeleteCriticalSection(&m_cs);}
|
|
|
|
|
|
|
|
void Lock() {EnterCriticalSection(&m_cs);}
|
|
|
|
bool TryLock() {return TryEnterCriticalSection(&m_cs) == TRUE;}
|
|
|
|
void Unlock() {LeaveCriticalSection(&m_cs);}
|
|
|
|
};
|
|
|
|
|
|
|
|
class GSAutoResetEvent
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
HANDLE m_hEvent;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSAutoResetEvent() {m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);}
|
|
|
|
~GSAutoResetEvent() {CloseHandle(m_hEvent);}
|
|
|
|
|
|
|
|
void Set() {SetEvent(m_hEvent);}
|
|
|
|
bool Wait() {return WaitForSingleObject(m_hEvent, INFINITE) == WAIT_OBJECT_0;}
|
|
|
|
};
|
2011-03-27 03:12:12 +00:00
|
|
|
/*
|
|
|
|
class GSAutoResetEvent
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
long m_sync;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSAutoResetEvent() {m_sync = 0;}
|
|
|
|
~GSAutoResetEvent() {}
|
|
|
|
|
|
|
|
void Set() {_interlockedbittestandset(&m_sync, 0);}
|
|
|
|
bool Wait() {while(!_interlockedbittestandreset(&m_sync, 0)) _mm_pause(); return true;}
|
|
|
|
};
|
|
|
|
*/
|
2011-02-19 03:36:30 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <pthread.h>
|
2011-02-19 10:27:10 +00:00
|
|
|
#include <semaphore.h>
|
2011-02-19 03:36:30 +00:00
|
|
|
|
|
|
|
class GSThread
|
|
|
|
{
|
|
|
|
pthread_attr_t m_thread_attr;
|
|
|
|
pthread_t m_thread;
|
|
|
|
|
|
|
|
static void* StaticThreadProc(void* param);
|
2010-04-24 21:37:39 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void ThreadProc() = 0;
|
|
|
|
|
|
|
|
void CreateThread();
|
|
|
|
void CloseThread();
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSThread();
|
|
|
|
virtual ~GSThread();
|
|
|
|
};
|
2011-02-19 03:36:30 +00:00
|
|
|
|
|
|
|
class GSCritSec
|
|
|
|
{
|
|
|
|
pthread_mutexattr_t m_mutex_attr;
|
|
|
|
pthread_mutex_t m_mutex;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSCritSec()
|
|
|
|
{
|
|
|
|
pthread_mutexattr_init(&m_mutex_attr);
|
|
|
|
pthread_mutexattr_settype(&m_mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
|
|
|
|
pthread_mutex_init(&m_mutex, &m_mutex_attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
~GSCritSec()
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&m_mutex);
|
|
|
|
pthread_mutexattr_destroy(&m_mutex_attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Lock() {pthread_mutex_lock(&m_mutex);}
|
|
|
|
bool TryLock() {return pthread_mutex_trylock(&m_mutex) == 0;}
|
|
|
|
void Unlock() {pthread_mutex_unlock(&m_mutex);}
|
|
|
|
};
|
|
|
|
|
|
|
|
class GSAutoResetEvent
|
|
|
|
{
|
|
|
|
protected:
|
2011-02-19 10:27:10 +00:00
|
|
|
sem_t m_sem;
|
2011-02-19 03:36:30 +00:00
|
|
|
|
|
|
|
public:
|
2011-02-19 10:27:10 +00:00
|
|
|
GSAutoResetEvent() {sem_init(&m_sem, 0, 0);}
|
|
|
|
~GSAutoResetEvent() {sem_destroy(&m_sem);}
|
2011-02-19 03:36:30 +00:00
|
|
|
|
2011-02-19 10:27:10 +00:00
|
|
|
void Set() {sem_post(&m_sem);}
|
|
|
|
bool Wait() {return sem_wait(&m_sem) == 0;}
|
2011-02-19 03:36:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class GSAutoLock
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
GSCritSec* m_cs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSAutoLock(GSCritSec* cs) {m_cs = cs; m_cs->Lock();}
|
|
|
|
~GSAutoLock() {m_cs->Unlock();}
|
|
|
|
};
|