summaryrefslogtreecommitdiff
path: root/tools/future.h
blob: 73d4bfecf756fe33c3c85819a1a71d6f20e62c8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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