16 #ifndef BLOOMBERG_QUANTUM_CONTEXT_H 17 #define BLOOMBERG_QUANTUM_CONTEXT_H 19 #include <quantum/quantum_promise.h> 20 #include <quantum/quantum_task.h> 21 #include <quantum/quantum_io_task.h> 22 #include <quantum/quantum_dispatcher_core.h> 23 #include <quantum/quantum_traits.h> 36 class Context :
public IThreadContext<RET>,
37 public ICoroContext<RET>,
39 public std::enable_shared_from_this<Context<RET>>
44 template <
class OTHER_RET>
friend class Context;
47 using Ptr = std::shared_ptr<Context<RET>>;
67 bool isSleeping(
bool updateTimer = false) final;
72 bool valid() const final;
73 bool validAt(
int num) const final;
79 void waitAt(
int num) const final;
80 std::future_status
waitForAt(
int num,
std::chrono::milliseconds timeMs) const final;
81 void wait() const final;
82 std::future_status
waitFor(
std::chrono::milliseconds timeMs) const final;
88 template <
class V = RET>
90 template <
class V = RET>
96 template <
class V = RET>
98 template <
class OTHER_RET>
100 template <
class OTHER_RET>
115 template <
class V = RET>
117 template <
class V = RET>
123 template <
class V = RET>
125 template <
class OTHER_RET>
127 template <
class OTHER_RET>
129 template <
class OTHER_RET>
131 template <
class OTHER_RET>
141 void sleep(const
std::chrono::milliseconds& timeMs) final;
142 void sleep(const
std::chrono::microseconds& timeUs) final;
159 post(FUNC&& func, ARGS&&... args);
163 post(
int queueId,
bool isHighPriority, FUNC&& func, ARGS&&... args);
171 postFirst(
int queueId,
bool isHighPriority, FUNC&& func, ARGS&&... args);
175 then(FUNC&& func, ARGS&&... args);
179 onError(FUNC&& func, ARGS&&... args);
183 finally(FUNC&& func, ARGS&&... args);
196 postAsyncIo(
int queueId,
bool isHighPriority, FUNC&& func, ARGS&&... args);
203 forEach(INPUT_IT first, INPUT_IT last,
Functions::ForEachFunc<OTHER_RET, INPUT_IT> func);
207 forEach(INPUT_IT first,
size_t num,
Functions::ForEachFunc<OTHER_RET, INPUT_IT> func);
228 Functions::MapFunc<KEY, MAPPED_TYPE, INPUT_IT> mapper,
229 Functions::ReduceFunc<KEY, MAPPED_TYPE, REDUCED_TYPE> reducer);
238 Functions::MapFunc<KEY, MAPPED_TYPE, INPUT_IT> mapper,
239 Functions::ReduceFunc<KEY, MAPPED_TYPE, REDUCED_TYPE> reducer);
249 Functions::MapFunc<KEY, MAPPED_TYPE, INPUT_IT> mapper,
250 Functions::ReduceFunc<KEY, MAPPED_TYPE, REDUCED_TYPE> reducer);
259 Functions::MapFunc<KEY, MAPPED_TYPE, INPUT_IT> mapper,
260 Functions::ReduceFunc<KEY, MAPPED_TYPE, REDUCED_TYPE> reducer);
265 static
void* operator new(
size_t size);
266 static
void operator delete(
void* p);
272 template <
class OTHER_RET>
279 thenImpl(
ITask::Type type, FUNC&& func, ARGS&&... args);
283 postImpl(
int queueId,
bool isHighPriority,
ITask::Type type, FUNC&& func, ARGS&&... args);
287 postAsyncIoImpl(
int queueId,
bool isHighPriority, FUNC&& func, ARGS&&... args);
289 int index(
int num) const;
291 void validateTaskType(
ITask::Type type) const;
299 std::atomic_flag _terminated;
300 std::atomic_int _signal;
302 std::chrono::microseconds _sleepDuration;
303 std::chrono::high_resolution_clock::time_point _sleepTimestamp;
311 #include <quantum/interface/quantum_icontext.h> 312 #include <quantum/impl/quantum_context_impl.h> 314 #endif //BLOOMBERG_QUANTUM_CONTEXT_H BufferRetType< V > pull(bool &isBufferClosed)
Pull a single value from the future buffer.
Definition: quantum_context_impl.h:868
Context< std::map< KEY, REDUCED_TYPE > >::Ptr mapReduceBatch(INPUT_IT first, INPUT_IT last, Functions::MapFunc< KEY, MAPPED_TYPE, INPUT_IT > mapper, Functions::ReduceFunc< KEY, MAPPED_TYPE, REDUCED_TYPE > reducer)
This version of mapReduce() runs both the mapper and the reducer functions in batches for improved pe...
Definition: quantum_buffer_impl.h:22
std::enable_if_t< Traits::IsBuffer< T >::value &&!std::is_same< std::decay_t< V >, T >::value &&std::is_convertible< std::decay_t< V >, typename Traits::IsBuffer< T >::Type >::value > BufferType
Definition: quantum_traits.h:89
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:1027
typename ICoroFuture< T >::Ptr CoroFuturePtr
Definition: quantum_icoro_future.h:72
bool valid() const final
Determines if the future object associated with this context has a valid shared state with the corres...
Definition: quantum_context_impl.h:474
std::shared_ptr< IContextBase > Ptr
Definition: quantum_icontext_base.h:35
std::enable_if_t<!Traits::IsBuffer< T >::value &&std::is_convertible< std::decay_t< V >, T >::value > NonBufferType
Definition: quantum_traits.h:91
CoroFuturePtr< OTHER_RET > postAsyncIo(FUNC &&func, ARGS &&... args)
Posts an IO method to run asynchronously on the IO thread pool.
Definition: quantum_context_impl.h:697
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:896
Exposes methods to manipulate the coroutine context.
Definition: quantum_icoro_context.h:38
Class implementing the dispatching logic unto worker threads. Used for both coroutines and IO tasks.
Definition: quantum_dispatcher_core.h:45
Utility to bind a user callable function unto a coroutine or an IO task.
Definition: quantum_util.h:45
~Context()
Definition: quantum_context_impl.h:450
Context< std::vector< OTHER_RET > >::Ptr forEach(INPUT_IT first, INPUT_IT last, Functions::ForEachFunc< OTHER_RET, INPUT_IT > func)
Applies the given unary function to all the elements in the range [first,last). This function runs in...
Provides an interface to facilitate 'implicit' coroutine yielding within other primitives such as mut...
Definition: quantum_icoro_sync.h:34
Definition: quantum_stl_impl.h:23
void sleep(const std::chrono::milliseconds &timeMs) final
Sleeps the coroutine associated with this context for at least 'timeMs' milliseconds or 'timeUs' micr...
Definition: quantum_context_impl.h:618
ITask::Ptr getTask() const final
Definition: quantum_context_impl.h:587
Context< OTHER_RET >::Ptr postFirst(FUNC &&func, ARGS &&... args)
Posts a coroutine to run asynchronously.
int set(V &&value)
Set the promised value associated with this context.
Definition: quantum_context_impl.h:847
Runnable object representing a coroutine.
Definition: quantum_task.h:40
std::shared_ptr< Context< RET > > Ptr
Definition: quantum_context.h:47
void waitAt(int num) const final
Waits for the future in the 'num-th' continuation context to be ready.
Definition: quantum_context_impl.h:916
Parallel execution engine used to run coroutines or IO tasks asynchronously. This class is the main e...
Definition: quantum_dispatcher.h:34
std::enable_if_t<!Traits::IsBuffer< T >::value, typename Traits::IsBuffer< T >::Type > NonBufferRetType
Definition: quantum_traits.h:95
Context< OTHER_RET >::Ptr finally(FUNC &&func, ARGS &&... args)
Posts a coroutine to run asynchronously. This coroutine is always guaranteed to run.
bool isBlocked() const final
Definition: quantum_context_impl.h:486
typename Context< RET >::Ptr ContextPtr
Definition: quantum_context.h:307
Concrete class representing a coroutine or a thread context.
Definition: quantum_icoro_context.h:29
void waitAll() const final
Wait for all the futures in the continuation chain to be ready.
Definition: quantum_context_impl.h:940
int getNumIoThreads() const
Returns the number of underlying IO threads as specified in the dispatcher constructor.
Definition: quantum_context_impl.h:1021
This interface exposes shared functionality between IThreadContext and ICoroContext.
Definition: quantum_icontext_base.h:33
void terminate() final
Terminates the object.
Definition: quantum_context_impl.h:456
void setYieldHandle(Traits::Yield &yield) final
Sets the underlying boost::coroutine object so that it can be yielded on.
Definition: quantum_context_impl.h:593
Context< std::map< KEY, REDUCED_TYPE > >::Ptr mapReduce(INPUT_IT first, INPUT_IT last, Functions::MapFunc< KEY, MAPPED_TYPE, INPUT_IT > mapper, Functions::ReduceFunc< KEY, MAPPED_TYPE, REDUCED_TYPE > reducer)
Implementation of map-reduce functionality.
void yield() final
Explicitly yields this coroutine context.
Definition: quantum_context_impl.h:606
Context< std::vector< std::vector< OTHER_RET > > >::Ptr forEachBatch(INPUT_IT first, INPUT_IT last, Functions::ForEachFunc< OTHER_RET, INPUT_IT > func)
Applies the given unary function to all the elements in the range [first,last). This function runs se...
Context< OTHER_RET >::Ptr onError(FUNC &&func, ARGS &&... args)
Posts a coroutine to run asynchronously. This is the error handler for a continuation chain and acts ...
Ptr end()
This is the last method in a continuation chain.
Definition: quantum_context_impl.h:685
int getNumCoroutineThreads() const
Returns the number of underlying coroutine threads as specified in the dispatcher constructor....
Definition: quantum_context_impl.h:1015
const NonBufferRetType< OTHER_RET > & getPrevRef()
Get a reference to future value associated with the previous coroutine context in the continuation ch...
Definition: quantum_context_impl.h:161
void wait() const final
Waits for the future associated with this context to be ready.
Definition: quantum_context_impl.h:928
int setException(std::exception_ptr ex) final
Set an exception in the promise associated with the current IThreadContext or ICoroContext.
Definition: quantum_context_impl.h:480
static void deleter(Context< RET > *p)
Definition: quantum_context_impl.h:1148
bool validAt(int num) const final
Determines if the future object associated with the 'num'-th continuation context is still valid.
Definition: quantum_context_impl.h:468
std::enable_if_t< Traits::IsBuffer< T >::value, typename Traits::IsBuffer< T >::Type > BufferRetType
Definition: quantum_traits.h:93
NonBufferRetType< OTHER_RET > getPrev()
Get the future value associated with the previous coroutine context in the continuation chain.
Definition: quantum_context_impl.h:153
int closeBuffer()
Close a promise buffer.
Definition: quantum_context_impl.h:882
Context< OTHER_RET >::Ptr post(FUNC &&func, ARGS &&... args)
Post a coroutine to run asynchronously.
Exposes methods to manipulate and access a promise.
Definition: quantum_ipromise_base.h:32
std::future_status waitFor(std::chrono::milliseconds timeMs) const final
Waits for the future associated with this context to be ready for a maximum of 'timeMs' milliseconds.
Definition: quantum_context_impl.h:934
Exposes methods to manipulate the thread context.
Definition: quantum_ithread_context.h:37
bool isSleeping(bool updateTimer=false) final
Definition: quantum_context_impl.h:492
Interface to a task. For internal use only.
Definition: quantum_itask.h:32
std::atomic_int & signal() final
Accessor to the underlying synchronization variable.
Definition: quantum_context_impl.h:612
NonBufferRetType< V > get()
Get the future value associated with this context.
Definition: quantum_context_impl.h:903
Contains typedefs for various functions.
Definition: quantum_functions.h:31
NonBufferRetType< OTHER_RET > getAt(int num)
Get the future value from the 'num-th' continuation context.
Definition: quantum_context_impl.h:889
Configuration parameters for the Quantum library.
Traits::Yield & getYieldHandle() final
Retrieve the underlying boost::coroutine object.
Definition: quantum_context_impl.h:599
std::future_status waitForAt(int num, std::chrono::milliseconds timeMs) const final
Waits for the future in the 'num-th' continuation context to be ready for a maximum of 'timeMs' milli...
Definition: quantum_context_impl.h:922
Contains definitions for various traits used by this library. For internal use only.
Definition: quantum_traits.h:55
void push(V &&value)
Push a single value into the promise buffer.
Definition: quantum_context_impl.h:854
Context< OTHER_RET >::Ptr then(FUNC &&func, ARGS &&... args)
Posts a coroutine to run asynchronously.
void setTask(ITask::Ptr task) final
Definition: quantum_context_impl.h:581
const NonBufferRetType< V > & getRef() const
Get a reference the future value associated with this context.
Definition: quantum_context_impl.h:910