QuantumLibrary
quantum_ithread_context.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_ITHREAD_CONTEXT_H
17 #define BLOOMBERG_QUANTUM_ITHREAD_CONTEXT_H
18 
19 #include <future>
20 #include <chrono>
21 #include <quantum/quantum_traits.h>
22 #include <quantum/interface/quantum_ithread_context_base.h>
23 
24 namespace Bloomberg {
25 namespace quantum {
26 
27 template <class RET>
28 class Context;
29 
30 //==============================================================================================
31 // interface IThreadContext
32 //==============================================================================================
36 template <class RET>
38 {
40  using Ptr = std::shared_ptr<IThreadContext<RET>>;
41  using Impl = Context<RET>;
42 
45  virtual void wait() const = 0;
46 
51  virtual std::future_status waitFor(std::chrono::milliseconds timeMs) const = 0;
52 
59  virtual void waitAt(int num) const = 0;
60 
69  virtual std::future_status waitForAt(int num, std::chrono::milliseconds timeMs) const = 0;
70 
73  virtual void waitAll() const = 0;
74 
78  template <class V = RET>
80 
85  template <class V = RET>
86  const NonBufferRetType<V>& getRef() const;
87 
97  template <class OTHER_RET>
99 
109  template <class OTHER_RET>
110  const NonBufferRetType<OTHER_RET>& getRefAt(int num) const;
111 
117  template <class V, class = NonBufferType<RET,V>>
118  int set(V&& value);
119 
126  template <class V, class = BufferType<RET,V>>
127  void push(V&& value);
128 
135  template <class V = RET>
136  BufferRetType<V> pull(bool& isBufferClosed);
137 
143  template <class V = RET, class = BufferRetType<V>>
144  int closeBuffer();
145 
151  int getNumCoroutineThreads() const;
152 
157  int getNumIoThreads() const;
158 
162  const std::pair<int, int>& getCoroQueueIdRangeForAny() const;
163 
164  //=========================================================================================
165  // TASK CONTINUATIONS
166  //=========================================================================================
175  //-----------------------------------------------------------------------------------------
176 
190  template <class OTHER_RET = int, class FUNC, class ... ARGS>
192  then(FUNC&& func, ARGS&&... args);
193 
210  template <class OTHER_RET = int, class FUNC, class ... ARGS>
212  onError(FUNC&& func, ARGS&&... args);
213 
226  template <class OTHER_RET = int, class FUNC, class ... ARGS>
228  finally(FUNC&& func, ARGS&&... args);
229 
235  Ptr end();
236 };
237 
238 template <class RET>
240 
241 template <class RET>
243 
244 }}
245 
246 #endif //BLOOMBERG_QUANTUM_ITHREAD_CONTEXT_H
Definition: quantum_buffer_impl.h:22
int getNumIoThreads() const
Returns the number of underlying IO threads as specified in the dispatcher constructor.
Definition: quantum_context_impl.h:92
std::shared_ptr< IContextBase > Ptr
Definition: quantum_icontext_base.h:35
virtual std::future_status waitFor(std::chrono::milliseconds timeMs) const =0
Waits for the future associated with this context to be ready for a maximum of 'timeMs' milliseconds.
virtual void waitAll() const =0
Wait for all the futures in the continuation chain to be ready.
virtual std::future_status waitForAt(int num, std::chrono::milliseconds timeMs) const =0
Waits for the future in the 'num-th' continuation context to be ready for a maximum of 'timeMs' milli...
const NonBufferRetType< OTHER_RET > & getRefAt(int num) const
Get a reference to the future value from the 'num-th' continuation context.
Definition: quantum_context_impl.h:52
const NonBufferRetType< V > & getRef() const
Get a reference the future value associated with this context.
Definition: quantum_context_impl.h:38
std::shared_ptr< IThreadContext< RET > > Ptr
Definition: quantum_ithread_context.h:40
Exposes methods to manipulate the thread context, especially future wait methods.
Definition: quantum_ithread_context_base.h:34
int closeBuffer()
Close a promise buffer.
Definition: quantum_context_impl.h:80
std::enable_if_t<!Traits::IsBuffer< T >::value, typename Traits::IsBuffer< T >::Type > NonBufferRetType
Definition: quantum_traits.h:95
const std::pair< int, int > & getCoroQueueIdRangeForAny() const
Gets the range [minQueueId, maxQueueId] of coroutine queueIds covered by IQueue::QueueId::Any by the ...
Definition: quantum_context_impl.h:98
virtual void waitAt(int num) const =0
Waits for the future in the 'num-th' continuation context to be ready.
Concrete class representing a coroutine or a thread context.
Definition: quantum_icoro_context.h:29
NonBufferRetType< V > get()
Get the future value associated with this context.
Definition: quantum_context_impl.h:31
IThreadContext< OTHER_RET >::Ptr then(FUNC &&func, ARGS &&... args)
Posts a function to run asynchronously.
virtual void wait() const =0
Waits for the future associated with this context to be ready.
Definition: quantum_icontext_base.h:26
void push(V &&value)
Push a single value into the promise buffer.
Definition: quantum_context_impl.h:66
NonBufferRetType< OTHER_RET > getAt(int num)
Get the future value from the 'num-th' continuation context.
Definition: quantum_context_impl.h:45
int set(V &&value)
Set the promised value associated with this context.
Definition: quantum_context_impl.h:59
IThreadContext< OTHER_RET >::Ptr onError(FUNC &&func, ARGS &&... args)
Posts a function to run asynchronously. This is the error handler for a continuation chain and acts a...
std::enable_if_t< Traits::IsBuffer< T >::value, typename Traits::IsBuffer< T >::Type > BufferRetType
Definition: quantum_traits.h:93
Exposes methods to manipulate the thread context.
Definition: quantum_ithread_context.h:37
Ptr end()
This is the last method in a continuation chain.
Definition: quantum_context_impl.h:129
int getNumCoroutineThreads() const
Returns the number of underlying coroutine threads as specified in the dispatcher constructor....
Definition: quantum_context_impl.h:86
typename IThreadContext< RET >::Ptr ThreadContextPtr
Definition: quantum_ithread_context.h:242
BufferRetType< V > pull(bool &isBufferClosed)
Pull a single value from the future buffer.
Definition: quantum_context_impl.h:73