Browse Source

Convert cgtimer_t to struct timeval

Luke Dashjr 12 years ago
parent
commit
039282ccdd
2 changed files with 22 additions and 60 deletions
  1. 12 56
      util.c
  2. 10 4
      util.h

+ 12 - 56
util.c

@@ -1254,19 +1254,7 @@ void timeraddspec(struct timespec *a, const struct timespec *b)
 	}
 	}
 }
 }
 
 
-static int timespec_to_ms(struct timespec *ts)
-{
-	return ts->tv_sec * 1000 + ts->tv_nsec / 1000000;
-}
-
-/* These are cgminer specific sleep functions that use an absolute nanosecond
- * resolution timer to avoid poor usleep accuracy and overruns. */
 #ifndef WIN32
 #ifndef WIN32
-void cgtimer_time(cgtimer_t *ts_start)
-{
-	clock_gettime(CLOCK_MONOTONIC, ts_start);
-}
-
 static void nanosleep_abstime(struct timespec *ts_end)
 static void nanosleep_abstime(struct timespec *ts_end)
 {
 {
 	int ret;
 	int ret;
@@ -1279,40 +1267,28 @@ static void nanosleep_abstime(struct timespec *ts_end)
 /* Reentrant version of cgsleep functions allow start time to be set separately
 /* Reentrant version of cgsleep functions allow start time to be set separately
  * from the beginning of the actual sleep, allowing scheduling delays to be
  * from the beginning of the actual sleep, allowing scheduling delays to be
  * counted in the sleep. */
  * counted in the sleep. */
-void cgsleep_ms_r(cgtimer_t *ts_start, int ms)
+void cgsleep_ms_r(cgtimer_t *tv_start, int ms)
 {
 {
+	struct timespec ts_start[1];
 	struct timespec ts_end;
 	struct timespec ts_end;
 
 
+	timeval_to_spec(ts_start, tv_start);
 	ms_to_timespec(&ts_end, ms);
 	ms_to_timespec(&ts_end, ms);
 	timeraddspec(&ts_end, ts_start);
 	timeraddspec(&ts_end, ts_start);
 	nanosleep_abstime(&ts_end);
 	nanosleep_abstime(&ts_end);
 }
 }
 
 
-void cgsleep_us_r(cgtimer_t *ts_start, int64_t us)
+void cgsleep_us_r(cgtimer_t *tv_start, int64_t us)
 {
 {
+	struct timespec ts_start[1];
 	struct timespec ts_end;
 	struct timespec ts_end;
 
 
+	timeval_to_spec(ts_start, tv_start);
 	us_to_timespec(&ts_end, us);
 	us_to_timespec(&ts_end, us);
 	timeraddspec(&ts_end, ts_start);
 	timeraddspec(&ts_end, ts_start);
 	nanosleep_abstime(&ts_end);
 	nanosleep_abstime(&ts_end);
 }
 }
 
 
-int cgtimer_to_ms(cgtimer_t *cgt)
-{
-	return timespec_to_ms(cgt);
-}
-
-/* Subtracts b from a and stores it in res. */
-void cgtimer_sub(cgtimer_t *a, cgtimer_t *b, cgtimer_t *res)
-{
-	res->tv_sec = a->tv_sec - b->tv_sec;
-	res->tv_nsec = a->tv_nsec - b->tv_nsec;
-	if (res->tv_nsec < 0) {
-		res->tv_nsec += 1000000000;
-		res->tv_sec--;
-	}
-}
-
 static
 static
 void _now_gettimeofday(struct timeval *tv)
 void _now_gettimeofday(struct timeval *tv)
 {
 {
@@ -1346,15 +1322,6 @@ void _now_gettimeofday(struct timeval *tv)
 	tv->tv_usec = lidiv.rem / 10;
 	tv->tv_usec = lidiv.rem / 10;
 }
 }
 
 
-void cgtimer_time(cgtimer_t *ts_start)
-{
-	lldiv_t lidiv;;
-
-	decius_time(&lidiv);
-	ts_start->tv_sec = lidiv.quot;
-	ts_start->tv_nsec = lidiv.quot * 100;
-}
-
 /* Subtract b from a */
 /* Subtract b from a */
 static void timersubspec(struct timespec *a, const struct timespec *b)
 static void timersubspec(struct timespec *a, const struct timespec *b)
 {
 {
@@ -1378,36 +1345,25 @@ static void cgsleep_spec(struct timespec *ts_diff, const struct timespec *ts_sta
 	nanosleep(ts_diff, NULL);
 	nanosleep(ts_diff, NULL);
 }
 }
 
 
-void cgsleep_ms_r(cgtimer_t *ts_start, int ms)
+void cgsleep_ms_r(cgtimer_t *tv_start, int ms)
 {
 {
+	struct timespec ts_start;
 	struct timespec ts_diff;
 	struct timespec ts_diff;
 
 
+	timeval_to_spec(&ts_start, tv_start);
 	ms_to_timespec(&ts_diff, ms);
 	ms_to_timespec(&ts_diff, ms);
 	cgsleep_spec(&ts_diff, ts_start);
 	cgsleep_spec(&ts_diff, ts_start);
 }
 }
 
 
-void cgsleep_us_r(cgtimer_t *ts_start, int64_t us)
+void cgsleep_us_r(cgtimer_t *tv_start, int64_t us)
 {
 {
+	struct timespec ts_start;
 	struct timespec ts_diff;
 	struct timespec ts_diff;
 
 
+	timeval_to_spec(&ts_start, tv_start);
 	us_to_timespec(&ts_diff, us);
 	us_to_timespec(&ts_diff, us);
 	cgsleep_spec(&ts_diff, ts_start);
 	cgsleep_spec(&ts_diff, ts_start);
 }
 }
-
-int cgtimer_to_ms(cgtimer_t *cgt)
-{
-	return timespec_to_ms(cgt);
-}
-
-void cgtimer_sub(cgtimer_t *a, cgtimer_t *b, cgtimer_t *res)
-{
-	res->tv_sec = a->tv_sec - b->tv_sec;
-	res->tv_nsec = a->tv_nsec - b->tv_nsec;
-	if (res->tv_nsec < 0) {
-		res->tv_nsec += 1000000000;;
-		res->tv_sec--;
-	}
-}
 #endif
 #endif
 
 
 void cgsleep_ms(int ms)
 void cgsleep_ms(int ms)

+ 10 - 4
util.h

@@ -98,7 +98,7 @@ bool isCspace(int c)
 	}
 	}
 }
 }
 
 
