2009-02-09 21:15:56 +00:00
|
|
|
/* Pcsx2 - Pc Ps2 Emulator
|
2009-02-15 23:23:46 +00:00
|
|
|
* Copyright (C) 2002-2009 Pcsx2 Team
|
2009-02-09 21:15:56 +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; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2009-04-28 05:56:22 +00:00
|
|
|
#include "PrecompiledHeader.h"
|
2009-07-03 00:49:40 +00:00
|
|
|
#include "Threading.h"
|
2009-07-03 20:12:33 +00:00
|
|
|
#include "x86emitter/tools.h"
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
// Note: assuming multicore is safer because it forces the interlocked routines to use
|
|
|
|
// the LOCK prefix. The prefix works on single core CPUs fine (but is slow), but not
|
|
|
|
// having the LOCK prefix is very bad indeed.
|
|
|
|
|
|
|
|
static bool isMultiCore = true; // assume more than one CPU (safer)
|
|
|
|
|
|
|
|
namespace Threading
|
|
|
|
{
|
|
|
|
// Note: Apparently this solution is Linux/Solaris only.
|
|
|
|
// FreeBSD/OsX need something far more complicated (apparently)
|
|
|
|
void CountLogicalCores( int LogicalCoresPerPhysicalCPU, int PhysicalCoresPerPhysicalCPU )
|
|
|
|
{
|
|
|
|
const uint numCPU = sysconf( _SC_NPROCESSORS_ONLN );
|
|
|
|
if( numCPU > 0 )
|
|
|
|
{
|
|
|
|
isMultiCore = numCPU > 1;
|
|
|
|
cpuinfo.LogicalCores = numCPU;
|
|
|
|
cpuinfo.PhysicalCores = ( numCPU / LogicalCoresPerPhysicalCPU ) * PhysicalCoresPerPhysicalCPU;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Indeterminate?
|
|
|
|
cpuinfo.LogicalCores = 1;
|
|
|
|
cpuinfo.PhysicalCores = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void Timeslice()
|
|
|
|
{
|
|
|
|
usleep(500);
|
|
|
|
}
|
|
|
|
|
2009-02-28 20:55:53 +00:00
|
|
|
__forceinline void Sleep( int ms )
|
|
|
|
{
|
|
|
|
usleep( 1000*ms );
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
// For use in spin/wait loops, Acts as a hint to Intel CPUs and should, in theory
|
|
|
|
// improve performance and reduce cpu power consumption.
|
|
|
|
__forceinline void SpinWait()
|
|
|
|
{
|
|
|
|
// If this doesn't compile you can just comment it out (it only serves as a
|
|
|
|
// performance hint and isn't required).
|
|
|
|
__asm__ ( "pause" );
|
|
|
|
}
|
2009-04-28 05:56:22 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
void* Thread::_internal_callback( void* itsme )
|
|
|
|
{
|
|
|
|
jASSUME( itsme != NULL );
|
|
|
|
|
2009-02-10 11:34:35 +00:00
|
|
|
Thread& owner = *((Thread*)itsme);
|
|
|
|
owner.m_returncode = owner.Callback();
|
2009-02-09 21:15:56 +00:00
|
|
|
owner.m_terminated = true;
|
2009-02-10 11:34:35 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|