Skip to content

Commit 5e2c960

Browse files
Opt, 新增__YY_Thunks_Disable_Rreload_Dlls弱符号,控制预加载状态
1 parent b0fea8b commit 5e2c960

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

src/Build.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ goto:eof
140140

141141
:: FixObj "XXX\YY_Thunks_for_Vista.obj" 1.def+2.def
142142
:FixObj
143-
LibMaker.exe FixObj %1 /WeakExternFix:__security_cookie=%PointType% /WeakExternFix:__acrt_atexit_table=%PointType% /WeakExternFix:__pfnDllMainCRTStartupForYY_Thunks=%PointType%
143+
LibMaker.exe FixObj %1 /WeakExternFix:__security_cookie=%PointType% /WeakExternFix:__acrt_atexit_table=%PointType% /WeakExternFix:__pfnDllMainCRTStartupForYY_Thunks=%PointType% /WeakExternFix:__YY_Thunks_Disable_Rreload_Dlls=4
144144
if %ErrorLevel% NEQ 0 exit /b %ErrorLevel%
145145
if "%2"=="" goto:eof
146146
set DEF_FILES=%2

src/Thunks/YY_Thunks.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ else
104104
*/
105105
EXTERN_C const UINT64 __YY_Thunks_Installed = YY_Thunks_Target;
106106

107+
/*
108+
导出一个外部弱符号,指示当前是否关闭DLL预载(默认开启DLL预载)。
109+
警告:关闭DLL虽然可以提升初始化速度,但是这可能带来死锁问题。目前已知的有:
110+
1. locale/DllMain交叉持锁产生死锁(https://github.com/Chuyu-Team/YY-Thunks/issues/7)
111+
2. thread safe/DllMain交叉持锁产生死锁
112+
113+
// 如果需要关闭DLL预载,那么再任意位置定义如下变量即可:
114+
EXTERN_C const BOOL __YY_Thunks_Disable_Rreload_Dlls = TRUE;
115+
*/
116+
EXTERN_C extern BOOL __YY_Thunks_Disable_Rreload_Dlls /* = FALSE*/;
117+
107118
// 从DllMain缓存RtlDllShutdownInProgress状态,规避退出时调用RtlDllShutdownInProgress。
108119
// 0:缓存无效
109120
// 1:模块正常卸载
@@ -642,14 +653,17 @@ static ThunksInitStatus __cdecl _YY_initialize_winapi_thunks(ThunksInitStatus _s
642653
// 后续过程已经可以线程安全执行了,所以我们立即解锁,避免其他线程过于长时间的等待
643654
InterlockedExchange((volatile LONG*)&s_eThunksStatus, LONG(_sInitStatus));
644655

645-
/*
646-
* https://github.com/Chuyu-Team/YY-Thunks/issues/7
647-
* 为了避免 try_get 系列函数竞争 DLL load锁,因此我们将所有被Thunk的API都预先加载完毕。
648-
*/
649-
for (auto p = (InitFunType*)__YY_THUNKS_INIT_FUN_START; p != (InitFunType*)__YY_THUNKS_INIT_FUN_END; ++p)
656+
if (!__YY_Thunks_Disable_Rreload_Dlls)
650657
{
651-
if (auto pInitFun = *p)
652-
pInitFun();
658+
/*
659+
* https://github.com/Chuyu-Team/YY-Thunks/issues/7
660+
* 为了避免 try_get 系列函数竞争 DLL load锁,因此我们将所有被Thunk的API都预先加载完毕。
661+
*/
662+
for (auto p = (InitFunType*)__YY_THUNKS_INIT_FUN_START; p != (InitFunType*)__YY_THUNKS_INIT_FUN_END; ++p)
663+
{
664+
if (auto pInitFun = *p)
665+
pInitFun();
666+
}
653667
}
654668

655669
return _sInitStatus;

src/YY-Thunks.UnitTest/weak.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
const void* __acrt_atexit_table;
33

44
const void* __pfnDllMainCRTStartupForYY_Thunks;
5+
6+
const void* __YY_Thunks_Disable_Rreload_Dlls;

0 commit comments

Comments
 (0)