From 16abbb62dab187ac46b5932142a25e3065eafda5 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 28 Mar 2004 07:17:13 +0000 Subject: [PATCH] implement ArchHooks_darwin::EnterTimeCriticalSection, ArchHooks_darwin::ExitTimeCriticalSection --- .../src/arch/ArchHooks/ArchHooks_darwin.cpp | 61 +++++++++++++++++++ .../src/arch/ArchHooks/ArchHooks_darwin.h | 9 ++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/stepmania/src/arch/ArchHooks/ArchHooks_darwin.cpp b/stepmania/src/arch/ArchHooks/ArchHooks_darwin.cpp index 26074554d5..2db0e02663 100644 --- a/stepmania/src/arch/ArchHooks/ArchHooks_darwin.cpp +++ b/stepmania/src/arch/ArchHooks/ArchHooks_darwin.cpp @@ -10,11 +10,19 @@ #include "global.h" #include "ArchHooks_darwin.h" #include "RageLog.h" +#include "RageThreads.h" +#include "RageUtil.h" +#include "RageTimer.h" #include "archutils/Darwin/Crash.h" #include "archutils/Unix/CrashHandler.h" #include "StepMania.h" +#define Random Random_ // work around namespace pollution #include +#undef Random_ +#include #include +#include +#include /* You would think that these would be defined somewhere. */ enum @@ -23,6 +31,9 @@ enum kMacOSX_10_3 = 0x1030 }; +static thread_time_constraint_policy g_oldttcpolicy; +static float g_fStartedTimeCritAt; + SInt16 ShowAlert(int type, CFStringRef message, CFStringRef OK, CFStringRef cancel = NULL) { struct AlertStdCFStringAlertParamRec params = {kStdCFStringAlertVersionOne, true, false, OK, cancel, NULL, @@ -43,6 +54,12 @@ ArchHooks_darwin::ArchHooks_darwin() { CrashHandlerHandleArgs(g_argc, g_argv); InstallExceptionHandler(CrashExceptionHandler); + TimeCritMutex = new RageMutex("TimeCritMutex"); +} + +ArchHooks_darwin::~ArchHooks_darwin() +{ + delete TimeCritMutex; } #define CASE_GESTALT_M(str,code,result) case gestalt##code: str = result; break @@ -255,3 +272,47 @@ ArchHooks::MessageBoxResult ArchHooks_darwin::MessageBoxAbortRetryIgnorePrivate( return ret; } + +void ArchHooks_darwin::EnterTimeCriticalSection() +{ + TimeCritMutex->Lock(); + + int mib[] = { CTL_HW, HW_BUS_FREQ }; + int miblen = ARRAYSIZE( mib ); + int bus_speed; + size_t len = sizeof (bus_speed); + if( sysctl( mib, miblen, &bus_speed, &len, NULL, 0 ) == -1 ) + { + LOG->Warn( "sysctl(HW_BUS_FREQ): %s", strerror(errno) ); + return; + } + + mach_msg_type_number_t cnt = THREAD_TIME_CONSTRAINT_POLICY_COUNT; + boolean_t bDefaults = false; + thread_policy_get( mach_thread_self(), THREAD_TIME_CONSTRAINT_POLICY, (int*)&g_oldttcpolicy, &cnt, &bDefaults ); + + /* We want to monopolize the CPU for a very short period of time. This means that the + * period doesn't really matter, and we don't want to be preempted. Set the period + * very high (~1 second), so that if we ever lose the CPU when we shouldn't, we can + * detect it and log it in ExitTimeCriticalSection(). */ + thread_time_constraint_policy ttcpolicy; + ttcpolicy.period = bus_speed; + ttcpolicy.computation = ttcpolicy.constraint = bus_speed/60; + ttcpolicy.preemptible = 0; + thread_policy_set( mach_thread_self(), THREAD_TIME_CONSTRAINT_POLICY, + (int*)&ttcpolicy, THREAD_TIME_CONSTRAINT_POLICY_COUNT ); + + g_fStartedTimeCritAt = RageTimer::GetTimeSinceStart(); +} + +void ArchHooks_darwin::ExitTimeCriticalSection() +{ + thread_policy_set( mach_thread_self(), THREAD_TIME_CONSTRAINT_POLICY, + (int*) &g_oldttcpolicy, THREAD_TIME_CONSTRAINT_POLICY_COUNT ); + TimeCritMutex->Unlock(); + + float fTimeCritLen = RageTimer::GetTimeSinceStart() - g_fStartedTimeCritAt; + if( fTimeCritLen > 0.1f ) + LOG->Warn( "Time-critical section lasted for %f", fTimeCritLen ); +} + diff --git a/stepmania/src/arch/ArchHooks/ArchHooks_darwin.h b/stepmania/src/arch/ArchHooks/ArchHooks_darwin.h index 77b0eb39d0..726dec64fd 100644 --- a/stepmania/src/arch/ArchHooks/ArchHooks_darwin.h +++ b/stepmania/src/arch/ArchHooks/ArchHooks_darwin.h @@ -10,14 +10,21 @@ */ #include "ArchHooks.h" +class RageMutex; class ArchHooks_darwin : public ArchHooks { public: ArchHooks_darwin(); - ~ArchHooks_darwin() { } + ~ArchHooks_darwin(); void DumpDebugInfo(); + + void EnterTimeCriticalSection(); + void ExitTimeCriticalSection(); + protected: + RageMutex *TimeCritMutex; + void MessageBoxErrorPrivate(CString sError, CString ID); void MessageBoxOKPrivate(CString sMessage, CString ID); MessageBoxResult MessageBoxAbortRetryIgnorePrivate(CString sMessage, CString ID);