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
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
#include "PrecompiledHeader.h"
|
|
|
|
#include "Threading.h"
|
2009-09-29 19:16:00 +00:00
|
|
|
#include "wxBaseTools.h"
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-08-25 15:38:48 +00:00
|
|
|
#include <wx/datetime.h>
|
2009-09-05 23:07:23 +00:00
|
|
|
#include <wx/thread.h>
|
|
|
|
#include <wx/app.h>
|
2009-08-25 15:38:48 +00:00
|
|
|
|
2009-08-20 23:05:26 +00:00
|
|
|
#ifdef __LINUX__
|
|
|
|
# include <signal.h> // for pthread_kill, which is in pthread.h on w32-pthreads
|
|
|
|
#endif
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
using namespace Threading;
|
|
|
|
|
|
|
|
namespace Threading
|
|
|
|
{
|
2009-09-09 14:08:15 +00:00
|
|
|
static const wxTimeSpan ts_msec_250( 0, 0, 0, 250 );
|
2009-09-07 21:16:12 +00:00
|
|
|
|
2009-09-03 11:59:05 +00:00
|
|
|
static void _pt_callback_cleanup( void* handle )
|
|
|
|
{
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
((PersistentThread*)handle)->_ThreadCleanup();
|
2009-09-03 11:59:05 +00:00
|
|
|
}
|
|
|
|
|
2009-08-15 06:17:43 +00:00
|
|
|
PersistentThread::PersistentThread() :
|
2009-09-29 19:16:00 +00:00
|
|
|
m_name( L"PersistentThread" )
|
|
|
|
, m_thread()
|
2009-09-03 11:59:05 +00:00
|
|
|
, m_sem_event()
|
2009-09-07 21:16:12 +00:00
|
|
|
, m_sem_finished()
|
2009-09-29 19:16:00 +00:00
|
|
|
, m_lock_start( true ) // recursive mutexing!
|
|
|
|
, m_detached( true ) // start out with m_thread in detached/invalid state
|
2009-08-15 06:17:43 +00:00
|
|
|
, m_running( false )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
// This destructor performs basic "last chance" cleanup, which is a blocking join
|
|
|
|
// against the thread. Extending classes should almost always implement their own
|
|
|
|
// thread closure process, since any PersistentThread will, by design, not terminate
|
|
|
|
// unless it has been properly canceled.
|
|
|
|
//
|
|
|
|
// Thread safetly: This class must not be deleted from its own thread. That would be
|
|
|
|
// like marrying your sister, and then cheating on her with your daughter.
|
2009-09-16 17:23:02 +00:00
|
|
|
PersistentThread::~PersistentThread() throw()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
try
|
2009-09-03 11:59:05 +00:00
|
|
|
{
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
wxString logfix = L"Thread Destructor for " + m_name;
|
|
|
|
|
|
|
|
if( m_running )
|
|
|
|
{
|
|
|
|
Console.WriteLn( logfix + L": Waiting for running thread to end.");
|
|
|
|
#if wxUSE_GUI
|
|
|
|
m_sem_finished.WaitGui();
|
|
|
|
#else
|
|
|
|
m_sem_finished.Wait();
|
|
|
|
#endif
|
|
|
|
// Need to lock here so that the thread can finish shutting down before
|
|
|
|
// it gets destroyed, otherwise th mutex handle would become invalid.
|
|
|
|
ScopedLock locker( m_lock_start );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Console.WriteLn( logfix + L": thread not running.");
|
|
|
|
Sleep( 1 );
|
|
|
|
Detach();
|
2009-09-03 11:59:05 +00:00
|
|
|
}
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
DESTRUCTOR_CATCHALL
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
// Main entry point for starting or e-starting a persistent thread. This function performs necessary
|
|
|
|
// locks and checks for avoiding race conditions, and then calls OnStart() immeediately before
|
|
|
|
// the actual thread creation. Extending classes should generally not override Start(), and should
|
|
|
|
// instead override DoPrepStart instead.
|
|
|
|
//
|
2009-09-03 11:59:05 +00:00
|
|
|
// This function should not be called from the owner thread.
|
2009-08-15 06:17:43 +00:00
|
|
|
void PersistentThread::Start()
|
2009-02-17 01:38:02 +00:00
|
|
|
{
|
2009-09-29 19:16:00 +00:00
|
|
|
ScopedLock startlock( m_lock_start ); // Prevents sudden parallel startup
|
2009-08-31 03:47:05 +00:00
|
|
|
if( m_running ) return;
|
2009-09-29 19:16:00 +00:00
|
|
|
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
Detach(); // clean up previous thread handle, if one exists.
|
2009-09-08 03:44:35 +00:00
|
|
|
m_sem_finished.Reset();
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
|
|
|
|
OnStart();
|
|
|
|
|
2009-02-17 01:38:02 +00:00
|
|
|
if( pthread_create( &m_thread, NULL, _internal_callback, this ) != 0 )
|
|
|
|
throw Exception::ThreadCreationError();
|
2009-08-15 06:17:43 +00:00
|
|
|
|
2009-09-29 19:16:00 +00:00
|
|
|
m_detached = false;
|
2009-08-15 06:17:43 +00:00
|
|
|
}
|
|
|
|
|
2009-09-29 19:16:00 +00:00
|
|
|
// Returns: TRUE if the detachment was performed, or FALSE if the thread was
|
|
|
|
// already detached or isn't running at all.
|
2009-09-03 11:59:05 +00:00
|
|
|
// This function should not be called from the owner thread.
|
2009-09-29 19:16:00 +00:00
|
|
|
bool PersistentThread::Detach()
|
2009-09-03 11:59:05 +00:00
|
|
|
{
|
2009-09-05 09:21:59 +00:00
|
|
|
wxASSERT( !IsSelf() ); // not allowed from our own thread.
|
2009-09-29 19:16:00 +00:00
|
|
|
|
|
|
|
if( _InterlockedExchange( &m_detached, true ) ) return false;
|
2009-09-03 11:59:05 +00:00
|
|
|
pthread_detach( m_thread );
|
2009-09-29 19:16:00 +00:00
|
|
|
return true;
|
2009-09-03 11:59:05 +00:00
|
|
|
}
|
|
|
|
|
2009-08-15 06:17:43 +00:00
|
|
|
// Remarks:
|
|
|
|
// Provision of non-blocking Cancel() is probably academic, since destroying a PersistentThread
|
|
|
|
// object performs a blocking Cancel regardless of if you explicitly do a non-blocking Cancel()
|
|
|
|
// prior, since the ExecuteTask() method requires a valid object state. If you really need
|
|
|
|
// fire-and-forget behavior on threads, use pthreads directly for now.
|
2009-09-03 11:59:05 +00:00
|
|
|
//
|
|
|
|
// This function should not be called from the owner thread.
|
2009-08-15 06:17:43 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// isBlocking - indicates if the Cancel action should block for thread completion or not.
|
|
|
|
//
|
|
|
|
void PersistentThread::Cancel( bool isBlocking )
|
|
|
|
{
|
2009-09-29 19:16:00 +00:00
|
|
|
wxASSERT( !IsSelf() );
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
|
|
|
|
if( !m_running ) return;
|
2009-09-05 12:02:07 +00:00
|
|
|
|
2009-09-29 19:16:00 +00:00
|
|
|
if( m_detached )
|
2009-09-03 11:59:05 +00:00
|
|
|
{
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
Console.Notice( "Threading Warning: Attempted to cancel detached thread; Ignoring..." );
|
2009-09-03 11:59:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-08-15 06:17:43 +00:00
|
|
|
|
|
|
|
pthread_cancel( m_thread );
|
2009-09-03 11:59:05 +00:00
|
|
|
|
2009-08-15 06:17:43 +00:00
|
|
|
if( isBlocking )
|
2009-09-07 21:16:12 +00:00
|
|
|
{
|
|
|
|
#if wxUSE_GUI
|
2009-09-08 03:44:35 +00:00
|
|
|
m_sem_finished.WaitGui();
|
2009-09-07 21:16:12 +00:00
|
|
|
#else
|
2009-09-08 03:44:35 +00:00
|
|
|
m_sem_finished.Wait();
|
2009-09-07 21:16:12 +00:00
|
|
|
#endif
|
|
|
|
}
|
2009-02-17 01:38:02 +00:00
|
|
|
}
|
|
|
|
|
2009-08-15 06:17:43 +00:00
|
|
|
// Blocks execution of the calling thread until this thread completes its task. The
|
|
|
|
// caller should make sure to signal the thread to exit, or else blocking may deadlock the
|
|
|
|
// calling thread. Classes which extend PersistentThread should override this method
|
|
|
|
// and signal any necessary thread exit variables prior to blocking.
|
|
|
|
//
|
|
|
|
// Returns the return code of the thread.
|
|
|
|
// This method is roughly the equivalent of pthread_join().
|
|
|
|
//
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
void PersistentThread::Block()
|
2009-08-15 06:17:43 +00:00
|
|
|
{
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
pxAssertDev( !IsSelf(), "Thread deadlock detected; Block() should never be called by the owner thread." );
|
2009-09-07 21:16:12 +00:00
|
|
|
|
2009-09-23 09:53:21 +00:00
|
|
|
if( m_running )
|
2009-09-07 21:16:12 +00:00
|
|
|
#if wxUSE_GUI
|
2009-09-08 03:44:35 +00:00
|
|
|
m_sem_finished.WaitGui();
|
2009-09-07 21:16:12 +00:00
|
|
|
#else
|
2009-09-08 03:44:35 +00:00
|
|
|
m_sem_finished.Wait();
|
2009-09-07 21:16:12 +00:00
|
|
|
#endif
|
2009-08-15 06:17:43 +00:00
|
|
|
}
|
2009-09-05 09:21:59 +00:00
|
|
|
|
2009-08-31 03:47:05 +00:00
|
|
|
bool PersistentThread::IsSelf() const
|
|
|
|
{
|
|
|
|
return pthread_self() == m_thread;
|
|
|
|
}
|
2009-08-20 23:05:26 +00:00
|
|
|
|
2009-08-15 06:17:43 +00:00
|
|
|
bool PersistentThread::IsRunning() const
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-09-29 19:16:00 +00:00
|
|
|
return !!m_running;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-29 19:16:00 +00:00
|
|
|
// Throws an exception if the thread encountered one. Uses the BaseException's Rethrow() method,
|
|
|
|
// which ensures the exception type remains consistent. Debuggable stacktraces will be lost, since
|
|
|
|
// the thread will have allowed itself to terminate properly.
|
|
|
|
void PersistentThread::RethrowException() const
|
|
|
|
{
|
|
|
|
if( !m_except ) return;
|
|
|
|
m_except->Rethrow();
|
|
|
|
}
|
2009-08-20 23:05:26 +00:00
|
|
|
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
// invoked internally when canceling or exiting the thread. Extending classes should implement
|
|
|
|
// OnThreadCleanup() to extend clenup functionality.
|
|
|
|
void PersistentThread::_ThreadCleanup()
|
2009-09-03 11:59:05 +00:00
|
|
|
{
|
|
|
|
wxASSERT( IsSelf() ); // only allowed from our own thread, thanks.
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
|
|
|
|
// Typically thread cleanup needs to lock against thread startup, since both
|
|
|
|
// will perform some measure of variable inits or resets, depending on how the
|
|
|
|
// derrived class is implemented.
|
|
|
|
ScopedLock startlock( m_lock_start );
|
|
|
|
|
|
|
|
OnThreadCleanup();
|
|
|
|
|
2009-09-29 19:16:00 +00:00
|
|
|
m_running = false;
|
2009-09-08 03:44:35 +00:00
|
|
|
m_sem_finished.Post();
|
2009-09-03 11:59:05 +00:00
|
|
|
}
|
|
|
|
|
2009-09-29 19:16:00 +00:00
|
|
|
wxString PersistentThread::GetName() const
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PersistentThread::_internal_execute()
|
|
|
|
{
|
|
|
|
m_running = true;
|
|
|
|
DoSetThreadName( m_name );
|
|
|
|
|
|
|
|
try {
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
ExecuteTask();
|
2009-09-29 19:16:00 +00:00
|
|
|
}
|
|
|
|
catch( std::logic_error& ex )
|
|
|
|
{
|
|
|
|
throw Exception::LogicError( wxsFormat( L"(thread: %s) STL Logic Error: %s\n\t%s",
|
|
|
|
GetName().c_str(), wxString::FromUTF8( ex.what() ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch( Exception::LogicError& ex )
|
|
|
|
{
|
|
|
|
m_except->DiagMsg() = wxsFormat( L"(thread:%s) ", GetName() ) + m_except->DiagMsg();
|
|
|
|
ex.Rethrow();
|
|
|
|
}
|
|
|
|
catch( std::runtime_error& ex )
|
|
|
|
{
|
|
|
|
m_except = new Exception::RuntimeError(
|
|
|
|
// Diagnostic message:
|
|
|
|
wxsFormat( L"(thread: %s) STL Runtime Error: %s\n\t%s",
|
|
|
|
GetName().c_str(), wxString::FromUTF8( ex.what() )
|
|
|
|
),
|
|
|
|
|
|
|
|
// User Message (not translated, std::exception doesn't have that kind of fancy!
|
|
|
|
wxsFormat( L"A runtime error occurred in %s:\n\n%s (STL)",
|
|
|
|
GetName().c_str(), wxString::FromUTF8( ex.what() )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch( Exception::RuntimeError& ex )
|
|
|
|
{
|
|
|
|
m_except = ex.Clone();
|
|
|
|
m_except->DiagMsg() = wxsFormat( L"(thread:%s) ", GetName() ) + m_except->DiagMsg();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
void PersistentThread::OnStart() {}
|
|
|
|
void PersistentThread::OnThreadCleanup() {}
|
|
|
|
|
2009-08-15 06:17:43 +00:00
|
|
|
void* PersistentThread::_internal_callback( void* itsme )
|
|
|
|
{
|
|
|
|
jASSUME( itsme != NULL );
|
|
|
|
PersistentThread& owner = *((PersistentThread*)itsme);
|
2009-09-03 11:59:05 +00:00
|
|
|
|
|
|
|
pthread_cleanup_push( _pt_callback_cleanup, itsme );
|
2009-09-29 19:16:00 +00:00
|
|
|
owner._internal_execute();
|
2009-09-03 11:59:05 +00:00
|
|
|
pthread_cleanup_pop( true );
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
return NULL;
|
2009-08-15 06:17:43 +00:00
|
|
|
}
|
2009-09-29 19:16:00 +00:00
|
|
|
|
|
|
|
void PersistentThread::DoSetThreadName( const wxString& name )
|
|
|
|
{
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
DoSetThreadName( toUTF8(name) );
|
2009-09-29 19:16:00 +00:00
|
|
|
}
|
2009-09-22 17:28:48 +00:00
|
|
|
|
2009-09-29 19:16:00 +00:00
|
|
|
void PersistentThread::DoSetThreadName( __unused const char* name )
|
2009-09-22 17:28:48 +00:00
|
|
|
{
|
|
|
|
wxASSERT( IsSelf() ); // only allowed from our own thread, thanks.
|
|
|
|
|
|
|
|
#ifdef _WINDOWS_
|
2009-09-29 19:16:00 +00:00
|
|
|
|
|
|
|
// This code sample was borrowed form some obscure MSDN article.
|
|
|
|
// In a rare bout of sanity, it's an actual Micrsoft-published hack
|
|
|
|
// that actually works!
|
|
|
|
|
2009-09-22 17:28:48 +00:00
|
|
|
static const int MS_VC_EXCEPTION = 0x406D1388;
|
|
|
|
|
|
|
|
#pragma pack(push,8)
|
|
|
|
struct THREADNAME_INFO
|
|
|
|
{
|
|
|
|
DWORD dwType; // Must be 0x1000.
|
|
|
|
LPCSTR szName; // Pointer to name (in user addr space).
|
|
|
|
DWORD dwThreadID; // Thread ID (-1=caller thread).
|
|
|
|
DWORD dwFlags; // Reserved for future use, must be zero.
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
THREADNAME_INFO info;
|
2009-09-29 19:16:00 +00:00
|
|
|
info.dwType = 0x1000;
|
|
|
|
info.szName = name;
|
|
|
|
info.dwThreadID = GetCurrentThreadId();
|
|
|
|
info.dwFlags = 0;
|
2009-09-22 17:28:48 +00:00
|
|
|
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
|
|
|
|
}
|
|
|
|
__except(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-09-16 17:23:02 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// BaseTaskThread Implementations
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Tells the thread to exit and then waits for thread termination.
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
void BaseTaskThread::Block()
|
2009-09-16 17:23:02 +00:00
|
|
|
{
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
if( !IsRunning() ) return;
|
2009-09-16 17:23:02 +00:00
|
|
|
m_Done = true;
|
|
|
|
m_sem_event.Post();
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
PersistentThread::Block();
|
2009-09-16 17:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initiates the new task. This should be called after your own StartTask has
|
|
|
|
// initialized internal variables / preparations for task execution.
|
|
|
|
void BaseTaskThread::PostTask()
|
|
|
|
{
|
|
|
|
wxASSERT( !m_detached );
|
|
|
|
|
|
|
|
ScopedLock locker( m_lock_TaskComplete );
|
|
|
|
m_TaskPending = true;
|
|
|
|
m_post_TaskComplete.Reset();
|
|
|
|
m_sem_event.Post();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blocks current thread execution pending the completion of the parallel task.
|
|
|
|
void BaseTaskThread::WaitForResult()
|
|
|
|
{
|
|
|
|
if( m_detached || !m_running ) return;
|
|
|
|
if( m_TaskPending )
|
|
|
|
#ifdef wxUSE_GUI
|
|
|
|
m_post_TaskComplete.WaitGui();
|
|
|
|
#else
|
|
|
|
m_post_TaskComplete.Wait();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_post_TaskComplete.Reset();
|
|
|
|
}
|
|
|
|
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
void BaseTaskThread::ExecuteTask()
|
2009-09-16 17:23:02 +00:00
|
|
|
{
|
|
|
|
while( !m_Done )
|
|
|
|
{
|
|
|
|
// Wait for a job -- or get a pthread_cancel. I'm easy.
|
|
|
|
m_sem_event.Wait();
|
|
|
|
|
|
|
|
Task();
|
|
|
|
m_lock_TaskComplete.Lock();
|
|
|
|
m_TaskPending = false;
|
|
|
|
m_post_TaskComplete.Post();
|
|
|
|
m_lock_TaskComplete.Unlock();
|
|
|
|
};
|
|
|
|
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
return;
|
2009-09-16 17:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// pthread Cond is an evil api that is not suited for Pcsx2 needs.
|
|
|
|
// Let's not use it. (Air)
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
|
2009-02-24 02:08:37 +00:00
|
|
|
#if 0
|
2009-07-03 20:12:33 +00:00
|
|
|
WaitEvent::WaitEvent()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
int err = 0;
|
2009-07-03 20:12:33 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
err = pthread_cond_init(&cond, NULL);
|
|
|
|
err = pthread_mutex_init(&mutex, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
WaitEvent::~WaitEvent()
|
|
|
|
{
|
|
|
|
pthread_cond_destroy( &cond );
|
|
|
|
pthread_mutex_destroy( &mutex );
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitEvent::Set()
|
|
|
|
{
|
|
|
|
pthread_mutex_lock( &mutex );
|
|
|
|
pthread_cond_signal( &cond );
|
|
|
|
pthread_mutex_unlock( &mutex );
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitEvent::Wait()
|
|
|
|
{
|
|
|
|
pthread_mutex_lock( &mutex );
|
|
|
|
pthread_cond_wait( &cond, &mutex );
|
|
|
|
pthread_mutex_unlock( &mutex );
|
|
|
|
}
|
2009-02-24 02:08:37 +00:00
|
|
|
#endif
|
2009-02-28 20:55:53 +00:00
|
|
|
|
2009-09-16 17:23:02 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// Semaphore Implementations
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
Semaphore::Semaphore()
|
|
|
|
{
|
|
|
|
sem_init( &sema, false, 0 );
|
|
|
|
}
|
2009-07-03 20:12:33 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
Semaphore::~Semaphore()
|
|
|
|
{
|
|
|
|
sem_destroy( &sema );
|
|
|
|
}
|
|
|
|
|
2009-02-28 20:55:53 +00:00
|
|
|
void Semaphore::Reset()
|
|
|
|
{
|
|
|
|
sem_destroy( &sema );
|
|
|
|
sem_init( &sema, false, 0 );
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
void Semaphore::Post()
|
|
|
|
{
|
|
|
|
sem_post( &sema );
|
|
|
|
}
|
|
|
|
|
2009-08-25 15:38:48 +00:00
|
|
|
// Valid on Win32 builds only!! Attempts to use it on Linux will result in unresolved
|
|
|
|
// external linker errors.
|
2009-02-09 21:15:56 +00:00
|
|
|
void Semaphore::Post( int multiple )
|
|
|
|
{
|
2009-09-16 17:23:02 +00:00
|
|
|
#if defined(_MSC_VER)
|
2009-02-09 21:15:56 +00:00
|
|
|
sem_post_multiple( &sema, multiple );
|
2009-09-16 17:23:02 +00:00
|
|
|
#else
|
|
|
|
// Only w32pthreads has the post_multiple, but it's easy enough to fake:
|
|
|
|
while( multiple > 0 )
|
|
|
|
{
|
|
|
|
multiple--;
|
|
|
|
sem_post( &sema );
|
|
|
|
}
|
2009-08-25 15:38:48 +00:00
|
|
|
#endif
|
2009-09-16 17:23:02 +00:00
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-09-05 23:07:23 +00:00
|
|
|
#if wxUSE_GUI
|
|
|
|
// This is a wxApp-safe implementation of Wait, which makes sure and executes the App's
|
|
|
|
// pending messages *if* the Wait is performed on the Main/GUI thread. If the Wait is
|
|
|
|
// called from another thread, no message pumping is performed.
|
|
|
|
void Semaphore::WaitGui()
|
|
|
|
{
|
|
|
|
if( !wxThread::IsMain() || (wxTheApp == NULL) )
|
|
|
|
Wait();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// In order to avoid deadlock we need to make sure we cut some time
|
2009-09-09 14:08:15 +00:00
|
|
|
// to handle messages.
|
2009-09-05 23:07:23 +00:00
|
|
|
|
|
|
|
do {
|
2009-09-16 17:23:02 +00:00
|
|
|
wxTheApp->Yield();
|
2009-09-09 14:08:15 +00:00
|
|
|
} while( !Wait( ts_msec_250 ) );
|
2009-09-07 21:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Semaphore::WaitGui( const wxTimeSpan& timeout )
|
|
|
|
{
|
|
|
|
if( !wxThread::IsMain() || (wxTheApp == NULL) )
|
|
|
|
{
|
|
|
|
return Wait( timeout );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxTimeSpan countdown( (timeout) );
|
|
|
|
|
|
|
|
// In order to avoid deadlock we need to make sure we cut some time
|
2009-09-09 14:08:15 +00:00
|
|
|
// to handle messages.
|
2009-09-07 21:16:12 +00:00
|
|
|
|
|
|
|
do {
|
2009-09-16 17:23:02 +00:00
|
|
|
wxTheApp->Yield();
|
2009-09-09 14:08:15 +00:00
|
|
|
if( Wait( ts_msec_250 ) ) break;
|
|
|
|
countdown -= ts_msec_250;
|
2009-09-07 21:16:12 +00:00
|
|
|
} while( countdown.GetMilliseconds() > 0 );
|
|
|
|
|
|
|
|
return countdown.GetMilliseconds() > 0;
|
2009-09-05 23:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
void Semaphore::Wait()
|
|
|
|
{
|
|
|
|
sem_wait( &sema );
|
|
|
|
}
|
|
|
|
|
2009-09-07 21:16:12 +00:00
|
|
|
bool Semaphore::Wait( const wxTimeSpan& timeout )
|
2009-08-25 15:38:48 +00:00
|
|
|
{
|
2009-09-09 14:08:15 +00:00
|
|
|
wxDateTime megafail( wxDateTime::UNow() + timeout );
|
|
|
|
const timespec fail = { megafail.GetTicks(), megafail.GetMillisecond() * 1000000 };
|
2009-09-08 03:44:35 +00:00
|
|
|
return sem_timedwait( &sema, &fail ) != -1;
|
2009-08-25 15:38:48 +00:00
|
|
|
}
|
|
|
|
|
2009-08-15 06:17:43 +00:00
|
|
|
// Performs an uncancellable wait on a semaphore; restoring the thread's previous cancel state
|
|
|
|
// after the wait has completed. Useful for situations where the semaphore itself is stored on
|
|
|
|
// the stack and passed to another thread via GUI message or such, avoiding complications where
|
2009-08-25 15:38:48 +00:00
|
|
|
// the thread might be canceled and the stack value becomes invalid.
|
2009-08-15 06:17:43 +00:00
|
|
|
//
|
|
|
|
// Performance note: this function has quite a bit more overhead compared to Semaphore::Wait(), so
|
|
|
|
// consider manually specifying the thread as uncancellable and using Wait() instead if you need
|
|
|
|
// to do a lot of no-cancel waits in a tight loop worker thread, for example.
|
|
|
|
void Semaphore::WaitNoCancel()
|
|
|
|
{
|
|
|
|
int oldstate;
|
|
|
|
pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, &oldstate );
|
|
|
|
Wait();
|
|
|
|
pthread_setcancelstate( oldstate, NULL );
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
int Semaphore::Count()
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
sem_getvalue( &sema, &retval );
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2009-09-16 17:23:02 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// MutexLock Implementations
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
MutexLock::MutexLock()
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
err = pthread_mutex_init( &mutex, NULL );
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:05:48 +00:00
|
|
|
MutexLock::MutexLock( bool isRecursive )
|
|
|
|
{
|
|
|
|
if( isRecursive )
|
|
|
|
{
|
2009-08-20 23:05:26 +00:00
|
|
|
pthread_mutexattr_t mutexAttribute;
|
2009-07-03 06:05:48 +00:00
|
|
|
int status = pthread_mutexattr_init( &mutexAttribute );
|
2009-08-20 23:05:26 +00:00
|
|
|
if (status != 0) { /* ... */ }
|
|
|
|
status = pthread_mutexattr_settype( &mutexAttribute, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
if (status != 0) { /* ... */}
|
2009-07-03 06:05:48 +00:00
|
|
|
|
|
|
|
int err = 0;
|
|
|
|
err = pthread_mutex_init( &mutex, &mutexAttribute );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
err = pthread_mutex_init( &mutex, NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
MutexLock::~MutexLock()
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy( &mutex );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MutexLock::Lock()
|
|
|
|
{
|
|
|
|
pthread_mutex_lock( &mutex );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MutexLock::Unlock()
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock( &mutex );
|
|
|
|
}
|
|
|
|
|
2009-09-29 19:16:00 +00:00
|
|
|
bool MutexLock::TryLock()
|
|
|
|
{
|
|
|
|
return EBUSY != pthread_mutex_trylock( &mutex );
|
|
|
|
}
|
|
|
|
|
2009-09-16 17:23:02 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// InterlockedExchanges / AtomicExchanges (PCSX2's Helper versions)
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// define some overloads for InterlockedExchanges for commonly used types, like u32 and s32.
|
2009-07-03 20:12:33 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
__forceinline void AtomicExchange( volatile u32& Target, u32 value )
|
|
|
|
{
|
2009-09-16 17:23:02 +00:00
|
|
|
_InterlockedExchange( (volatile long*)&Target, value );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void AtomicExchangeAdd( volatile u32& Target, u32 value )
|
|
|
|
{
|
2009-09-16 17:23:02 +00:00
|
|
|
_InterlockedExchangeAdd( (volatile long*)&Target, value );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void AtomicIncrement( volatile u32& Target )
|
|
|
|
{
|
2009-09-16 17:23:02 +00:00
|
|
|
_InterlockedExchangeAdd( (volatile long*)&Target, 1 );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void AtomicDecrement( volatile u32& Target )
|
|
|
|
{
|
2009-09-16 17:23:02 +00:00
|
|
|
_InterlockedExchangeAdd( (volatile long*)&Target, -1 );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void AtomicExchange( volatile s32& Target, s32 value )
|
|
|
|
{
|
2009-09-16 17:23:02 +00:00
|
|
|
_InterlockedExchange( (volatile long*)&Target, value );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void AtomicExchangeAdd( s32& Target, u32 value )
|
|
|
|
{
|
2009-09-16 17:23:02 +00:00
|
|
|
_InterlockedExchangeAdd( (volatile long*)&Target, value );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void AtomicIncrement( volatile s32& Target )
|
|
|
|
{
|
2009-09-16 17:23:02 +00:00
|
|
|
_InterlockedExchangeAdd( (volatile long*)&Target, 1 );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void AtomicDecrement( volatile s32& Target )
|
|
|
|
{
|
2009-09-16 17:23:02 +00:00
|
|
|
_InterlockedExchangeAdd( (volatile long*)&Target, -1 );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void _AtomicExchangePointer( const void ** target, const void* value )
|
|
|
|
{
|
2009-09-16 17:23:02 +00:00
|
|
|
_InterlockedExchange( (volatile long*)target, (long)value );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void _AtomicCompareExchangePointer( const void ** target, const void* value, const void* comparand )
|
|
|
|
{
|
2009-09-16 17:23:02 +00:00
|
|
|
_InterlockedCompareExchange( (volatile long*)target, (long)value, (long)comparand );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
2009-07-03 20:12:33 +00:00
|
|
|
}
|