2
0
Эх сурвалжийг харах

move rare thread functions to platform specific code

Nicolas Cannasse 7 жил өмнө
parent
commit
232d33686a
3 өөрчлөгдсөн 18 нэмэгдсэн , 74 устгасан
  1. 18 12
      libs/ui/ui_win.c
  2. 0 5
      src/hl.h
  3. 0 57
      src/std/thread.c

+ 18 - 12
libs/ui/ui_win.c

@@ -196,7 +196,7 @@ HL_PRIM void HL_NAME(ui_stop_loop)() {
 
 
 typedef struct {
 typedef struct {
 	hl_thread *thread;
 	hl_thread *thread;
-	hl_thread *original;
+	DWORD original;
 	void *callback;
 	void *callback;
 	double timeout;
 	double timeout;
 	int ticks;
 	int ticks;
@@ -205,9 +205,9 @@ typedef struct {
 
 
 static void sentinel_loop( vsentinel *s ) {
 static void sentinel_loop( vsentinel *s ) {
 	int time_ms = (int)((s->timeout * 1000.) / 16.);
 	int time_ms = (int)((s->timeout * 1000.) / 16.);
-	hl_thread_registers *regs = (hl_thread_registers*)malloc(sizeof(int_val) * hl_thread_context_size());
-	int eip = hl_thread_context_index("eip");
-	int esp = hl_thread_context_index("esp");
+	HANDLE h = OpenThread(THREAD_ALL_ACCESS,FALSE,s->original);
+	CONTEXT regs;
+	regs.ContextFlags = CONTEXT_FULL;
 	while( true ) {
 	while( true ) {
 		int k = 0;
 		int k = 0;
 		int tick = s->ticks;
 		int tick = s->ticks;
@@ -218,15 +218,21 @@ static void sentinel_loop( vsentinel *s ) {
 			k++;
 			k++;
 			if( k == 16 ) {
 			if( k == 16 ) {
 				// pause
 				// pause
-				hl_thread_pause(s->original, true);
-				hl_thread_get_context(s->original,regs);
+				SuspendThread(h);
+				GetThreadContext(h,&regs);
 				// simulate a call
 				// simulate a call
-				*--(int_val*)regs[esp] = regs[eip];
-				*--(int_val*)regs[esp] = regs[esp];
-				regs[eip] = (int_val)s->callback;
+#				ifdef HL_64
+				*--(int_val*)regs.Rsp = regs.Rip;
+				*--(int_val*)regs.Rsp = regs.Rsp;
+				regs.Rip = (int_val)s->callback;
+#				else
+				*--(int_val*)regs.Esp = regs.Eip;
+				*--(int_val*)regs.Esp = regs.Esp;
+				regs.Eip = (int_val)s->callback;
+#				endif
 				// resume
 				// resume
-				hl_thread_set_context(s->original,regs);
-				hl_thread_pause(s->original, false);
+				SetThreadContext(h,&regs);
+				ResumeThread(h);
 				break;
 				break;
 			}
 			}
 		}
 		}
@@ -242,7 +248,7 @@ HL_PRIM vsentinel *HL_NAME(ui_start_sentinel)( double timeout, vclosure *c ) {
 	s->timeout = timeout;
 	s->timeout = timeout;
 	s->ticks = 0;
 	s->ticks = 0;
 	s->pause = false;
 	s->pause = false;
-	s->original = hl_thread_current();
+	s->original = GetCurrentThreadId();
 	s->callback = c->fun;
 	s->callback = c->fun;
 	s->thread = hl_thread_start(sentinel_loop,s,false);
 	s->thread = hl_thread_start(sentinel_loop,s,false);
 	return s;
 	return s;

+ 0 - 5
src/hl.h

@@ -580,15 +580,10 @@ HL_API vdynamic *hl_dyn_call( vclosure *c, vdynamic **args, int nargs );
 
 
 struct _hl_thread;
 struct _hl_thread;
 typedef struct _hl_thread hl_thread;
 typedef struct _hl_thread hl_thread;
-typedef int_val hl_thread_registers;
 
 
 HL_API hl_thread *hl_thread_start( void *callback, void *param, bool withGC );
 HL_API hl_thread *hl_thread_start( void *callback, void *param, bool withGC );
 HL_API hl_thread *hl_thread_current();
 HL_API hl_thread *hl_thread_current();
 HL_API bool hl_thread_pause( hl_thread *t, bool pause );
 HL_API bool hl_thread_pause( hl_thread *t, bool pause );
-HL_API int hl_thread_context_size();
-HL_API int hl_thread_context_index( const char *name );
-HL_API bool hl_thread_get_context( hl_thread *t, hl_thread_registers *regs );
-HL_API bool hl_thread_set_context( hl_thread *t, hl_thread_registers *regs );
 
 
 // ----------------------- ALLOC --------------------------------------------------
 // ----------------------- ALLOC --------------------------------------------------
 
 

+ 0 - 57
src/std/thread.c

@@ -90,60 +90,3 @@ HL_PRIM bool hl_thread_pause( hl_thread *t, bool pause ) {
 	return false;
 	return false;
 #	endif
 #	endif
 }
 }
-
-HL_PRIM int hl_thread_context_size() {
-#	ifdef HL_WIN
-	return (sizeof(CONTEXT) + sizeof(int_val) - 1) / sizeof(int_val);
-#	else
-	return 0;
-#	endif
-}
-
-HL_PRIM int hl_thread_context_index( const char *name ) {
-#	ifdef HL_WIN
-	CONTEXT *c = NULL;
-#	define _ADDR(__r) (int)(((int_val)&c->__r) / sizeof(int_val))
-#	ifdef HL_64
-#		define ADDR(_,r) _ADDR(r)
-#	else
-#		define ADDR(r,_) _ADDR(r)
-#	endif
-	if( strcmp(name,"eip") == 0 )
-		return ADDR(Eip,Rip);
-	if( strcmp(name,"esp") == 0 )
-		return ADDR(Esp,Rsp);
-	return -1;
-#	else
-	return -1;
-#	endif
-#	undef ADDR
-#	undef _ADDR
-}
-
-HL_PRIM bool hl_thread_get_context( hl_thread *t, hl_thread_registers *regs ) {
-#	ifdef HL_WIN
-	HANDLE h = OpenThread(THREAD_ALL_ACCESS,FALSE,(DWORD)(int_val)t);
-	bool ret;
-	CONTEXT *c = (CONTEXT*)regs;
-	c->ContextFlags = CONTEXT_FULL;
-	ret = (bool)GetThreadContext(h,c);
-	CloseHandle(h);
-	return ret;
-#	else
-	return false;
-#	endif
-}
-
-HL_PRIM bool hl_thread_set_context( hl_thread *t, hl_thread_registers *regs ) {
-#	ifdef HL_WIN
-	HANDLE h = OpenThread(THREAD_ALL_ACCESS,FALSE,(DWORD)(int_val)t);
-	bool ret;
-	CONTEXT *c = (CONTEXT*)regs;
-	c->ContextFlags = CONTEXT_FULL;
-	ret = (bool)SetThreadContext(h,c);
-	CloseHandle(h);
-	return ret;
-#	else
-	return false;
-#	endif
-}