CPN
Computational Process Networks
Future.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
23 #ifndef SYNC_FUTURE_H
24 #define SYNC_FUTURE_H
25 #pragma once
26 #include <cpn/common.h>
29 #include <cpn/utils/AutoLock.h>
30 
31 namespace Sync {
36  template<typename T>
37  class Future {
38  public:
39  Future() : done(false), canceled(false), ret() {}
40  virtual ~Future() {}
41 
42  bool Done() {
44  return done || canceled;
45  }
46 
47  void Cancel() {
49  canceled = true;
51  }
52 
53  void Wait() {
55  InternalWait();
56  }
57 
58  bool IsCanceled() {
60  return canceled;
61  }
62 
63  virtual void Set(const T &r) {
65  InternalSet(r);
66  }
67 
68  virtual T Get() {
70  InternalWait();
71  return ret;
72  }
73 
74  protected:
75  void InternalWait() {
76  while (!canceled && !done) { future_cond.Wait(future_lock); }
77  }
78 
79  void InternalSet(const T &r) {
80  ret = r;
81  done = true;
83  }
84 
87  bool done;
88  bool canceled;
89  T ret;
90  };
91 
92  template<>
93  class Future<void> {
94  public:
95  Future() : done(false), canceled(false) {}
96  virtual ~Future() {}
97 
98  bool Done() {
100  return done || canceled;
101  }
102 
103  void Cancel() {
105  canceled = true;
107  }
108 
109  void Wait() {
111  InternalWait();
112  }
113 
114  bool IsCanceled() {
116  return canceled;
117  }
118 
119  virtual void Set() {
121  InternalSet();
122  }
123 
124  virtual void Get() {
126  InternalWait();
127  }
128 
129  protected:
130  void InternalWait() {
131  while (!canceled && !done) { future_cond.Wait(future_lock); }
132  }
133 
134  void InternalSet() {
135  done = true;
137  }
138 
141  bool done;
142  bool canceled;
143  };
144 
145 }
146 #endif
PthreadCondition & Wait(PthreadMutex &mutex)
PthreadCondition & Broadcast(void)
bool canceled
Definition: Future.h:88
void InternalWait()
Definition: Future.h:75
void InternalSet(const T &r)
Definition: Future.h:79
void Cancel()
Definition: Future.h:47
void InternalSet()
Definition: Future.h:134
virtual void Get()
Definition: Future.h:124
bool Done()
Definition: Future.h:42
virtual ~Future()
Definition: Future.h:96
virtual void Set(const T &r)
Definition: Future.h:63
bool IsCanceled()
Definition: Future.h:58
virtual ~Future()
Definition: Future.h:40
void InternalWait()
Definition: Future.h:130
virtual T Get()
Definition: Future.h:68
virtual void Set()
Definition: Future.h:119
PthreadCondition future_cond
Definition: Future.h:140
bool done
Definition: Future.h:87
PthreadCondition future_cond
Definition: Future.h:86
void Wait()
Definition: Future.h:53
PthreadMutex future_lock
Definition: Future.h:139
Automatic locking on the stack.
PthreadMutex future_lock
Definition: Future.h:85