summaryrefslogtreecommitdiff
path: root/tools/future.h
diff options
context:
space:
mode:
authorphintuka <phintuka>2006-06-03 10:04:49 +0000
committerphintuka <phintuka>2006-06-03 10:04:49 +0000
commit0e345486181ef82b681dd6047f3b6ccb44c77146 (patch)
treea5401c7f97ab047a0afa890e6806d8537564102a /tools/future.h
parent321eb114c9fe9abd954ce4270595d53df6cccbae (diff)
downloadxineliboutput-0_99rc4.tar.gz
xineliboutput-0_99rc4.tar.bz2
Initial importxineliboutput-0_99rc4
Diffstat (limited to 'tools/future.h')
-rw-r--r--tools/future.h84
1 files changed, 84 insertions, 0 deletions
diff --git a/tools/future.h b/tools/future.h
new file mode 100644
index 00000000..73d4bfec
--- /dev/null
+++ b/tools/future.h
@@ -0,0 +1,84 @@
+/*
+ * future.h: A variable that gets its value in future.
+ * Used to convert asynchronous IPCs to synchronous.
+ *
+ * See the main source file 'xineliboutput.c' for copyright information and
+ * how to reach the author.
+ *
+ * $Id: future.h,v 1.1 2006-06-03 10:04:27 phintuka Exp $
+ *
+ */
+
+#ifndef __FUTURE_H
+#define __FUTURE_H
+
+#include <vdr/thread.h>
+
+template <class T>
+class cFuture {
+
+ private:
+ cMutex mutex;
+ cCondVar cond;
+ bool m_Ready;
+ T m_Value;
+
+ public:
+
+ cFuture()
+ {
+ m_Ready = false;
+ }
+
+ void Reset(void)
+ {
+ cMutexLock l(&mutex);
+ m_Ready = false;
+ }
+
+ //
+ // Producer interface
+ //
+
+ void Set(T& Value)
+ {
+ cMutexLock l(&mutex);
+ m_Value = Value;
+ m_Ready = true;
+ cond.Broadcast();
+ }
+
+ //
+ // Consumer interface
+ //
+
+ bool Wait(int Timeout = -1)
+ {
+ cMutexLock l(&mutex);
+
+ if(Timeout >= 0)
+ return cond.TimedWait(mutex, Timeout);
+
+ while(!m_Ready)
+ cond.Wait(mutex);
+
+ return true;
+ }
+
+ bool IsReady(void)
+ {
+ cMutexLock l(&mutex);
+ return m_Ready;
+ }
+
+ T Value(void)
+ {
+ cMutexLock l(&mutex);
+ while(!m_Ready)
+ cond.Wait(mutex);
+ return m_Value;
+ }
+};
+
+
+#endif // __FUTURE_H