QuantumLibrary
quantum_io_task.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_IO_TASK_H
17 #define BLOOMBERG_QUANTUM_IO_TASK_H
18 
19 #include <functional>
20 #include <quantum/interface/quantum_itask.h>
21 #include <quantum/quantum_capture.h>
22 #include <quantum/quantum_promise.h>
23 #include <quantum/util/quantum_util.h>
24 
25 namespace Bloomberg {
26 namespace quantum {
27 
28 //==============================================================================================
29 // class IoTask
30 //==============================================================================================
34 class IoTask : public ITask
35 {
36 public:
37  using Ptr = std::shared_ptr<IoTask>;
38  using WeakPtr = std::weak_ptr<IoTask>;
39 
40  template <class RET, class FUNC, class ... ARGS>
41  IoTask(std::shared_ptr<Promise<RET>> promise,
42  FUNC&& func,
43  ARGS&&... args);
44 
45  template <class RET, class FUNC, class ... ARGS>
46  IoTask(std::shared_ptr<Promise<RET>> promise,
47  int queueId,
48  bool isHighPriority,
49  FUNC&& func,
50  ARGS&&... args);
51 
52  IoTask(const IoTask& task) = delete;
53  IoTask(IoTask&& task) = default;
54  IoTask& operator=(const IoTask& task) = delete;
55  IoTask& operator=(IoTask&& task) = default;
56 
57  ~IoTask();
58 
59  //ITerminate
60  void terminate() final;
61 
62  //ITask
63  int run() final;
64  void setQueueId(int queueId) final;
65  int getQueueId() final;
66  Type getType() const final;
67  bool isBlocked() const final;
68  bool isSleeping(bool updateTimer = false) final;
69  bool isHighPriority() const final;
70 
71  //===================================
72  // NEW / DELETE
73  //===================================
74  static void* operator new(size_t size);
75  static void operator delete(void* p);
76  static void deleter(IoTask* p);
77 
78 private:
79  Function<int()> _func; //the current runnable io function
80  std::atomic_flag _terminated;
81  int _queueId;
82  bool _isHighPriority;
83 };
84 
85 using IoTaskPtr = IoTask::Ptr;
87 
88 }}
89 
90 #include <quantum/impl/quantum_io_task_impl.h>
91 
92 #endif //BLOOMBERG_QUANTUM_IO_TASK_H
IoTask::Ptr IoTaskPtr
Definition: quantum_io_task.h:85
Type getType() const final
Definition: quantum_io_task_impl.h:101
std::weak_ptr< IoTask > WeakPtr
Definition: quantum_io_task.h:38
IoTask::WeakPtr IoTaskWeakPtr
Definition: quantum_io_task.h:86
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
bool isBlocked() const final
Definition: quantum_io_task_impl.h:107
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
std::shared_ptr< ITask > Ptr
Definition: quantum_itask.h:34
std::shared_ptr< IoTask > Ptr
Definition: quantum_io_task.h:37
std::weak_ptr< ITask > WeakPtr
Definition: quantum_itask.h:35
IoTask & operator=(const IoTask &task)=delete
Similar implementation to std::function except that it allows capture of non-copyable types.
Definition: quantum_capture.h:62
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
Interface to a task. For internal use only.
Definition: quantum_itask.h:32
void terminate() final
Terminates the object.
Definition: quantum_io_task_impl.h:74
int getQueueId() final
Definition: quantum_io_task_impl.h:95