QuantumLibrary
quantum_shared_state.h
1 /*
2 ** Copyright 2018 Bloomberg Finance L.P.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 #ifndef BLOOMBERG_QUANTUM_SHARED_STATE_MUTEX_H
17 #define BLOOMBERG_QUANTUM_SHARED_STATE_MUTEX_H
18 
19 #include <memory>
20 #include <exception>
21 #include <quantum/quantum_traits.h>
22 #include <quantum/quantum_future_state.h>
23 #include <quantum/quantum_yielding_thread.h>
24 #include <quantum/interface/quantum_icontext.h>
25 #include <quantum/quantum_condition_variable.h>
26 #include <quantum/quantum_buffer.h>
27 
28 namespace Bloomberg {
29 namespace quantum {
30 
31 //==============================================================================================
32 // class SharedState
33 //==============================================================================================
37 template <class T>
39 {
40  friend class Promise<T>;
41 
42 public:
43  template <class V = T>
44  int set(V&& value);
45 
46  template <class V = T>
47  int set(ICoroSync::Ptr sync, V&& value);
48 
49  //Moves value out of the shared state
50  T get();
51 
52  T get(ICoroSync::Ptr sync);
53 
54  const T& getRef() const;
55 
56  const T& getRef(ICoroSync::Ptr sync) const;
57 
58  void breakPromise();
59 
60  void wait() const;
61 
62  void wait(ICoroSync::Ptr sync) const;
63 
64  template<class REP, class PERIOD>
65  std::future_status waitFor(const std::chrono::duration<REP, PERIOD> &time) const;
66 
67  template<class REP, class PERIOD>
68  std::future_status waitFor(ICoroSync::Ptr sync,
69  const std::chrono::duration<REP, PERIOD> &time) const;
70 
71  int setException(std::exception_ptr ex);
72 
74  std::exception_ptr ex);
75 private:
76  SharedState();
77 
78  void conditionWait() const;
79 
80  void conditionWait(ICoroSync::Ptr sync) const;
81 
82  void checkPromiseState() const;
83 
84  bool stateHasChanged() const;
85 
86  // ============================= MEMBERS ==============================
87  mutable ConditionVariable _cond;
88  mutable Mutex _mutex;
89  FutureState _state;
90  std::exception_ptr _exception;
91  T _value;
92 };
93 
94 //==============================================================================================
95 // class SharedState<Buffer> (partial specialization)
96 //==============================================================================================
97 template <class T>
99 {
100  friend class Promise<Buffer<T>>;
101 
102 public:
103  template <class V = T>
104  void push(V&& value);
105 
106  template <class V = T>
107  void push(ICoroSync::Ptr sync, V&& value);
108 
109  T pull(bool& isBufferClosed);
110 
111  T pull(ICoroSync::Ptr sync, bool& isBufferClosed);
112 
113  void breakPromise();
114 
115  void wait() const;
116 
117  void wait(ICoroSync::Ptr sync) const;
118 
119  template<class REP, class PERIOD>
120  std::future_status waitFor(const std::chrono::duration<REP, PERIOD> &time) const;
121 
122  template<class REP, class PERIOD>
123  std::future_status waitFor(ICoroSync::Ptr sync,
124  const std::chrono::duration<REP, PERIOD> &time) const;
125 
126  int setException(std::exception_ptr ex);
127 
128  int setException(ICoroSync::Ptr sync,
129  std::exception_ptr ex);
130 
131  int closeBuffer();
132 private:
133  SharedState();
134 
135  void checkPromiseState() const;
136 
137  bool stateHasChanged(BufferStatus status) const;
138 
139  // ============================= MEMBERS ==============================
140  mutable ConditionVariable _cond;
141  mutable Mutex _mutex;
142  FutureState _state;
143  std::exception_ptr _exception;
144  Buffer<T> _reader;
145  Buffer<T> _writer;
146 };
147 
148 }}
149 
150 #include <quantum/impl/quantum_shared_state_impl.h>
151 
152 #endif //BLOOMBERG_QUANTUM_SHARED_STATE_MUTEX_H
int setException(std::exception_ptr ex)
Definition: quantum_shared_state_impl.h:172
Definition: quantum_buffer_impl.h:22
Shared state used between a Promise and a Future to exchange values.
Definition: quantum_shared_state.h:38
std::shared_ptr< ICoroSync > Ptr
Definition: quantum_icoro_sync.h:36
Class representing a promised value.
Definition: quantum_icoro_promise.h:77
void breakPromise()
Definition: quantum_shared_state_impl.h:110
std::future_status waitFor(const std::chrono::duration< REP, PERIOD > &time) const
Definition: quantum_shared_state_impl.h:146
void wait() const
Definition: quantum_shared_state_impl.h:123
int set(V &&value)
Definition: quantum_shared_state_impl.h:37
This class represents a coroutine-compatible implementation of the std::condition_variable....
Definition: quantum_condition_variable.h:36
Container which allows buffered access to a series of values. Values are pushed-in (written) by a pro...
Definition: quantum_buffer.h:49
FutureState
Represents the internal state of a future/promise pair. Modeled after std::future_errc.
Definition: quantum_future_state.h:31
T get()
Definition: quantum_shared_state_impl.h:72
BufferStatus
Defines the result of the operation on the buffer object.
Definition: quantum_buffer.h:32
const T & getRef() const
Definition: quantum_shared_state_impl.h:82