Browse Source

Abstract select_timeout function to convert a realtime timeval to a timeout pointer for select()

Luke Dashjr 13 years ago
parent
commit
7849bb1c11
2 changed files with 16 additions and 12 deletions
  1. 2 12
      miner.c
  2. 14 0
      util.h

+ 2 - 12
miner.c

@@ -6842,7 +6842,7 @@ void minerloop_async(struct thr_info *mythr)
 	struct cgpu_info *cgpu = mythr->cgpu;
 	const struct device_api *api = cgpu->api;
 	struct timeval tv_now;
-	struct timeval tv_timeout, *tvp_timeout;
+	struct timeval tv_timeout;
 	struct work *work;
 	const bool primary = (!mythr->device_thread) || mythr->primary_thread;
 	struct cgpu_info *proc;
@@ -6903,21 +6903,11 @@ disabled: ;
 		}
 		
 		gettimeofday(&tv_now, NULL);  // NOTE: Can go away when fully async
-		if (tv_timeout.tv_sec == -1)
-			tvp_timeout = NULL;
-		else
-		{
-			tvp_timeout = &tv_timeout;
-			if (timercmp(&tv_timeout, &tv_now, <))
-				timerclear(&tv_timeout);
-			else
-				timersub(&tv_timeout, &tv_now, &tv_timeout);
-		}
 		// FIXME: break select on work restart
 		FD_ZERO(&rfds);
 		FD_SET(mythr->notifier[0], &rfds);
 		maxfd = mythr->notifier[0];
-		if (select(maxfd + 1, &rfds, NULL, NULL, tvp_timeout) < 0)
+		if (select(maxfd + 1, &rfds, NULL, NULL, select_timeout(&tv_timeout, &tv_now)) < 0)
 			continue;
 		if (FD_ISSET(mythr->notifier[0], &rfds)) {
 			notifier_read(mythr->notifier);

+ 14 - 0
util.h

@@ -106,5 +106,19 @@ void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
 		*tvp_timeout = *tvp_time;
 }
 
+static inline
+struct timeval *select_timeout(struct timeval *tvp_timeout, struct timeval *tvp_now)
+{
+	if (tvp_timeout->tv_sec == -1)
+		return NULL;
+	
+	if (timercmp(tvp_timeout, tvp_now, <))
+		timerclear(tvp_timeout);
+	else
+		timersub(tvp_timeout, tvp_now, tvp_timeout);
+	
+	return tvp_timeout;
+}
+
 
 #endif /* __UTIL_H__ */