Browse Source

notifier_wait, notifier_wait_us, and notifier_reset functions

Luke Dashjr 11 years ago
parent
commit
e222bdb04e
2 changed files with 39 additions and 0 deletions
  1. 36 0
      util.c
  2. 3 0
      util.h

+ 36 - 0
util.c

@@ -2946,6 +2946,42 @@ void notifier_read(notifier_t fd)
 #endif
 }
 
+bool notifier_wait(notifier_t notifier, const struct timeval *tvp_timeout)
+{
+	struct timeval tv_now, tv_timeout;
+	fd_set rfds;
+	int e;
+	
+	while (true)
+	{
+		FD_ZERO(&rfds);
+		FD_SET(notifier[0], &rfds);
+		tv_timeout = *tvp_timeout;
+		timer_set_now(&tv_now);
+		e = select(notifier[0]+1, &rfds, NULL, NULL, select_timeout(&tv_timeout, &tv_now));
+		if (e > 0)
+			return true;
+		if (e == 0)
+			return false;
+	}
+}
+
+bool notifier_wait_us(notifier_t notifier, const unsigned long long usecs)
+{
+	struct timeval tv_timeout = TIMEVAL_USECS(usecs);
+	return notifier_wait(notifier, &tv_timeout);
+}
+
+void notifier_reset(notifier_t notifier)
+{
+	fd_set rfds;
+	struct timeval tv_timeout = { .tv_sec = 0, };
+	FD_ZERO(&rfds);
+	FD_SET(notifier[0], &rfds);
+	while (select(notifier[0]+1, &rfds, NULL, NULL, &tv_timeout) != 0)
+		notifier_read(notifier);
+}
+
 void notifier_init_invalid(notifier_t fd)
 {
 	fd[0] = fd[1] = INVSOCK;

+ 3 - 0
util.h

@@ -197,6 +197,9 @@ typedef SOCKETTYPE notifier_t[2];
 extern void notifier_init(notifier_t);
 extern void notifier_wake(notifier_t);
 extern void notifier_read(notifier_t);
+extern bool notifier_wait(notifier_t, const struct timeval *);
+extern bool notifier_wait_us(notifier_t, unsigned long long usecs);
+extern void notifier_reset(notifier_t);
 extern void notifier_init_invalid(notifier_t);
 extern void notifier_destroy(notifier_t);