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 * @addtogroup FFRT 18 * @{ 19 * 20 * @brief Provides FFRT C++ APIs. 21 * 22 * @since 10 23 */ 24 25 /** 26 * @file queue.h 27 * 28 * @brief Declares the queue interfaces in C++. 29 * 30 * @library libffrt.z.so 31 * @kit FunctionFlowRuntimeKit 32 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 33 * @since 10 34 */ 35 36 #ifndef FFRT_API_CPP_QUEUE_H 37 #define FFRT_API_CPP_QUEUE_H 38 39 #include "c/queue.h" 40 #include "task.h" 41 42 namespace ffrt { 43 /** 44 * @enum queue_type 45 * @brief Defines the types of queues supported. 46 * 47 * @since 12 48 */ 49 enum queue_type { 50 queue_serial = ffrt_queue_serial, ///< A serial queue that processes tasks sequentially. 51 queue_concurrent = ffrt_queue_concurrent, ///< A concurrent queue that processes tasks in parallel. 52 queue_max = ffrt_queue_max, ///< Defines the maximum type for validation. 53 }; 54 55 /** 56 * @class queue_attr 57 * @brief Represents attributes for configuring a queue. 58 * 59 * This class provides methods to set and retrieve queue attributes such as QoS, 60 * timeout values, callback functions, and maximum concurrency. 61 * 62 * @since 10 63 */ 64 class queue_attr : public ffrt_queue_attr_t { 65 public: 66 /** 67 * @brief Constructs a queue_attr object with default values. 68 * 69 * @since 10 70 */ queue_attr()71 queue_attr() 72 { 73 ffrt_queue_attr_init(this); 74 } 75 76 /** 77 * @brief Destroys the queue_attr object and releases its resources. 78 * 79 * @since 10 80 */ ~queue_attr()81 ~queue_attr() 82 { 83 ffrt_queue_attr_destroy(this); 84 } 85 86 /** 87 * @brief Deleted copy constructor to prevent copying of queue_attr object. 88 */ 89 queue_attr(const queue_attr&) = delete; 90 91 /** 92 * @brief Deleted copy assignment operator to prevent assignment of queue_attr object. 93 */ 94 queue_attr& operator=(const queue_attr&) = delete; 95 96 /** 97 * @brief Sets the QoS for this queue attribute. 98 * 99 * @param attr Indicates the QoS. 100 * @since 10 101 */ qos(qos qos_)102 inline queue_attr& qos(qos qos_) 103 { 104 ffrt_queue_attr_set_qos(this, qos_); 105 return *this; 106 } 107 108 /** 109 * @brief Gets the QoS level of this queue attribute. 110 * 111 * @return Returns the QoS level. 112 * @since 10 113 */ qos()114 inline int qos() const 115 { 116 return ffrt_queue_attr_get_qos(this); 117 } 118 119 /** 120 * @brief Sets the timeout value for this queue attribute. 121 * 122 * The lower limit of timeout value is 1 ms, if the value is less than 1 ms, it will be set to 1 ms. 123 * 124 * @param timeout_us Indicates the timeout value in microseconds. 125 * @return Returns the current queue_attr object for chaining. 126 * @since 10 127 */ timeout(uint64_t timeout_us)128 inline queue_attr& timeout(uint64_t timeout_us) 129 { 130 ffrt_queue_attr_set_timeout(this, timeout_us); 131 return *this; 132 } 133 134 /** 135 * @brief Gets the timeout value of this queue attribute. 136 * 137 * @return Returns the timeout value in microseconds. 138 * @since 10 139 */ timeout()140 inline uint64_t timeout() const 141 { 142 return ffrt_queue_attr_get_timeout(this); 143 } 144 145 /** 146 * @brief Sets the timeout callback function for this queue attribute. 147 * 148 * @warning Do not call `exit` in `func` - this my cause unexpected behavior. 149 * 150 * @param func Indicates the callback function. 151 * @return Returns the current queue_attr object for chaining. 152 * @since 10 153 */ callback(const std::function<void ()> & func)154 inline queue_attr& callback(const std::function<void()>& func) 155 { 156 ffrt_queue_attr_set_callback(this, create_function_wrapper(func, ffrt_function_kind_queue)); 157 return *this; 158 } 159 160 /** 161 * @brief Gets the timeout callback function of this queue attribute. 162 * 163 * @return Returns a pointer to the callback function header. 164 * @since 10 165 */ callback()166 inline ffrt_function_header_t* callback() const 167 { 168 return ffrt_queue_attr_get_callback(this); 169 } 170 171 /** 172 * @brief Sets the maximum concurrency level for this queue attribute. 173 * 174 * @param max_concurrency Indicates the maximum concurrency level. 175 * @return Returns the current queue_attr object for chaining. 176 * @since 12 177 */ max_concurrency(const int max_concurrency)178 inline queue_attr& max_concurrency(const int max_concurrency) 179 { 180 ffrt_queue_attr_set_max_concurrency(this, max_concurrency); 181 return *this; 182 } 183 184 /** 185 * @brief Gets the maximum concurrency level of this queue attribute. 186 * 187 * @return Returns the maximum concurrency level. 188 * @since 12 189 */ max_concurrency()190 inline int max_concurrency() const 191 { 192 return ffrt_queue_attr_get_max_concurrency(this); 193 } 194 195 /** 196 * @brief Sets the mode for this queue attribute. 197 * 198 * @param legacy_mode Indicates the queue mode. 199 * @return Returns the current queue_attr object for chaining. 200 * @since 20 201 */ thread_mode(bool mode)202 inline queue_attr& thread_mode(bool mode) 203 { 204 ffrt_queue_attr_set_thread_mode(this, mode); 205 return *this; 206 } 207 208 /** 209 * @brief Gets the mode of this queue attribute. 210 * 211 * @return Returns the queue mode. 212 * @since 20 213 */ thread_mode()214 inline bool thread_mode() const 215 { 216 return ffrt_queue_attr_get_thread_mode(this); 217 } 218 }; 219 220 /** 221 * @class queue 222 * @brief Represents a task queue for managing and submitting tasks. 223 * 224 * This class provides methods to submit tasks, cancel tasks, wait for completion, 225 * and retrieve the number of pending tasks in the queue. 226 * 227 * @since 10 228 */ 229 class queue { 230 public: 231 /** 232 * @brief Constructs a queue object with the specified type, name, and attributes. 233 * 234 * @param type Indicates the type of queue. 235 * @param name Indicates the name of the queue. 236 * @param attr Specifies the attributes for the queue. 237 * @since 10 238 */ 239 queue(const queue_type type, const char* name, const queue_attr& attr = {}) 240 { 241 queue_handle = ffrt_queue_create(ffrt_queue_type_t(type), name, &attr); 242 deleter = ffrt_queue_destroy; 243 } 244 245 /** 246 * @brief Constructs a serial queue object with the specified name and attributes. 247 * 248 * @param name Indicates the name of the queue. 249 * @param attr Specifies the attributes for the queue. 250 * @since 10 251 */ 252 queue(const char* name, const queue_attr& attr = {}) 253 { 254 queue_handle = ffrt_queue_create(ffrt_queue_serial, name, &attr); 255 deleter = ffrt_queue_destroy; 256 } 257 258 /** 259 * @brief Destroys the queue object and releases its resources. 260 * @since 10 261 */ ~queue()262 ~queue() 263 { 264 if (deleter) { 265 deleter(queue_handle); 266 } 267 } 268 269 /** 270 * @brief Deleted copy constructor to prevent copying of the queue object. 271 */ 272 queue(const queue&) = delete; 273 274 /** 275 * @brief Deleted copy assignment operator to prevent assignment of the queue object. 276 */ 277 void operator=(const queue&) = delete; 278 279 /** 280 * @brief Submits a task with a specified attribute to this queue. 281 * 282 * @param func Indicates a task executor function closure. 283 * @param attr Indicates a task attribute. 284 * @since 10 285 */ 286 inline void submit(const std::function<void()>& func, const task_attr& attr = {}) 287 { 288 ffrt_queue_submit(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); 289 } 290 291 /** 292 * @brief Submits a task with a specified attribute to this queue. 293 * 294 * @param func Indicates a task executor function closure. 295 * @param attr Indicates a task attribute. 296 * @since 10 297 */ 298 inline void submit(std::function<void()>&& func, const task_attr& attr = {}) 299 { 300 ffrt_queue_submit(queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); 301 } 302 303 /** 304 * @brief Submits a task with a specified attribute to this queue, and obtains a task handle. 305 * 306 * @param func Indicates a task executor function closure. 307 * @param attr Indicates a task attribute. 308 * @return Returns a non-null task handle if the task is submitted; 309 returns a null pointer otherwise. 310 * @since 10 311 */ 312 inline task_handle submit_h(const std::function<void()>& func, const task_attr& attr = {}) 313 { 314 return ffrt_queue_submit_h(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); 315 } 316 317 /** 318 * @brief Submits a task with a specified attribute to this queue, and obtains a task handle. 319 * 320 * @param func Indicates a task executor function closure. 321 * @param attr Indicates a task attribute. 322 * @return Returns a non-null task handle if the task is submitted; 323 returns a null pointer otherwise. 324 * @since 10 325 */ 326 inline task_handle submit_h(std::function<void()>&& func, const task_attr& attr = {}) 327 { 328 return ffrt_queue_submit_h( 329 queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); 330 } 331 332 /** 333 * @brief Submits a task with a specified attribute to this queue. 334 * 335 * @param func Indicates a task executor function closure. 336 * @param attr Indicates a task attribute. 337 */ 338 inline void submit_head(const std::function<void()>& func, const task_attr& attr = {}) 339 { 340 ffrt_queue_submit_head(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); 341 } 342 343 /** 344 * @brief Submits a task with a specified attribute to this queue. 345 * 346 * @param func Indicates a task executor function closure. 347 * @param attr Indicates a task attribute. 348 */ 349 inline void submit_head(std::function<void()>&& func, const task_attr& attr = {}) 350 { 351 ffrt_queue_submit_head(queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); 352 } 353 354 /** 355 * @brief Submits a task with a specified attribute to this queue, and obtains a task handle. 356 * 357 * @param func Indicates a task executor function closure. 358 * @param attr Indicates a task attribute. 359 * @return Returns a non-null task handle if the task is submitted; 360 returns a null pointer otherwise. 361 */ 362 inline task_handle submit_head_h(const std::function<void()>& func, const task_attr& attr = {}) 363 { 364 return ffrt_queue_submit_head_h(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); 365 } 366 367 /** 368 * @brief Submits a task with a specified attribute to this queue, and obtains a task handle. 369 * 370 * @param func Indicates a task executor function closure. 371 * @param attr Indicates a task attribute. 372 * @return Returns a non-null task handle if the task is submitted; 373 returns a null pointer otherwise. 374 */ 375 inline task_handle submit_head_h(std::function<void()>&& func, const task_attr& attr = {}) 376 { 377 return ffrt_queue_submit_head_h( 378 queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); 379 } 380 381 /** 382 * @brief Cancels a task. 383 * 384 * @param handle Indicates a task handle. 385 * @return Returns 0 if the task is canceled; -1 otherwise. 386 * @since 10 387 */ cancel(const task_handle & handle)388 inline int cancel(const task_handle& handle) 389 { 390 return ffrt_queue_cancel(handle); 391 } 392 393 /** 394 * @brief Waits until a task is complete. 395 * 396 * @param handle Indicates a task handle. 397 * @since 10 398 */ wait(const task_handle & handle)399 inline void wait(const task_handle& handle) 400 { 401 return ffrt_queue_wait(handle); 402 } 403 404 /** 405 * @brief Get queue task count. 406 * 407 * @param queue Indicates a queue handle. 408 * @return Returns the queue task count. 409 */ get_task_cnt()410 inline uint64_t get_task_cnt() 411 { 412 return ffrt_queue_get_task_cnt(queue_handle); 413 } 414 415 /** 416 * @brief Get application main thread queue. 417 * 418 * @return Returns application main thread queue. 419 * @since 12 420 */ get_main_queue()421 static inline queue* get_main_queue() 422 { 423 ffrt_queue_t q = ffrt_get_main_queue(); 424 // corner case: main queue is not ready. 425 if (q == nullptr) { 426 return nullptr; 427 } 428 static queue main_queue(q); 429 return &main_queue; 430 } 431 432 private: 433 /** 434 * @brief Type alias for the function pointer used to delete or destroy a queue. 435 */ 436 using QueueDeleter = void (*)(ffrt_queue_t); 437 438 /** 439 * @brief Constructs a queue object from an existing queue handle and an optional deleter. 440 * 441 * @param queue_handle The handle to the existing queue. 442 * @param deleter The function pointer used to destroy the queue. 443 */ queue_handle(queue_handle)444 queue(ffrt_queue_t queue_handle, QueueDeleter deleter = nullptr) : queue_handle(queue_handle), deleter(deleter) {} 445 446 ffrt_queue_t queue_handle = nullptr; ///< Handle to the underlying queue. 447 QueueDeleter deleter = nullptr; ///< Function pointer used to delete or destroy the queue. 448 }; 449 } // namespace ffrt 450 451 #endif // FFRT_API_CPP_QUEUE_H 452 /** @} */