2009-09-08 12:08:10 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
|
|
|
* Copyright (C) 2002-2009 PCSX2 Dev Team
|
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
2009-02-09 21:15:56 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 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.
|
2009-02-09 21:15:56 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2009-02-09 21:15:56 +00:00
|
|
|
*/
|
2009-09-08 12:08:10 +00:00
|
|
|
|
|
|
|
#pragma once
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
#include <errno.h> // EBUSY
|
2009-07-03 00:49:40 +00:00
|
|
|
#include <pthread.h>
|
2009-02-09 21:15:56 +00:00
|
|
|
#include <semaphore.h>
|
|
|
|
|
2009-04-29 11:47:05 +00:00
|
|
|
#include "Pcsx2Defs.h"
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-08-15 06:17:43 +00:00
|
|
|
namespace Exception
|
|
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Thread termination exception, used to quickly terminate threads from anywhere in the
|
|
|
|
// thread's call stack. This exception is handled by the PCSX2 PersistentThread class. Threads
|
|
|
|
// not derived from that class will not handle this exception.
|
|
|
|
//
|
2009-09-01 00:43:28 +00:00
|
|
|
class ThreadTermination
|
2009-08-15 06:17:43 +00:00
|
|
|
{
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-08-25 15:38:48 +00:00
|
|
|
class wxTimeSpan;
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
namespace Threading
|
|
|
|
{
|
2009-08-15 06:17:43 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
2009-02-09 21:15:56 +00:00
|
|
|
// Define some useful object handles - wait events, mutexes.
|
|
|
|
|
2009-07-17 03:43:45 +00:00
|
|
|
// pthread Cond is an evil api that is not suited for Pcsx2 needs.
|
|
|
|
// Let's not use it. Use mutexes and semaphores instead to create waits. (Air)
|
|
|
|
#if 0
|
2009-02-09 21:15:56 +00:00
|
|
|
struct WaitEvent
|
|
|
|
{
|
|
|
|
pthread_cond_t cond;
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
|
|
|
|
WaitEvent();
|
|
|
|
~WaitEvent();
|
|
|
|
|
|
|
|
void Set();
|
|
|
|
void Wait();
|
|
|
|
};
|
2009-07-17 03:43:45 +00:00
|
|
|
#endif
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
struct Semaphore
|
|
|
|
{
|
|
|
|
sem_t sema;
|
|
|
|
|
|
|
|
Semaphore();
|
|
|
|
~Semaphore();
|
|
|
|
|
2009-02-28 20:55:53 +00:00
|
|
|
void Reset();
|
2009-02-09 21:15:56 +00:00
|
|
|
void Post();
|
|
|
|
void Post( int multiple );
|
2009-08-25 15:38:48 +00:00
|
|
|
|
2009-09-05 23:07:23 +00:00
|
|
|
#if wxUSE_GUI
|
|
|
|
void WaitGui();
|
2009-09-07 21:16:12 +00:00
|
|
|
bool WaitGui( const wxTimeSpan& timeout );
|
2009-09-05 23:07:23 +00:00
|
|
|
#endif
|
2009-02-09 21:15:56 +00:00
|
|
|
void Wait();
|
2009-09-07 21:16:12 +00:00
|
|
|
bool Wait( const wxTimeSpan& timeout );
|
2009-08-15 06:17:43 +00:00
|
|
|
void WaitNoCancel();
|
|
|
|
int Count();
|
2009-02-09 21:15:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct MutexLock
|
|
|
|
{
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
|
|
|
|
MutexLock();
|
2009-07-03 06:05:48 +00:00
|
|
|
MutexLock( bool isRecursive );
|
2009-02-09 21:15:56 +00:00
|
|
|
~MutexLock();
|
|
|
|
|
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
|
|
|
};
|
|
|
|
|
2009-03-16 18:32:18 +00:00
|
|
|
// Returns the number of available logical CPUs (cores plus hyperthreaded cpus)
|
2009-02-09 21:15:56 +00:00
|
|
|
extern void CountLogicalCores( int LogicalCoresPerPhysicalCPU, int PhysicalCoresPerPhysicalCPU );
|
|
|
|
|
|
|
|
// Releases a timeslice to other threads.
|
|
|
|
extern void Timeslice();
|
|
|
|
|
|
|
|
// For use in spin/wait loops.
|
|
|
|
extern void SpinWait();
|
2009-08-20 23:05:26 +00:00
|
|
|
|
2009-02-28 20:55:53 +00:00
|
|
|
// sleeps the current thread for the given number of milliseconds.
|
|
|
|
extern void Sleep( int ms );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-03-16 18:32:18 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
2009-08-15 06:17:43 +00:00
|
|
|
// PersistentThread - Helper class for the basics of starting/managing persistent threads.
|
2009-03-16 18:32:18 +00:00
|
|
|
//
|
2009-08-15 06:17:43 +00:00
|
|
|
// Use this as a base class for your threaded procedure, and implement the 'int ExecuteTask()'
|
2009-08-25 15:38:48 +00:00
|
|
|
// method. Use Start() and Cancel() to start and shutdown the thread, and use m_sem_event
|
2009-03-16 18:32:18 +00:00
|
|
|
// internally to post/receive events for the thread (make a public accessor for it in your
|
|
|
|
// derived class if your thread utilizes the post).
|
|
|
|
//
|
|
|
|
// Notes:
|
2009-09-03 11:59:05 +00:00
|
|
|
// * Constructing threads as static global vars isn't recommended since it can potentially
|
|
|
|
// confuse w32pthreads, if the static initializers are executed out-of-order (C++ offers
|
2009-08-15 06:17:43 +00:00
|
|
|
// no dependency options for ensuring correct static var initializations). Use heap
|
|
|
|
// allocation to create thread objects instead.
|
2009-03-16 18:32:18 +00:00
|
|
|
//
|
2009-08-20 14:34:48 +00:00
|
|
|
class PersistentThread
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-08-20 14:34:48 +00:00
|
|
|
DeclareNoncopyableObject(PersistentThread)
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
protected:
|
|
|
|
typedef int (*PlainJoeFP)();
|
2009-08-15 06:17:43 +00:00
|
|
|
pthread_t m_thread;
|
2009-08-25 15:38:48 +00:00
|
|
|
Semaphore m_sem_event; // general wait event that's needed by most threads.
|
2009-09-07 21:16:12 +00:00
|
|
|
Semaphore m_sem_finished; // used for canceling and closing threads in a deadlock-safe manner
|
2009-09-03 11:59:05 +00:00
|
|
|
sptr m_returncode; // value returned from the thread on close.
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-09-03 11:59:05 +00:00
|
|
|
volatile long m_detached; // a boolean value which indicates if the m_thread handle is valid
|
|
|
|
volatile long m_running; // set true by Start(), and set false by Cancel(), Block(), etc.
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
public:
|
2009-09-16 17:23:02 +00:00
|
|
|
virtual ~PersistentThread() throw();
|
2009-08-15 06:17:43 +00:00
|
|
|
PersistentThread();
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-02-17 01:38:02 +00:00
|
|
|
virtual void Start();
|
2009-08-15 06:17:43 +00:00
|
|
|
virtual void Cancel( bool isBlocking = true );
|
2009-09-03 11:59:05 +00:00
|
|
|
virtual void Detach();
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
// Gets the return code of the thread.
|
|
|
|
// Throws std::logic_error if the thread has not terminated.
|
2009-08-15 06:17:43 +00:00
|
|
|
virtual int GetReturnCode() const;
|
2009-08-20 23:05:26 +00:00
|
|
|
|
2009-08-15 06:17:43 +00:00
|
|
|
virtual bool IsRunning() const;
|
2009-08-20 23:05:26 +00:00
|
|
|
virtual sptr Block();
|
2009-08-31 03:47:05 +00:00
|
|
|
|
|
|
|
bool IsSelf() const;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-09-03 11:59:05 +00:00
|
|
|
virtual void DoThreadCleanup();
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
protected:
|
|
|
|
// Used to dispatch the thread callback function.
|
|
|
|
// (handles some thread cleanup on Win32, and is basically a typecast
|
|
|
|
// on linux).
|
|
|
|
static void* _internal_callback( void* func );
|
|
|
|
|
2009-03-16 18:32:18 +00:00
|
|
|
// Implemented by derived class to handle threading actions!
|
2009-08-15 06:17:43 +00:00
|
|
|
virtual sptr ExecuteTask()=0;
|
2009-02-09 21:15:56 +00:00
|
|
|
};
|
|
|
|
|
2009-03-16 18:32:18 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ScopedLock: Helper class for using Mutexes.
|
|
|
|
// Using this class provides an exception-safe (and generally clean) method of locking
|
2009-03-18 18:56:03 +00:00
|
|
|
// code inside a function or conditional block.
|
|
|
|
//
|
2009-08-20 14:34:48 +00:00
|
|
|
class ScopedLock
|
2009-03-16 18:32:18 +00:00
|
|
|
{
|
2009-08-20 14:34:48 +00:00
|
|
|
DeclareNoncopyableObject(ScopedLock)
|
2009-08-20 23:05:26 +00:00
|
|
|
|
2009-03-16 18:32:18 +00:00
|
|
|
protected:
|
|
|
|
MutexLock& m_lock;
|
2009-08-15 06:17:43 +00:00
|
|
|
bool m_IsLocked;
|
2009-03-16 18:32:18 +00:00
|
|
|
|
|
|
|
public:
|
2009-09-16 17:23:02 +00:00
|
|
|
virtual ~ScopedLock() throw()
|
2009-03-16 18:32:18 +00:00
|
|
|
{
|
2009-08-15 06:17:43 +00:00
|
|
|
if( m_IsLocked )
|
|
|
|
m_lock.Unlock();
|
2009-03-16 18:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedLock( MutexLock& locker ) :
|
2009-08-15 06:17:43 +00:00
|
|
|
m_lock( locker )
|
|
|
|
, m_IsLocked( true )
|
|
|
|
{
|
|
|
|
m_lock.Lock();
|
|
|
|
}
|
2009-08-20 23:05:26 +00:00
|
|
|
|
2009-08-15 06:17:43 +00:00
|
|
|
// Provides manual unlocking of a scoped lock prior to object destruction.
|
|
|
|
void Unlock()
|
2009-03-16 18:32:18 +00:00
|
|
|
{
|
2009-08-15 06:17:43 +00:00
|
|
|
if( !m_IsLocked ) return;
|
|
|
|
m_IsLocked = false;
|
|
|
|
m_lock.Unlock();
|
|
|
|
}
|
2009-08-20 23:05:26 +00:00
|
|
|
|
2009-08-15 06:17:43 +00:00
|
|
|
// provides manual locking of a scoped lock, to re-lock after a manual unlocking.
|
|
|
|
void Lock()
|
|
|
|
{
|
|
|
|
if( m_IsLocked ) return;
|
2009-03-16 18:32:18 +00:00
|
|
|
m_lock.Lock();
|
2009-08-15 06:17:43 +00:00
|
|
|
m_IsLocked = true;
|
2009-03-16 18:32:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-03-18 18:56:03 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BaseTaskThread - an abstract base class which provides simple parallel execution of
|
|
|
|
// single tasks.
|
|
|
|
//
|
|
|
|
// Implementation:
|
|
|
|
// To use this class your derived class will need to implement its own Task() function
|
|
|
|
// and also a "StartTask( parameters )" function which suits the need of your task, along
|
|
|
|
// with any local variables your task needs to do its job. You may additionally want to
|
|
|
|
// implement a "GetResult()" function, which would be a combination of WaitForResult()
|
|
|
|
// and a return value of the computational result.
|
|
|
|
//
|
|
|
|
// Thread Safety:
|
|
|
|
// If operating on local variables, you must execute WaitForResult() before leaving the
|
|
|
|
// variable scope -- or alternatively have your StartTask() implementation make full
|
|
|
|
// copies of dependent data. Also, by default PostTask() always assumes the previous
|
|
|
|
// task has completed. If your system can post a new task before the previous one has
|
|
|
|
// completed, then it needs to explicitly call WaitForResult() or provide a mechanism
|
|
|
|
// to cancel the previous task (which is probably more work than it's worth).
|
|
|
|
//
|
|
|
|
// Performance notes:
|
|
|
|
// * Remember that thread creation is generally slow, so you should make your object
|
|
|
|
// instance once early and then feed it tasks repeatedly over the course of program
|
|
|
|
// execution.
|
|
|
|
//
|
|
|
|
// * For threading to be a successful speedup, the task being performed should be as lock
|
|
|
|
// free as possible. For example using STL containers in parallel usually fails to
|
|
|
|
// yield any speedup due to the gratuitous amount of locking that the STL performs
|
|
|
|
// internally.
|
|
|
|
//
|
|
|
|
// * The best application of tasking threads is to divide a large loop over a linear array
|
|
|
|
// into smaller sections. For example, if you have 20,000 items to process, the task
|
|
|
|
// can be divided into two threads of 10,000 items each.
|
2009-08-20 23:05:26 +00:00
|
|
|
//
|
2009-08-15 06:17:43 +00:00
|
|
|
class BaseTaskThread : public PersistentThread
|
2009-03-18 18:56:03 +00:00
|
|
|
{
|
|
|
|
protected:
|
2009-08-15 06:17:43 +00:00
|
|
|
volatile bool m_Done;
|
2009-09-16 17:23:02 +00:00
|
|
|
volatile bool m_TaskPending;
|
2009-08-15 06:17:43 +00:00
|
|
|
Semaphore m_post_TaskComplete;
|
2009-09-16 17:23:02 +00:00
|
|
|
MutexLock m_lock_TaskComplete;
|
2009-03-18 18:56:03 +00:00
|
|
|
|
|
|
|
public:
|
2009-09-16 17:23:02 +00:00
|
|
|
virtual ~BaseTaskThread() throw() {}
|
2009-03-18 18:56:03 +00:00
|
|
|
BaseTaskThread() :
|
2009-08-15 06:17:43 +00:00
|
|
|
m_Done( false )
|
2009-09-16 17:23:02 +00:00
|
|
|
, m_TaskPending( false )
|
2009-08-15 06:17:43 +00:00
|
|
|
, m_post_TaskComplete()
|
2009-03-18 18:56:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-09-16 17:23:02 +00:00
|
|
|
sptr Block();
|
|
|
|
void PostTask();
|
|
|
|
void WaitForResult();
|
2009-08-20 23:05:26 +00:00
|
|
|
|
2009-03-18 18:56:03 +00:00
|
|
|
protected:
|
|
|
|
// Abstract method run when a task has been posted. Implementing classes should do
|
|
|
|
// all your necessary processing work here.
|
|
|
|
virtual void Task()=0;
|
|
|
|
|
2009-09-16 17:23:02 +00:00
|
|
|
sptr ExecuteTask();
|
2009-03-18 18:56:03 +00:00
|
|
|
};
|
2009-03-16 18:32:18 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Our fundamental interlocking functions. All other useful interlocks can be derived
|
|
|
|
// from these little beasties!
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
extern void AtomicExchange( volatile u32& Target, u32 value );
|
|
|
|
extern void AtomicExchangeAdd( volatile u32& Target, u32 value );
|
|
|
|
extern void AtomicIncrement( volatile u32& Target );
|
|
|
|
extern void AtomicDecrement( volatile u32& Target );
|
|
|
|
extern void AtomicExchange( volatile s32& Target, s32 value );
|
|
|
|
extern void AtomicExchangeAdd( volatile s32& Target, u32 value );
|
|
|
|
extern void AtomicIncrement( volatile s32& Target );
|
|
|
|
extern void AtomicDecrement( volatile s32& Target );
|
|
|
|
|
|
|
|
extern void _AtomicExchangePointer( const void ** target, const void* value );
|
|
|
|
extern void _AtomicCompareExchangePointer( const void ** target, const void* value, const void* comparand );
|
|
|
|
|
|
|
|
#define AtomicExchangePointer( target, value ) \
|
|
|
|
_AtomicExchangePointer( (const void**)(&target), (const void*)(value) )
|
|
|
|
|
|
|
|
#define AtomicCompareExchangePointer( target, value, comparand ) \
|
|
|
|
_AtomicCompareExchangePointer( (const void**)(&target), (const void*)(value), (const void*)(comparand) )
|
|
|
|
|
|
|
|
}
|
|
|
|
|