Browse Source

Generic refactoring for timer_set_now

Luke Dashjr 12 years ago
parent
commit
d7d70cbe71
4 changed files with 30 additions and 7 deletions
  1. 4 1
      compat.h
  2. 2 0
      miner.c
  3. 21 4
      util.c
  4. 3 2
      util.h

+ 4 - 1
compat.h

@@ -85,7 +85,8 @@ struct tm *localtime_convert(time_t t)
 #endif
 #endif
 
 
 #ifndef HAVE_NANOSLEEP
 #ifndef HAVE_NANOSLEEP
-extern void cgtime(struct timeval *);
+extern void (*timer_set_now)(struct timeval *);
+#define cgtime(tvp)  timer_set_now(tvp)
 
 
 static inline int nanosleep(const struct timespec *req, struct timespec *rem)
 static inline int nanosleep(const struct timespec *req, struct timespec *rem)
 {
 {
@@ -118,6 +119,8 @@ static inline int nanosleep(const struct timespec *req, struct timespec *rem)
 	}
 	}
 	return 0;
 	return 0;
 }
 }
+
+#undef cgtime
 #endif
 #endif
 
 
 #ifdef WIN32
 #ifdef WIN32

+ 2 - 0
miner.c

@@ -532,6 +532,7 @@ struct pool *add_pool(void)
 	/* Make sure the pool doesn't think we've been idle since time 0 */
 	/* Make sure the pool doesn't think we've been idle since time 0 */
 	pool->tv_idle.tv_sec = ~0UL;
 	pool->tv_idle.tv_sec = ~0UL;
 	
 	
+	bfg_init_time();
 	cgtime(&pool->cgminer_stats.start_tv);
 	cgtime(&pool->cgminer_stats.start_tv);
 
 
 	pool->rpc_proxy = NULL;
 	pool->rpc_proxy = NULL;
@@ -9182,6 +9183,7 @@ int main(int argc, char *argv[])
 #endif
 #endif
 
 
 	raise_fd_limits();
 	raise_fd_limits();
+	bfg_init_time();
 	
 	
 	if (opt_benchmark) {
 	if (opt_benchmark) {
 		struct pool *pool;
 		struct pool *pool;

+ 21 - 4
util.c

@@ -1098,12 +1098,11 @@ void nusleep(unsigned int usecs)
 #endif
 #endif
 }
 }
 
 
-/* This is a cgminer gettimeofday wrapper. Since we always call gettimeofday
- * with tz set to NULL, and windows' default resolution is only 15ms, this
- * gives us higher resolution times on windows. */
-void cgtime(struct timeval *tv)
+static
+void _now_gettimeofday(struct timeval *tv)
 {
 {
 #ifdef WIN32
 #ifdef WIN32
+	// Windows' default resolution is only 15ms. This requests 1ms.
 	timeBeginPeriod(1);
 	timeBeginPeriod(1);
 #endif
 #endif
 	gettimeofday(tv, NULL);
 	gettimeofday(tv, NULL);
@@ -1112,6 +1111,24 @@ void cgtime(struct timeval *tv)
 #endif
 #endif
 }
 }
 
 
+static
+void _now_is_not_set(__maybe_unused struct timeval *tv)
+{
+	// Might be unclean to swap algorithms after getting a timer
+	quit(1, "timer_set_now called before bfg_init_time");
+}
+
+void (*timer_set_now)(struct timeval *tv) = _now_is_not_set;
+
+void bfg_init_time()
+{
+	if (timer_set_now != _now_is_not_set)
+		return;
+	
+	timer_set_now = _now_gettimeofday;
+	applog(LOG_DEBUG, "Timers: Using gettimeofday");
+}
+
 void subtime(struct timeval *a, struct timeval *b)
 void subtime(struct timeval *a, struct timeval *b)
 {
 {
 	timersub(a, b, b);
 	timersub(a, b, b);

+ 3 - 2
util.h

@@ -92,7 +92,6 @@ void thr_info_freeze(struct thr_info *thr);
 void thr_info_cancel(struct thr_info *thr);
 void thr_info_cancel(struct thr_info *thr);
 void nmsleep(unsigned int msecs);
 void nmsleep(unsigned int msecs);
 void nusleep(unsigned int usecs);
 void nusleep(unsigned int usecs);
-void cgtime(struct timeval *tv);
 void subtime(struct timeval *a, struct timeval *b);
 void subtime(struct timeval *a, struct timeval *b);
 void addtime(struct timeval *a, struct timeval *b);
 void addtime(struct timeval *a, struct timeval *b);
 bool time_more(struct timeval *a, struct timeval *b);
 bool time_more(struct timeval *a, struct timeval *b);
@@ -223,7 +222,9 @@ bool timer_isset(const struct timeval *tvp)
 	return tvp->tv_sec != -1;
 	return tvp->tv_sec != -1;
 }
 }
 
 
-#define timer_set_now(tvp)  cgtime(tvp)
+extern void (*timer_set_now)(struct timeval *);
+extern void bfg_init_time();
+#define cgtime(tvp)  timer_set_now(tvp)
 
 
 #define TIMEVAL_USECS(usecs)  (  \
 #define TIMEVAL_USECS(usecs)  (  \
 	(struct timeval){  \
 	(struct timeval){  \