2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-08 04:46:09 +00:00
|
|
|
|
|
|
|
// 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, version 2.0.
|
|
|
|
|
|
|
|
// 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 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
#ifndef _THREAD_H_
|
|
|
|
#define _THREAD_H_
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2011-01-27 20:47:58 +00:00
|
|
|
#include "StdThread.h"
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2009-03-18 17:17:58 +00:00
|
|
|
// Don't include common.h here as it will break LogManager
|
|
|
|
#include "CommonTypes.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2009-02-24 04:54:00 +00:00
|
|
|
// This may not be defined outside _WIN32
|
|
|
|
#ifndef _WIN32
|
2010-04-29 13:46:20 +00:00
|
|
|
#ifndef INFINITE
|
|
|
|
#define INFINITE 0xffffffff
|
|
|
|
#endif
|
2010-01-09 19:06:23 +00:00
|
|
|
|
2011-01-27 20:47:58 +00:00
|
|
|
#include <xmmintrin.h>
|
|
|
|
|
2010-11-22 02:42:53 +00:00
|
|
|
//for gettimeofday and struct time(spec|val)
|
2010-01-09 19:06:23 +00:00
|
|
|
#include <time.h>
|
2010-11-20 03:24:51 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2009-02-24 04:54:00 +00:00
|
|
|
|
2009-02-22 21:16:12 +00:00
|
|
|
|
2008-12-08 04:46:09 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2011-01-27 20:47:58 +00:00
|
|
|
|
|
|
|
int CurrentThreadId();
|
|
|
|
|
|
|
|
void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask);
|
|
|
|
void SetCurrentThreadAffinity(u32 mask);
|
2010-04-29 13:46:20 +00:00
|
|
|
|
|
|
|
class CriticalSection
|
|
|
|
{
|
2008-12-08 04:46:09 +00:00
|
|
|
#ifdef _WIN32
|
2010-04-29 13:46:20 +00:00
|
|
|
CRITICAL_SECTION section;
|
2009-01-09 12:10:02 +00:00
|
|
|
#else
|
2009-05-01 15:17:03 +00:00
|
|
|
#ifdef _POSIX_THREADS
|
2010-04-29 12:50:12 +00:00
|
|
|
pthread_mutex_t mutex;
|
2008-12-08 04:46:09 +00:00
|
|
|
#endif
|
2009-05-01 15:17:03 +00:00
|
|
|
#endif
|
2010-04-29 13:46:20 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
CriticalSection(int spincount = 1000);
|
|
|
|
~CriticalSection();
|
|
|
|
void Enter();
|
|
|
|
bool TryEnter();
|
|
|
|
void Leave();
|
|
|
|
};
|
|
|
|
|
2010-01-14 10:52:14 +00:00
|
|
|
#ifdef _WIN32
|
2010-04-29 13:46:20 +00:00
|
|
|
// Event(WaitForSingleObject) is too expensive
|
|
|
|
// as it always enters Ring0 regardless of the state of lock
|
|
|
|
// This EventEx will try to stay in Ring3 as much as possible
|
|
|
|
// If the lock can be obtained in the first time, Ring0 won't be entered at all
|
|
|
|
class EventEx
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EventEx();
|
|
|
|
void Init();
|
|
|
|
void Shutdown();
|
|
|
|
void Set();
|
|
|
|
// Infinite wait
|
|
|
|
void Spin();
|
|
|
|
// Infinite wait with sleep
|
|
|
|
void Wait();
|
|
|
|
// Wait with message processing and sleep
|
|
|
|
bool MsgWait();
|
|
|
|
private:
|
|
|
|
volatile long m_Lock;
|
|
|
|
};
|
2010-01-14 10:52:14 +00:00
|
|
|
#else
|
2010-04-29 13:46:20 +00:00
|
|
|
// TODO: implement for Linux
|
2010-01-14 10:52:14 +00:00
|
|
|
#define EventEx Event
|
|
|
|
#endif
|
2010-04-29 13:46:20 +00:00
|
|
|
|
|
|
|
class Event
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Event();
|
|
|
|
void Init();
|
|
|
|
void Shutdown();
|
|
|
|
|
|
|
|
void Set();
|
|
|
|
//returns whether the wait timed out
|
|
|
|
bool Wait(const u32 timeout = INFINITE);
|
2009-02-22 22:49:42 +00:00
|
|
|
#ifdef _WIN32
|
2010-04-29 13:46:20 +00:00
|
|
|
void MsgWait();
|
2009-02-22 22:49:42 +00:00
|
|
|
#else
|
2010-04-29 13:46:20 +00:00
|
|
|
void MsgWait() {Wait();}
|
2009-02-22 22:49:42 +00:00
|
|
|
#endif
|
2010-04-29 13:46:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
private:
|
2008-12-08 04:46:09 +00:00
|
|
|
#ifdef _WIN32
|
2010-04-29 13:46:20 +00:00
|
|
|
|
|
|
|
HANDLE m_hEvent;
|
|
|
|
/* If we have waited more than five seconds we can be pretty sure that the thread is deadlocked.
|
|
|
|
So then we can just as well continue and hope for the best. I could try several times that
|
|
|
|
this works after a five second timeout (with works meaning that the game stopped and I could
|
|
|
|
start another game without any noticable problems). But several times it failed to, and ended
|
|
|
|
with a crash. But it's better than an infinite deadlock. */
|
|
|
|
static const int THREAD_WAIT_TIMEOUT = 5000; // INFINITE or 5000 for example
|
|
|
|
|
2009-01-09 12:10:02 +00:00
|
|
|
#else
|
2010-04-29 13:46:20 +00:00
|
|
|
|
|
|
|
bool is_set_;
|
2009-05-01 15:17:03 +00:00
|
|
|
#ifdef _POSIX_THREADS
|
2010-04-29 13:46:20 +00:00
|
|
|
pthread_cond_t event_;
|
|
|
|
pthread_mutex_t mutex_;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
void SleepCurrentThread(int ms);
|
2010-06-16 14:22:17 +00:00
|
|
|
void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms
|
|
|
|
|
2010-04-29 13:46:20 +00:00
|
|
|
// Use this function during a spin-wait to make the current thread
|
|
|
|
// relax while another thread is working. This may be more efficient
|
|
|
|
// than using events because event functions use kernel calls.
|
|
|
|
inline void YieldCPU()
|
|
|
|
{
|
2009-07-17 22:57:02 +00:00
|
|
|
#ifdef _WIN32
|
2011-02-27 18:06:55 +00:00
|
|
|
Sleep(0);
|
2009-07-23 21:17:14 +00:00
|
|
|
#elif defined(_M_IX86) || defined(_M_X64)
|
2011-03-02 22:19:56 +00:00
|
|
|
sleep(0);
|
2009-07-17 22:57:02 +00:00
|
|
|
#endif
|
2010-04-29 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetCurrentThreadName(const char *name);
|
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
} // namespace Common
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2009-05-07 21:16:52 +00:00
|
|
|
#endif // _THREAD_H_
|