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 "task.h" 29 30 namespace ffrt { 31 enum queue_type { 32 queue_serial = ffrt_queue_serial, 33 queue_concurrent = ffrt_queue_concurrent, 34 queue_max = ffrt_queue_max, 35 }; 36 37 class queue_attr : public ffrt_queue_attr_t { 38 public: queue_attr()39 queue_attr() 40 { 41 ffrt_queue_attr_init(this); 42 } 43 ~queue_attr()44 ~queue_attr() 45 { 46 ffrt_queue_attr_destroy(this); 47 } 48 49 queue_attr(const queue_attr&) = delete; 50 queue_attr& operator=(const queue_attr&) = delete; 51 52 /** 53 * @brief Sets the QoS for this queue attribute. 54 * 55 * @param attr Indicates the QoS. 56 * @since 10 57 * @version 1.0 58 */ qos(qos qos_)59 inline queue_attr& qos(qos qos_) 60 { 61 ffrt_queue_attr_set_qos(this, qos_); 62 return *this; 63 } 64 65 // get qos qos()66 inline int qos() const 67 { 68 return ffrt_queue_attr_get_qos(this); 69 } 70 71 // set timeout timeout(uint64_t timeout_us)72 inline queue_attr& timeout(uint64_t timeout_us) 73 { 74 ffrt_queue_attr_set_timeout(this, timeout_us); 75 return *this; 76 } 77 78 // get timeout timeout()79 inline uint64_t timeout() const 80 { 81 return ffrt_queue_attr_get_timeout(this); 82 } 83 84 // set timeout callback callback(const std::function<void ()> & func)85 inline queue_attr& callback(const std::function<void()>& func) 86 { 87 ffrt_queue_attr_set_callback(this, create_function_wrapper(func, ffrt_function_kind_queue)); 88 return *this; 89 } 90 91 // get timeout callback callback()92 inline ffrt_function_header_t* callback() const 93 { 94 return ffrt_queue_attr_get_callback(this); 95 } 96 97 // set max concurrency of queue max_concurrency(const int max_concurrency)98 inline queue_attr& max_concurrency(const int max_concurrency) 99 { 100 ffrt_queue_attr_set_max_concurrency(this, max_concurrency); 101 return *this; 102 } 103 104 // get max concurrency of queue max_concurrency()105 inline int max_concurrency() const 106 { 107 return ffrt_queue_attr_get_max_concurrency(this); 108 } 109 }; 110 111 class queue { 112 public: 113 queue(const queue_type type, const char* name, const queue_attr& attr = {}) 114 { 115 queue_handle = ffrt_queue_create(ffrt_queue_type_t(type), name, &attr); 116 deleter = ffrt_queue_destroy; 117 } 118 119 queue(const char* name, const queue_attr& attr = {}) 120 { 121 queue_handle = ffrt_queue_create(ffrt_queue_serial, name, &attr); 122 deleter = ffrt_queue_destroy; 123 } 124 ~queue()125 ~queue() 126 { 127 if (deleter) { 128 deleter(queue_handle); 129 } 130 } 131 132 queue(queue const&) = delete; 133 void operator=(queue const&) = delete; 134 135 /** 136 * @brief Submits a task with a specified attribute to this queue. 137 * 138 * @param func Indicates a task executor function closure. 139 * @param attr Indicates a task attribute. 140 * @since 10 141 * @version 1.0 142 */ 143 inline void submit(const std::function<void()>& func, const task_attr& attr = {}) 144 { 145 ffrt_queue_submit(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); 146 } 147 148 /** 149 * @brief Submits a task with a specified attribute to this queue. 150 * 151 * @param func Indicates a task executor function closure. 152 * @param attr Indicates a task attribute. 153 * @since 10 154 * @version 1.0 155 */ 156 inline void submit(std::function<void()>&& func, const task_attr& attr = {}) 157 { 158 ffrt_queue_submit(queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); 159 } 160 161 /** 162 * @brief Submits a task with a specified attribute to this queue, and obtains a task handle. 163 * 164 * @param func Indicates a task executor function closure. 165 * @param attr Indicates a task attribute. 166 * @return Returns a non-null task handle if the task is submitted; 167 returns a null pointer otherwise. 168 * @since 10 169 * @version 1.0 170 */ 171 inline task_handle submit_h(const std::function<void()>& func, const task_attr& attr = {}) 172 { 173 return ffrt_queue_submit_h(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); 174 } 175 176 /** 177 * @brief Submits a task with a specified attribute to this queue, and obtains a task handle. 178 * 179 * @param func Indicates a task executor function closure. 180 * @param attr Indicates a task attribute. 181 * @return Returns a non-null task handle if the task is submitted; 182 returns a null pointer otherwise. 183 * @since 10 184 * @version 1.0 185 */ 186 inline task_handle submit_h(std::function<void()>&& func, const task_attr& attr = {}) 187 { 188 return ffrt_queue_submit_h( 189 queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); 190 } 191 192 /** 193 * @brief Submits a task with a specified attribute to this queue. 194 * 195 * @param func Indicates a task executor function closure. 196 * @param attr Indicates a task attribute. 197 * @since 10 198 * @version 1.0 199 */ 200 inline void submit_head(const std::function<void()>& func, const task_attr& attr = {}) 201 { 202 ffrt_queue_submit_head(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); 203 } 204 205 /** 206 * @brief Submits a task with a specified attribute to this queue. 207 * 208 * @param func Indicates a task executor function closure. 209 * @param attr Indicates a task attribute. 210 * @since 10 211 * @version 1.0 212 */ 213 inline void submit_head(std::function<void()>&& func, const task_attr& attr = {}) 214 { 215 ffrt_queue_submit_head(queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); 216 } 217 218 /** 219 * @brief Submits a task with a specified attribute to this queue, and obtains a task handle. 220 * 221 * @param func Indicates a task executor function closure. 222 * @param attr Indicates a task attribute. 223 * @return Returns a non-null task handle if the task is submitted; 224 returns a null pointer otherwise. 225 * @since 10 226 * @version 1.0 227 */ 228 inline task_handle submit_head_h(const std::function<void()>& func, const task_attr& attr = {}) 229 { 230 return ffrt_queue_submit_head_h(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); 231 } 232 233 /** 234 * @brief Submits a task with a specified attribute to this queue, and obtains a task handle. 235 * 236 * @param func Indicates a task executor function closure. 237 * @param attr Indicates a task attribute. 238 * @return Returns a non-null task handle if the task is submitted; 239 returns a null pointer otherwise. 240 * @since 10 241 * @version 1.0 242 */ 243 inline task_handle submit_head_h(std::function<void()>&& func, const task_attr& attr = {}) 244 { 245 return ffrt_queue_submit_head_h( 246 queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); 247 } 248 249 /** 250 * @brief Cancels a task. 251 * 252 * @param handle Indicates a task handle. 253 * @return Returns <b>0</b> if the task is canceled; 254 returns <b>-1</b> otherwise. 255 * @since 10 256 * @version 1.0 257 */ cancel(const task_handle & handle)258 inline int cancel(const task_handle& handle) 259 { 260 return ffrt_queue_cancel(handle); 261 } 262 263 /** 264 * @brief Waits until a task is complete. 265 * 266 * @param handle Indicates a task handle. 267 * @since 10 268 * @version 1.0 269 */ wait(const task_handle & handle)270 inline void wait(const task_handle& handle) 271 { 272 return ffrt_queue_wait(handle); 273 } 274 275 /** 276 * @brief Get queue task count. 277 * 278 * @param queue Indicates a queue handle. 279 * @return Returns the queue task count. 280 * @since 10 281 * @version 1.0 282 */ get_task_cnt()283 inline uint64_t get_task_cnt() 284 { 285 return ffrt_queue_get_task_cnt(queue_handle); 286 } 287 288 /** 289 * @brief Get application main thread queue. 290 * 291 * @return Returns application main thread queue. 292 * @since 12 293 * @version 1.0 294 */ get_main_queue()295 static inline queue* get_main_queue() 296 { 297 ffrt_queue_t q = ffrt_get_main_queue(); 298 // corner case: main queue is not ready. 299 if (q == nullptr) { 300 return nullptr; 301 } 302 static queue main_queue(q); 303 return &main_queue; 304 } 305 306 private: 307 using QueueDeleter = void (*)(ffrt_queue_t); 308 queue_handle(queue_handle)309 queue(ffrt_queue_t queue_handle, QueueDeleter deleter = nullptr) : queue_handle(queue_handle), deleter(deleter) {} 310 311 ffrt_queue_t queue_handle = nullptr; 312 QueueDeleter deleter = nullptr; 313 }; 314 } // namespace ffrt 315 316 #endif // FFRT_API_CPP_QUEUE_H