2009-09-08 12:08:10 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2009-10-05 11:05:11 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* 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-10-05 11:05:11 +00:00
|
|
|
|
2021-09-03 10:43:33 +00:00
|
|
|
#if !defined(_WIN32) && !defined(__APPLE__)
|
2012-03-17 11:21:51 +00:00
|
|
|
#include <unistd.h>
|
2016-05-08 20:17:19 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#elif defined(__unix__)
|
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
2010-05-12 11:25:30 +00:00
|
|
|
|
2021-09-01 20:31:46 +00:00
|
|
|
#include "common/PersistentThread.h"
|
|
|
|
|
2010-05-12 11:25:30 +00:00
|
|
|
// We wont need this until we actually have this more then just stubbed out, so I'm commenting this out
|
|
|
|
// to remove an unneeded dependency.
|
|
|
|
//#include "x86emitter/tools.h"
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2016-05-08 20:17:19 +00:00
|
|
|
#if !defined(__unix__)
|
2009-11-26 03:37:10 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
#pragma message("LnxThreads.cpp should only be compiled by projects or makefiles targeted at Linux/BSD distros.")
|
2009-11-26 03:37:10 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
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.
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
__forceinline void Threading::Sleep(int ms)
|
2009-10-19 23:13:22 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
usleep(1000 * ms);
|
2009-10-19 23:13:22 +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 Threading::SpinWait()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
// 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-10-19 23:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void Threading::EnableHiresScheduler()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
// Don't know if linux has a customizable scheduler resolution like Windows (doubtful)
|
2009-10-19 23:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__forceinline void Threading::DisableHiresScheduler()
|
|
|
|
{
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
2009-11-26 03:37:10 +00:00
|
|
|
|
2014-09-13 15:25:22 +00:00
|
|
|
// Unit of time of GetThreadCpuTime/GetCpuTime
|
2010-01-25 15:31:17 +00:00
|
|
|
u64 Threading::GetThreadTicksPerSecond()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return 1000000;
|
2010-01-25 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 15:25:22 +00:00
|
|
|
// Helper function to get either either the current cpu usage
|
|
|
|
// in called thread or in id thread
|
|
|
|
static u64 get_thread_time(uptr id = 0)
|
2010-01-25 15:31:17 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
clockid_t cid;
|
|
|
|
if (id)
|
|
|
|
{
|
|
|
|
int err = pthread_getcpuclockid((pthread_t)id, &cid);
|
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cid = CLOCK_THREAD_CPUTIME_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timespec ts;
|
|
|
|
int err = clock_gettime(cid, &ts);
|
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (u64)ts.tv_sec * (u64)1e6 + (u64)ts.tv_nsec / (u64)1e3;
|
2014-09-13 15:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the current timestamp (not relative to a real world clock)
|
|
|
|
u64 Threading::GetThreadCpuTime()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return get_thread_time();
|
2010-01-25 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 14:39:07 +00:00
|
|
|
u64 Threading::pxThread::GetCpuTime() const
|
2010-01-25 15:31:17 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
// Get the cpu time for the thread belonging to this object. Use m_native_id and/or
|
|
|
|
// m_native_handle to implement it. Return value should be a measure of total time the
|
|
|
|
// thread has used on the CPU (scaled by the value returned by GetThreadTicksPerSecond(),
|
|
|
|
// which typically would be an OS-provided scalar or some sort).
|
2010-01-25 15:31:17 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
if (!m_native_id)
|
|
|
|
return 0;
|
2011-09-04 21:27:11 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
return get_thread_time(m_native_id);
|
2010-01-25 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 14:39:07 +00:00
|
|
|
void Threading::pxThread::_platform_specific_OnStartInThread()
|
2010-01-25 15:31:17 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
// Obtain linux-specific thread IDs or Handles here, which can be used to query
|
|
|
|
// kernel scheduler performance information.
|
|
|
|
m_native_id = (uptr)pthread_self();
|
2010-01-25 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 14:39:07 +00:00
|
|
|
void Threading::pxThread::_platform_specific_OnCleanupInThread()
|
2010-01-25 15:31:17 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
// Cleanup handles here, which were opened above.
|
2010-01-25 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
void Threading::SetNameOfCurrentThread(const char* name)
|
2010-01-25 15:31:17 +00:00
|
|
|
{
|
2016-05-08 20:17:19 +00:00
|
|
|
#if defined(__linux__)
|
2021-09-06 18:28:26 +00:00
|
|
|
// Extract of manpage: "The name can be up to 16 bytes long, and should be
|
|
|
|
// null-terminated if it contains fewer bytes."
|
|
|
|
prctl(PR_SET_NAME, name, 0, 0, 0);
|
2016-05-08 20:17:19 +00:00
|
|
|
#elif defined(__unix__)
|
2021-09-06 18:28:26 +00:00
|
|
|
pthread_set_name_np(pthread_self(), name);
|
2016-05-08 20:17:19 +00:00
|
|
|
#endif
|
2010-01-25 15:31:17 +00:00
|
|
|
}
|
|
|
|
|
2009-11-26 03:37:10 +00:00
|
|
|
#endif
|
2021-09-01 20:31:46 +00:00
|
|
|
#endif
|