-typedef struct timespec cgtimer_t;
+typedef struct timeval cgtimer_t;
 
 
 struct thr_info;
 struct thr_info;
 struct pool;
 struct pool;
@@ -132,12 +132,18 @@ void ms_to_timespec(struct timespec *spec, int64_t ms);
 void timeraddspec(struct timespec *a, const struct timespec *b);
 void timeraddspec(struct timespec *a, const struct timespec *b);
 void cgsleep_ms(int ms);
 void cgsleep_ms(int ms);
 void cgsleep_us(int64_t us);
 void cgsleep_us(int64_t us);
-void cgtimer_time(cgtimer_t *ts_start);
+#define cgtimer_time(ts_start) timer_set_now(ts_start)
 #define cgsleep_prepare_r(ts_start) cgtimer_time(ts_start)
 #define cgsleep_prepare_r(ts_start) cgtimer_time(ts_start)
 void cgsleep_ms_r(cgtimer_t *ts_start, int ms);
 void cgsleep_ms_r(cgtimer_t *ts_start, int ms);
 void cgsleep_us_r(cgtimer_t *ts_start, int64_t us);
 void cgsleep_us_r(cgtimer_t *ts_start, int64_t us);
-int cgtimer_to_ms(cgtimer_t *cgt);
-void cgtimer_sub(cgtimer_t *a, cgtimer_t *b, cgtimer_t *res);
+
+static inline
+int cgtimer_to_ms(cgtimer_t *cgt)
+{
+	return (cgt->tv_sec * 1000) + (cgt->tv_usec / 1000);
+}
+
+#define cgtimer_sub(a, b, res)  timersub(a, b, res)
 double us_tdiff(struct timeval *end, struct timeval *start);
 double us_tdiff(struct timeval *end, struct timeval *start);
 double tdiff(struct timeval *end, struct timeval *start);
 double tdiff(struct timeval *end, struct timeval *start);
 bool _stratum_send(struct pool *pool, char *s, ssize_t len, bool force);
 bool _stratum_send(struct pool *pool, char *s, ssize_t len, bool force);