QuantumLibrary
quantum_io_task_impl.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 //NOTE: DO NOT INCLUDE DIRECTLY
17 
18 //##############################################################################################
19 //#################################### IMPLEMENTATIONS #########################################
20 //##############################################################################################
21 #include <quantum/quantum_allocator.h>
22 
23 namespace Bloomberg {
24 namespace quantum {
25 
26 #ifndef __QUANTUM_IO_TASK_ALLOC_SIZE
27  #define __QUANTUM_IO_TASK_ALLOC_SIZE __QUANTUM_DEFAULT_POOL_ALLOC_SIZE
28 #endif
29 #ifndef __QUANTUM_USE_DEFAULT_ALLOCATOR
30  #ifdef __QUANTUM_ALLOCATE_POOL_FROM_HEAP
31  using IoTaskAllocator = HeapAllocator<IoTask>;
32  #else
34  #endif
35 #else
37 #endif
38 
39 template <class RET, class FUNC, class ... ARGS>
40 IoTask::IoTask(std::shared_ptr<Promise<RET>> promise,
41  FUNC&& func,
42  ARGS&&... args) :
43  _func(Util::bindIoCaller(promise,
44  std::forward<FUNC>(func),
45  std::forward<ARGS>(args)...)),
46  _terminated ATOMIC_FLAG_INIT,
47  _queueId((int)IQueue::QueueId::Any),
48  _isHighPriority(false)
49 {
50 }
51 
52 template <class RET, class FUNC, class ... ARGS>
53 IoTask::IoTask(std::shared_ptr<Promise<RET>> promise,
54  int queueId,
55  bool isHighPriority,
56  FUNC&& func,
57  ARGS&&... args) :
58  _func(Util::bindIoCaller(promise,
59  std::forward<FUNC>(func),
60  std::forward<ARGS>(args)...)),
61  _terminated ATOMIC_FLAG_INIT,
62  _queueId(queueId),
63  _isHighPriority(isHighPriority)
64 {
65 }
66 
67 inline
69 {
70  terminate();
71 }
72 
73 inline
75 {
76  if (!_terminated.test_and_set())
77  {
78  //not used
79  }
80 }
81 
82 inline
84 {
85  return _func ? _func() : (int)ITask::RetCode::NotCallable;
86 }
87 
88 inline
89 void IoTask::setQueueId(int queueId)
90 {
91  _queueId = queueId;
92 }
93 
94 inline
96 {
97  return _queueId;
98 }
99 
100 inline
102 {
103  return ITask::Type::IO;
104 }
105 
106 inline
107 bool IoTask::isBlocked() const
108 {
109  return false;
110 }
111 
112 inline
114 {
115  return false;
116 }
117 
118 inline
120 {
121  return _isHighPriority;
122 }
123 
124 inline
125 void* IoTask::operator new(size_t)
126 {
128 }
129 
130 inline
131 void IoTask::operator delete(void* p)
132 {
133  Allocator<IoTaskAllocator>::instance(AllocatorTraits::ioTaskAllocSize()).deallocate(static_cast<IoTask*>(p));
134 }
135 
136 inline
138 {
139 #ifndef __QUANTUM_USE_DEFAULT_ALLOCATOR
141 #else
142  delete p;
143 #endif
144 }
145 
146 }}
147 
static size_type & ioTaskAllocSize()
Get/set if the default size for IO task object pools.
Definition: quantum_allocator_traits.h:154
static AllocType & instance(std::enable_if_t<!A::default_constructor::value, uint16_t > size)
Definition: quantum_allocator.h:56
Type getType() const final
Definition: quantum_io_task_impl.h:101
Long running or blocking task running in the IO thread pool.
Definition: quantum_io_task.h:34
Definition: quantum_buffer_impl.h:22
bool isHighPriority() const final
Definition: quantum_io_task_impl.h:119
Definition: quantum_allocator.h:36
bool isBlocked() const final
Definition: quantum_io_task_impl.h:107
Utility to bind a user callable function unto a coroutine or an IO task.
Definition: quantum_util.h:45
Definition: quantum_stl_impl.h:23
int run() final
Definition: quantum_io_task_impl.h:83
Type
Definition: quantum_itask.h:37
Class representing a promised value.
Definition: quantum_icoro_promise.h:77
~IoTask()
Definition: quantum_io_task_impl.h:68
static void deleter(IoTask *p)
Definition: quantum_io_task_impl.h:137
bool isSleeping(bool updateTimer=false) final
Definition: quantum_io_task_impl.h:113
Provides a stack-based object pool to the underlying ContiguousPoolManager. The default buffer size i...
Definition: quantum_stack_allocator.h:34
Interface to a task queue. For internal use only.
Definition: quantum_iqueue.h:33
void setQueueId(int queueId) final
Definition: quantum_io_task_impl.h:89
IoTask(std::shared_ptr< Promise< RET >> promise, FUNC &&func, ARGS &&... args)
Definition: quantum_io_task_impl.h:40
StackAllocator< IoTask, __QUANTUM_IO_TASK_ALLOC_SIZE > IoTaskAllocator
Definition: quantum_io_task_impl.h:33
void terminate() final
Terminates the object.
Definition: quantum_io_task_impl.h:74
int getQueueId() final
Definition: quantum_io_task_impl.h:95