1717#include < sstream>
1818#include < thread>
1919#include < signal.h>
20+
21+ #if _WINDOWS
22+
23+ #include < windows.h>
24+ #pragma comment(lib,"Winmm.lib")
25+
26+ #else
27+
2028#include < sys/time.h>
2129#include < unistd.h>
2230
31+ #endif
32+
2333using namespace cn ::vimfung::luascriptcore;
2434
2535/* *
@@ -32,6 +42,7 @@ static const char * CatchLuaExceptionHandlerName = "__catchExcepitonHandler";
3242 */
3343static std::vector<LuaContext *> _needsGCContextList;
3444
45+
3546/* *
3647 * 方法路由处理器
3748 *
@@ -93,13 +104,35 @@ static LuaValue* catchLuaExceptionHandler (LuaContext *context, std::string meth
93104 return NULL ;
94105}
95106
107+ #if _WINDOWS
108+
109+ void WINAPI contextGCHandler (UINT wTimerID, UINT msg, DWORD dwUser, DWORD dwl, DWORD dw2)
110+ {
111+ unityDebug (" gc handler..." );
112+
113+ // 重置计时器
114+ timeKillEvent (wTimerID);
115+
116+ // 进行内存回收
117+ for (std::vector<LuaContext *>::iterator it = _needsGCContextList.begin (); it != _needsGCContextList.end (); it++)
118+ {
119+ LuaContext *context = *it;
120+ context->gcHandler ();
121+ context->release (); // 回收后释放
122+ }
123+ // 清空
124+ _needsGCContextList.clear ();
125+ }
126+
127+ #else
96128/* *
97129 上下文内存回收处理
98130
99131 @param signo 信号
100132 */
101133static void contextGCHandler (int signo)
102134{
135+
103136 // 重置计时器
104137 struct itimerval itv;
105138 itv.it_value .tv_sec = 0 ;
@@ -118,6 +151,8 @@ static void contextGCHandler(int signo)
118151 _needsGCContextList.clear ();
119152}
120153
154+ #endif
155+
121156/* *
122157 上下文开始回收
123158
@@ -141,6 +176,16 @@ static void contextStartGC(LuaContext *context)
141176 context -> retain ();
142177 _needsGCContextList.push_back (context);
143178
179+ #if _WINDOWS
180+
181+ MMRESULT gcTimerId = timeSetEvent (100 , 1 , (LPTIMECALLBACK)contextGCHandler, NULL , TIME_ONESHOT);
182+ if (NULL == gcTimerId)
183+ {
184+ unityDebug (" gc timer create error!" );
185+ }
186+
187+ #else
188+
144189 // 监听定时器信号
145190 signal (SIGALRM, contextGCHandler);
146191
@@ -150,6 +195,9 @@ static void contextStartGC(LuaContext *context)
150195 itv.it_value .tv_usec = 100000 ;
151196 itv.it_interval = itv.it_value ;
152197 setitimer (ITIMER_REAL, &itv, NULL );
198+
199+ #endif
200+
153201 }
154202}
155203
0 commit comments