diff --git a/src/co/hook.h b/src/co/hook.h index c0cd597aa..ff771ec6f 100644 --- a/src/co/hook.h +++ b/src/co/hook.h @@ -21,6 +21,13 @@ void hook_sleep(bool x); #define __sys_api(x) _sys_##x #define _CO_DEC_SYS_API(x) extern x##_fp_t __sys_api(x) +struct HookInitializer { + HookInitializer(); + ~HookInitializer(); +}; + +static HookInitializer g_hook_initializer; + #ifdef _WIN32 #include #include // for inet_ntop... @@ -116,13 +123,6 @@ _CO_DEC_SYS_API(GetQueuedCompletionStatusEx); #define _CO_DEF_SYS_API(x) x##_fp_t __sys_api(x) = 0 -struct HookInitializer { - HookInitializer(); - ~HookInitializer(); -}; - -static HookInitializer g_hook_initializer; - namespace co { // deduce type of the second parameter of ioctl diff --git a/src/co/hook_win.cc b/src/co/hook_win.cc index 9f7b35a28..2fd28f31a 100644 --- a/src/co/hook_win.cc +++ b/src/co/hook_win.cc @@ -1375,7 +1375,6 @@ inline void detour_detach(PVOID* ppbReal, PVOID pbMine, PCHAR psz) { // it will be called at initialization of coroutine schedulers void init_hook() { - g_hook = co::_make_static(); __sys_api(WSARecvMsg) = get_WSARecvMsg_fp(); __sys_api(WSASendMsg) = get_WSASendMsg_fp(); @@ -1458,6 +1457,15 @@ void hook_sleep(bool x) { } // co +static int g_nifty_counter; +HookInitializer::HookInitializer() { + if (g_nifty_counter++ == 0) { + g_hook = co::_make_static(); + } +} + +HookInitializer::~HookInitializer() {} + #undef attach_hook #undef detach_hook #undef do_hard_hook