瀏覽代碼

Fixed std.time.Now() which should hopefully fix Timers.

Mark Sibly 9 年之前
父節點
當前提交
6c314b7f33
共有 3 個文件被更改,包括 22 次插入70 次删除
  1. 13 14
      modules/std/async/native/async.cpp
  2. 8 55
      modules/std/time/native/time.cpp
  3. 1 1
      modules/std/time/time.monkey2

+ 13 - 14
modules/std/async/native/async.cpp

@@ -3,14 +3,14 @@
 
 namespace bbAsync{
 
-	typedef std::chrono::duration<double> secs_t;
-	typedef std::chrono::high_resolution_clock clock_t;
-	typedef std::chrono::time_point<clock_t,secs_t> time_t;
+	typedef std::chrono::duration<double> Duration;
+	typedef std::chrono::high_resolution_clock Clock;
+	typedef std::chrono::time_point<Clock,Duration> TimePoint;
 
 	struct DelayedEvent{
 		DelayedEvent *succ;
 		Event *event;
-		time_t time;
+		TimePoint time;
 	};
 
 	DelayedEvent *que;
@@ -31,22 +31,19 @@ namespace bbAsync{
 			for(;;){
 			
 				if( que ){
-					que_condvar.wait_for( lock,que->time-clock_t::now() );
+					que_condvar.wait_until( lock,que->time );
 				}else{
 					que_condvar.wait( lock );
 				}
 
-				//prevent spamming...?				
-				time_t now=clock_t::now();
-				
-				while( que && que->time<=now ){
+				while( que && que->time<=Clock::now() ){
 					
 					DelayedEvent *devent=que;
 	
-					devent->event->post();
-					
 					que=devent->succ;
 					
+					devent->event->post();
+					
 					devent->succ=free_que;
 					
 					free_que=devent;
@@ -70,14 +67,15 @@ namespace bbAsync{
 	
 	void Event::post( double delay ){
 	
-		time_t now=clock_t::now();
+		TimePoint time=Clock::now()+Duration( delay );
 		
 		initQue();
 		
 		{
-			std::unique_lock<std::mutex> lock( que_mutex );
+			std::lock_guard<std::mutex> lock( que_mutex );
 			
 			DelayedEvent *devent=free_que;
+			
 			if( devent ){
 				free_que=devent->succ;
 			}else{
@@ -85,7 +83,7 @@ namespace bbAsync{
 			}
 			
 			devent->event=this;
-			devent->time=now+secs_t( delay );
+			devent->time=time;
 	
 			DelayedEvent *succ,**pred=&que;
 			
@@ -95,6 +93,7 @@ namespace bbAsync{
 			}
 			
 			devent->succ=succ;
+
 			*pred=devent;
 		}
 		

+ 8 - 55
modules/std/time/native/time.cpp

@@ -1,72 +1,25 @@
 
 #include "time.h"
 
-#if _WIN32
-
-#include <windows.h>
-
-#else
-
-#include <sys/time.h>
-
-#endif
+#include <chrono>
+#include <thread>
 
 namespace bbTime{
 
-#if _WIN32
-
-	long start;
-	double freq;
+	typedef std::chrono::duration<double> Duration;
+	typedef std::chrono::high_resolution_clock Clock;
 	
 	double now(){
 	
-		LARGE_INTEGER icounter;
-		QueryPerformanceCounter( &icounter );
-		long counter=icounter.QuadPart;
+		static Clock::time_point start=Clock::now();
 		
-		if( !start ){
-			start=counter;
-			LARGE_INTEGER ifreq;
-			QueryPerformanceFrequency( &ifreq );
-			freq=double( ifreq.QuadPart );
-			return 0;
-		}
-		
-		counter-=start;
-		return double( counter )/freq;
-	}
-	
-	void sleep( double seconds ){
-	
-		Sleep( seconds * 1000 );
-	}
-	
-#else
-
-	long start;
-	
-	double now(){
+		auto elapsed=(Clock::now()-start).count();
 	
-		timeval tv;
-		gettimeofday( &tv,0 );
-		long counter=tv.tv_sec*1000000+tv.tv_usec;
-		
-		if( !start ){
-			start=counter;
-			return 0;
-		}
-		
-		counter-=start;
-		return double( counter )/1000000.0;
+		return elapsed * ((double)Clock::period::num/(double)Clock::period::den);
 	}
 	
 	void sleep( double seconds ){
 	
-		sleep( seconds * 1000 );
-//		usleep( seconds * 1000000 );
+		std::this_thread::sleep_for( Duration( seconds ) );
 	}
-	
-	
-#endif
-
 }

+ 1 - 1
modules/std/time/time.monkey2

@@ -15,7 +15,7 @@ Extern
 #end
 Function Now:Double()="bbTime::now"
 
-#rem monkeydoc Puts app to sleep.
+#rem monkeydoc Puts thread to sleep.
 
 Note: this will also cause all fibers to sleep.