SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FutureValue.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 
31 ///
32 ///@file FutureValue.h
33 ///@brief Delayed return values
34 ///
35 ///@author Steve Parker
36 /// Department of Computer Science
37 /// University of Utah
38 ///@date June 1997
39 ///
40 
41 #ifndef Core_Thread_FutureValue_h
42 #define Core_Thread_FutureValue_h
43 
44 #include <Core/Thread/Legacy/Legacy/Semaphore.h>
45 #include <Core/Thread/Legacy/Legacy/Thread.h>
46 
47 namespace SCIRun {
48 /**************************************
49 
50 @class
51  FutureValue
52 
53  KEYWORDS
54  Thread
55 
56 @details
57  Creates a single slot for some return value. The <i>wait</i> method
58  waits for a value to be sent from another thread via the <i>reply</i>
59  method. This is typically used to provide a simple means of returning
60  data from a server thread. An <b>FutureValue</b> object is created on the
61  stack, and some request is sent (usually via a <b>Mailbox</b>) to a server
62  thread. Then the thread will block in <i>wait</i> until the server thread
63  receives the message and responds using <i>reply</i>.
64 
65  <p><b>FutureValue</b> is a one-shot wait/reply pair - a new
66  <b>FutureValue</b> object must be created for each reply, and these are
67  typically created on the stack. Only a single thread should
68  call <i>wait</i> and a single thread shuold call <i>reply</i>.
69 
70 ****************************************/
71 template<class Item> class FutureValue {
72 public:
73  //////////
74  /// Create the FutureValue object. <i>name</i> should be a
75  /// static string which describes the primitive for debugging
76  /// purposes.
77  FutureValue(const char* name);
78 
79  //////////
80  /// Destroy the object
81  ~FutureValue();
82 
83  //////////
84  /// Wait until the reply is sent by anothe thread, then return
85  /// that reply.
86  Item receive();
87 
88  //////////
89  /// Send the reply to the waiting thread.
90  void send(const Item& reply);
91 
92 private:
93  const char* name;
94  Item value;
95  Semaphore sema;
96 
97  // Cannot copy them
99  FutureValue<Item>& operator=(const FutureValue<Item>&);
100 };
101 
102 template<class Item>
104  : name(name), sema("FutureValue semaphore", 0)
105 {
106 }
107 
108 template<class Item>
110 {
111 }
112 
113 template<class Item>
114 Item
116 {
117  int s=Thread::couldBlock(name);
118  sema.down();
120  return value;
121 } // End namespace SCIRun
122 
123 template<class Item>
124 void
125 FutureValue<Item>::send(const Item& reply)
126 {
127  value=reply;
128  sema.up();
129 }
130 } // End namespace SCIRun
131 
132 #endif
133 
Item receive()
Definition: FutureValue.h:115
~FutureValue()
Destroy the object.
Definition: FutureValue.h:109
static void couldBlockDone(int restore)
const char * name[]
Definition: BoostGraphExampleTests.cc:87
void send(const Item &reply)
Send the reply to the waiting thread.
Definition: FutureValue.h:125
Definition: Semaphore.h:67
FutureValue(const char *name)
Definition: FutureValue.h:103
static int couldBlock(const char *why)
Definition: FutureValue.h:71