Skip to content

Commit

Permalink
Replace SK_Thread_HybridSpinlock with an SRWLock wherever possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaldaien committed Dec 29, 2024
1 parent 76b5507 commit 61ca40d
Show file tree
Hide file tree
Showing 27 changed files with 248 additions and 88 deletions.
6 changes: 3 additions & 3 deletions include/SpecialK/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
#include <SpecialK/window.h>
#include <SpecialK/core.h>

#define SK_NoPreference -1
#define SK_Disabled 0
#define SK_Enabled 1
static constexpr int SK_NoPreference = -1;
static constexpr int SK_Disabled = 0;
static constexpr int SK_Enabled = 1;

struct SK_Keybind
{
Expand Down
2 changes: 1 addition & 1 deletion include/SpecialK/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ bool SK_API_IsLayeredOnD3D11 (SK_RenderAPI api);


#ifdef SK_BUILD_DLL
class SK_Thread_HybridSpinlock;


extern "C++" SK_Thread_HybridSpinlock* init_mutex;
extern "C++" SK_Thread_HybridSpinlock* budget_mutex;
Expand Down
2 changes: 2 additions & 0 deletions include/SpecialK/injection/injection.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,7 @@ float SK_Inject_GetInjectionDelayInSeconds (void);
void SK_Inject_SetFocusWindow (HWND hWndFocus);
HWND SK_Inject_GetFocusWindow (void);

BOOL NTAPI RtlpWaitCouldDeadlock (void);


#endif /* __SK__INJECTION_H__ */
12 changes: 6 additions & 6 deletions include/SpecialK/render/d3d11/d3d11_state_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ struct memory_tracking_s
//constant_buffers.reserve (2048);
//}

void clear (SK_Thread_CriticalSection* /*cs*/)
void clear (SK_Thread_HybridSpinlock* /*cs*/)
{
///std::scoped_lock <SK_Thread_CriticalSection> auto_lock (*cs);
///std::scoped_lock <SK_Thread_HybridSpinlock> auto_lock (*cs);

for (int i = 0; i < __types; i++)
{
Expand Down Expand Up @@ -341,7 +341,7 @@ struct SK_D3D11_KnownThreads
SK_D3D11_KnownThreads (void) noexcept (false)///
{
_lock =
std::make_unique <SK_Thread_HybridSpinlock> (0x400);
std::make_unique <SK_Thread_HybridSpinlock> (/*0x400*/);

//ids.reserve (16);
//active.reserve (16);
Expand Down Expand Up @@ -417,7 +417,7 @@ class SK_D3D11_IsShaderLoaded

static
std::map < std::type_index,
std::pair < SK_Thread_CriticalSection*,
std::pair < SK_Thread_HybridSpinlock*,
SK_D3D11_KnownShaders::ShaderRegistry <IUnknown>*
>
>
Expand Down Expand Up @@ -815,7 +815,7 @@ SK_D3D11_CreateShader_Impl (
S_OK;

const auto GetResources =
[&]( gsl::not_null <SK_Thread_CriticalSection**> ppCritical,
[&]( gsl::not_null <SK_Thread_HybridSpinlock**> ppCritical,
gsl::not_null <SK_D3D11_KnownShaders::ShaderRegistry <IUnknown>**> ppShaderDomain ) noexcept
{
auto& shaders =
Expand Down Expand Up @@ -869,7 +869,7 @@ SK_D3D11_CreateShader_Impl (
}
};

SK_Thread_CriticalSection* pCritical = nullptr;
SK_Thread_HybridSpinlock* pCritical = nullptr;
SK_D3D11_KnownShaders::ShaderRegistry <IUnknown>* pShaderRepo = nullptr;

GetResources (
Expand Down
2 changes: 1 addition & 1 deletion include/SpecialK/render/d3d11/d3d11_tex_mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ struct lod_hash_table_s
lod_hash_table_s (void) noexcept (false)
{
mutex =
std::make_shared <SK_Thread_HybridSpinlock> (120);
std::make_shared <SK_Thread_HybridSpinlock> (/*120*/);
}

lod_hash_table_s (const lod_hash_table_s &) = default;
Expand Down
6 changes: 3 additions & 3 deletions include/SpecialK/render/d3d9/d3d9_texmgr.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#pragma once

#include <Windows.h>

Expand Down Expand Up @@ -37,7 +37,7 @@ class SK_ThreadSafe_HashSet
public:
SK_ThreadSafe_HashSet (void) {
lock_ =
std::make_unique <SK_Thread_HybridSpinlock> (0x100);
std::make_unique <SK_Thread_HybridSpinlock> (/*0x100*/);
}

~SK_ThreadSafe_HashSet (void) = default;
Expand Down Expand Up @@ -693,7 +693,7 @@ struct TexThreadStats {
//SK_Thread_HybridSpinlock cs_jobs_ =
// SK_Thread_HybridSpinlock ( 100UL);
SK_Thread_HybridSpinlock cs_results_ =
SK_Thread_HybridSpinlock (1000UL);
SK_Thread_HybridSpinlock (/*1000UL*/);

HANDLE spool_thread_;

Expand Down
5 changes: 5 additions & 0 deletions include/SpecialK/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class SK_Thread_CriticalSection //: public std::recursive_mutex
CRITICAL_SECTION* cs_;
};

#if 0
class SK_Thread_HybridSpinlock : public SK_Thread_CriticalSection
{
public:
Expand All @@ -186,6 +187,10 @@ class SK_Thread_HybridSpinlock : public SK_Thread_CriticalSection
private:
CRITICAL_SECTION impl_cs_ = { };
};
#else
#include <mutex>
using SK_Thread_HybridSpinlock = std::recursive_mutex;
#endif


DWORD
Expand Down
18 changes: 9 additions & 9 deletions include/imgui/imgui_user.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2948,7 +2948,6 @@ ImGui::PlotLinesC ( const char* label, const float* values,
}


#include <SpecialK/control_panel.h>
#include <SpecialK/render/dxgi/dxgi_backend.h>

static int64_t g_Time = { };
Expand Down Expand Up @@ -3222,8 +3221,9 @@ SK_ImGui_UpdateMouseButtons (bool bActive, ImGuiIO& io)
return false;
}

POINT SK_ImGui_LastKnownCursorPos;
HCURSOR SK_ImGui_LastKnownCursor;
ULONG64 SK_ImGui_LastKeyboardInputFrame = 0 ;
POINT SK_ImGui_LastKnownCursorPos = { };
HCURSOR SK_ImGui_LastKnownCursor = 0 ;

void
SK_ImGui_UpdateClassCursor (void)
Expand Down Expand Up @@ -3481,7 +3481,7 @@ SK_ImGui_User_NewFrame (void)
bool new_input =
( !GetLastInputInfo (&cii) ||
std::exchange ( lii.dwTime,
cii.dwTime ) != cii.dwTime );
cii.dwTime ) != cii.dwTime ) || SK_ImGui_LastKeyboardInputFrame >= SK_GetFramesDrawn () - 1;


if ((! game_window.mouse.can_track) || game_window.mouse.last_move_msg < SK::ControlPanel::current_time - _IdleCursorTimeout) // No Hover / Leave Tracking
Expand Down Expand Up @@ -3578,17 +3578,17 @@ SK_ImGui_User_NewFrame (void)
SK_IsGameWindowActive (false, hWndForeground);

// Avoid overhead from calling SK_GetAsyncKeyState (...)
// repeatedly; we already have a low-level keyboard hook!
#ifdef SK_DOES_NOT_TRUST_LOW_LEVEL_KEYBOARD_HOOK
if (bActive && new_input)
{
// repeatedly; we already have a keyboard hook!
//#ifdef SK_DOES_NOT_TRUST_LOW_LEVEL_KEYBOARD_HOOK
if (bActive && new_input && SK_ImGui_LastKeyboardInputFrame < SK_GetFramesDrawn () - 5)
{ // ^^^^ The last frame we saw any Keyboard input on the keyboard hook.
for (UINT i = 7 ; i < 255 ; ++i)
{
io.KeysDown [i] =
((SK_GetAsyncKeyState (i) & 0x8000) != 0x0);
}
}
#endif
//#endif

if (! bActive)
RtlZeroMemory (&io.KeysDown [7], sizeof (bool) * 248);
Expand Down
14 changes: 7 additions & 7 deletions src/SpecialK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1629,20 +1629,20 @@ SK_Attach (DLL_ROLE role)
void
{
cs_dbghelp =
new SK_Thread_HybridSpinlock (2048UL);
new SK_Thread_HybridSpinlock (/*2048UL*/);
budget_mutex =
new SK_Thread_HybridSpinlock ( 100UL);
new SK_Thread_HybridSpinlock (/* 100UL*/);
init_mutex =
new SK_Thread_HybridSpinlock ( 768UL);
new SK_Thread_HybridSpinlock (/* 768UL*/);
wmi_cs =
new SK_Thread_HybridSpinlock ( 128UL);
new SK_Thread_HybridSpinlock (/* 128UL*/);

steam_callback_cs =
new SK_Thread_HybridSpinlock ( 256UL);
new SK_Thread_HybridSpinlock (/* 256UL*/);
platform_popup_cs =
new SK_Thread_HybridSpinlock ( 512UL);
new SK_Thread_HybridSpinlock (/* 512UL*/);
steam_init_cs =
new SK_Thread_HybridSpinlock ( 128UL);
new SK_Thread_HybridSpinlock (/* 128UL*/);
};

_InitMutexes ();
Expand Down
6 changes: 3 additions & 3 deletions src/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ class SK_SourceCmd : public SK_ICommand
SK_ICommandProcessor::SK_ICommandProcessor (void)
{
process_cmd_lock =
std::make_unique <SK_Thread_HybridSpinlock> (1024);
std::make_unique <SK_Thread_HybridSpinlock> (/*1024*/);

add_remove_cmd_lock =
std::make_unique <SK_Thread_HybridSpinlock> (128);
std::make_unique <SK_Thread_HybridSpinlock> (/*128*/);

add_remove_var_lock =
std::make_unique <SK_Thread_HybridSpinlock> (128);
std::make_unique <SK_Thread_HybridSpinlock> (/*128*/);

AddCommand ("source", new SK_SourceCmd (this));
}
Expand Down
9 changes: 7 additions & 2 deletions src/control_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7440,6 +7440,8 @@ SK_ImGui_MouseProc (int code, WPARAM wParam, LPARAM lParam)
CallNextHookEx (0, code, wParam, lParam);
}

extern ULONG64 SK_ImGui_LastKeyboardInputFrame;

LRESULT
CALLBACK
SK_ImGui_KeyboardProc (int code, WPARAM wParam, LPARAM lParam)
Expand All @@ -7460,7 +7462,10 @@ SK_ImGui_KeyboardProc (int code, WPARAM wParam, LPARAM lParam)
auto& io =
ImGui::GetIO ();

io.KeysDown [vKey] = isPressed;
if ( vKey > 7) {
io.KeysDown [vKey] = isPressed;
SK_ImGui_LastKeyboardInputFrame = SK_GetFramesDrawn ();
}

if (io.KeyAlt && vKey == VK_F4 && isPressed)
{
Expand Down Expand Up @@ -7598,7 +7603,7 @@ SK_ImGui_StageNextFrame (void)

reset_frame_history = false;

if (config.render.framerate.latent_sync.show_fcat_bars)
if (config.render.framerate.latent_sync.show_fcat_bars)
{
SK_ImGui_DrawFCAT ();
}
Expand Down
9 changes: 9 additions & 0 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2887,6 +2887,15 @@ bool
__stdcall
SK_ShutdownCore (const wchar_t* backend)
{
const auto
bNoCanWait =
RtlpWaitCouldDeadlock ();

if (bNoCanWait)
{
//SK_LOGi0 (L"Wait Could Deadlock!");
}

SK_Inject_BroadcastExitNotify ();
SK_DisableApplyQueuedHooks ();

Expand Down
37 changes: 36 additions & 1 deletion src/injection/injection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ NtUserUnhookWindowsHookEx_pfn NtUserUnhookWindowsHookEx = nullptr;

HMODULE hModHookInstance = nullptr;

using RtlDllShutdownInProgress_pfn = BOOL (NTAPI *)(void);
static RtlDllShutdownInProgress_pfn
RtlDllShutdownInProgress = nullptr;

BOOL
NTAPI
RtlpWaitCouldDeadlock (void)
{
if (RtlDllShutdownInProgress == nullptr)
RtlDllShutdownInProgress =
(RtlDllShutdownInProgress_pfn)GetProcAddress (
GetModuleHandleW (L"ntdll.dll"),
"RtlDllShutdownInProgress");

if (! RtlDllShutdownInProgress)
return TRUE;

return
RtlDllShutdownInProgress () != FALSE;
}

// It's not possible to store a structure in the shared data segment.
//
// This struct will be filled-in when SK boots up using the loose mess of
Expand Down Expand Up @@ -1237,6 +1258,9 @@ SK_IsImmersiveProcess (HANDLE hProcess = SK_GetCurrentProcess ())
void
SK_Inject_SpawnUnloadListener (void)
{
if (RtlpWaitCouldDeadlock ())
return;

//if (SK_GetHostAppUtil ()->isBlacklisted ())
//{
// return;
Expand Down Expand Up @@ -1326,13 +1350,24 @@ SK_Inject_SpawnUnloadListener (void)
}
}, nullptr, &app_state_cookie);

const DWORD dwTimeout = INFINITE;
const DWORD dwTimeout = 1500UL;
const DWORD dwWaitState =
WaitForMultipleObjects ( sizeof (signals) /
sizeof (signals [0]),
signals, FALSE,
dwTimeout );

if (RtlpWaitCouldDeadlock ())
{
HMODULE this_module = (HMODULE)
ReadPointerAcquire ((void **)&g_hModule_CBT);
InterlockedExchangePointer ((void **)&g_hModule_CBT, nullptr);
SK_FreeLibraryAndExitThread (this_module, 0x0);
InterlockedDecrement (&injected_procs);

return 0;
}

// Is Process Actively Using Special K (?)
if ( dwWaitState == WAIT_OBJECT_0+1 &&
hModHookInstance != nullptr )
Expand Down
4 changes: 2 additions & 2 deletions src/input/hid_reports/playstation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ SK_HID_PlayStationDevice::SK_HID_PlayStationDevice (HANDLE file)
setPollingFrequency (0);
setBufferCount (config.input.gamepad.hid.max_allowed_buffers);

latency.pollrate = std::make_shared <SK::Framerate::Stats> ( );
xinput.lock_report = std::make_shared <SK_Thread_HybridSpinlock> (0x400);
latency.pollrate = std::make_shared <SK::Framerate::Stats> ( );
xinput.lock_report = std::make_shared <SK_Thread_HybridSpinlock> (/*0x400*/);
}

SK_HID_PlayStationDevice::~SK_HID_PlayStationDevice (void)
Expand Down
2 changes: 1 addition & 1 deletion src/osd/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std::unique_ptr <SK_Thread_HybridSpinlock> SK_TextOverlayManager::lock_ = nullpt
SK_TextOverlayManager::SK_TextOverlayManager (void)
{
lock_ =
std::make_unique <SK_Thread_HybridSpinlock> (104858);
std::make_unique <SK_Thread_HybridSpinlock> (/*104858*/);

need_full_reset_ = false;

Expand Down
Loading

0 comments on commit 61ca40d

Please sign in to comment.