1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @file queue.h 18 * 19 * @brief Declares the queue interfaces in C++. 20 * 21 * @since 10 22 * @version 1.0 23 */ 24 #ifndef FFRT_API_CPP_QUEUE_H 25 #define FFRT_API_CPP_QUEUE_H 26 27 #include "c/queue.h" 28 #include "cpp/task.h" 29 30 namespace ffrt { 31 class queue_attr : public ffrt_queue_attr_t { 32 public: queue_attr()33 queue_attr() 34 { 35 ffrt_queue_attr_init(this); 36 } 37 ~queue_attr()38 ~queue_attr() 39 { 40 ffrt_queue_attr_destroy(this); 41 } 42 43 queue_attr(const queue_attr&) = delete; 44 queue_attr& operator=(const queue_attr&) = delete; 45 46 /** 47 * @brief Sets the QoS for this queue attribute. 48 * 49 * @param attr Indicates the QoS. 50 * @since 10 51 * @version 1.0 52 */ qos(qos qos_)53 inline queue_attr& qos(qos qos_) 54 { 55 ffrt_queue_attr_set_qos(this, qos_); 56 return *this; 57 } 58 59 // get qos qos()60 inline int qos() const 61 { 62 return ffrt_queue_attr_get_qos(this); 63 } 64 65 // set timeout timeout(uint64_t timeout_us)66 inline queue_attr& timeout(uint64_t timeout_us) 67 { 68 ffrt_queue_attr_set_timeout(this, timeout_us); 69 return *this; 70 } 71 72 // get timeout timeout()73 inline uint64_t timeout() const 74 { 75 return ffrt_queue_attr_get_timeout(this); 76 } 77 78 // set timeout callback callback(std::function<void ()> & func)79 inline queue_attr& callback(std::function<void()>& func) 80 { 81 ffrt_queue_attr_set_callback(this, create_function_wrapper(func, ffrt_function_kind_queue)); 82 return *this; 83 } 84 85 // get timeout callback callback()86 inline ffrt_function_header_t* callback() const 87 { 88 return ffrt_queue_attr_get_callback(this); 89 } 90 }; 91 92 class queue { 93 public: 94 queue(const char* name, const queue_attr& attr = {}) 95 { 96 queue_handle = ffrt_queue_create(ffrt_queue_serial, name, &attr); 97 } 98 ~queue()99 ~queue() 100 { 101 ffrt_queue_destroy(queue_handle); 102 } 103 104 queue(queue const&) = delete; 105 void operator=(queue const&) = delete; 106 107 /** 108 * @brief Submits a task to this queue. 109 * 110 * @param func Indicates a task executor function closure. 111 * @since 10 112 * @version 1.0 113 */ submit(std::function<void ()> & func)114 inline void submit(std::function<void()>& func) 115 { 116 ffrt_queue_submit(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), nullptr); 117 } 118 119 /** 120 * @brief Submits a task with a specified attribute to this queue. 121 * 122 * @param func Indicates a task executor function closure. 123 * @param attr Indicates a task attribute. 124 * @since 10 125 * @version 1.0 126 */ submit(std::function<void ()> & func,const task_attr & attr)127 inline void submit(std::function<void()>& func, const task_attr& attr) 128 { 129 ffrt_queue_submit(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); 130 } 131 132 /** 133 * @brief Submits a task to this queue. 134 * 135 * @param func Indicates a task executor function closure. 136 * @since 10 137 * @version 1.0 138 */ submit(std::function<void ()> && func)139 inline void submit(std::function<void()>&& func) 140 { 141 ffrt_queue_submit(queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), nullptr); 142 } 143 144 /** 145 * @brief Submits a task with a specified attribute to this queue. 146 * 147 * @param func Indicates a task executor function closure. 148 * @param attr Indicates a task attribute. 149 * @since 10 150 * @version 1.0 151 */ submit(std::function<void ()> && func,const task_attr & attr)152 inline void submit(std::function<void()>&& func, const task_attr& attr) 153 { 154 ffrt_queue_submit(queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); 155 } 156 157 /** 158 * @brief Submits a task to this queue, and obtains a task handle. 159 * 160 * @param func Indicates a task executor function closure. 161 * @return Returns a non-null task handle if the task is submitted; 162 returns a null pointer otherwise. 163 * @since 10 164 * @version 1.0 165 */ submit_h(std::function<void ()> & func)166 inline task_handle submit_h(std::function<void()>& func) 167 { 168 return ffrt_queue_submit_h(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), nullptr); 169 } 170 171 /** 172 * @brief Submits a task with a specified attribute to this queue, and obtains a task handle. 173 * 174 * @param func Indicates a task executor function closure. 175 * @param attr Indicates a task attribute. 176 * @return Returns a non-null task handle if the task is submitted; 177 returns a null pointer otherwise. 178 * @since 10 179 * @version 1.0 180 */ submit_h(std::function<void ()> & func,const task_attr & attr)181 inline task_handle submit_h(std::function<void()>& func, const task_attr& attr) 182 { 183 return ffrt_queue_submit_h(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); 184 } 185 186 /** 187 * @brief Submits a task to this queue, and obtains a task handle. 188 * 189 * @param func Indicates a task executor function closure. 190 * @return Returns a non-null task handle if the task is submitted; 191 returns a null pointer otherwise. 192 * @since 10 193 * @version 1.0 194 */ submit_h(std::function<void ()> && func)195 inline task_handle submit_h(std::function<void()>&& func) 196 { 197 return ffrt_queue_submit_h( 198 queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), nullptr); 199 } 200 201 /** 202 * @brief Submits a task with a specified attribute to this queue, and obtains a task handle. 203 * 204 * @param func Indicates a task executor function closure. 205 * @param attr Indicates a task attribute. 206 * @return Returns a non-null task handle if the task is submitted; 207 returns a null pointer otherwise. 208 * @since 10 209 * @version 1.0 210 */ submit_h(std::function<void ()> && func,const task_attr & attr)211 inline task_handle submit_h(std::function<void()>&& func, const task_attr& attr) 212 { 213 return ffrt_queue_submit_h( 214 queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); 215 } 216 217 /** 218 * @brief Cancels a task. 219 * 220 * @param handle Indicates a task handle. 221 * @return Returns <b>0</b> if the task is canceled; 222 returns <b>-1</b> otherwise. 223 * @since 10 224 * @version 1.0 225 */ cancel(task_handle & handle)226 inline int cancel(task_handle& handle) 227 { 228 return ffrt_queue_cancel(handle); 229 } 230 231 /** 232 * @brief Waits until a task is complete. 233 * 234 * @param handle Indicates a task handle. 235 * @since 10 236 * @version 1.0 237 */ wait(task_handle & handle)238 inline void wait(task_handle& handle) 239 { 240 return ffrt_queue_wait(handle); 241 } 242 243 private: 244 ffrt_queue_t queue_handle = nullptr; 245 }; 246 } // namespace ffrt 247 248 #endif // FFRT_API_CPP_QUEUE_H