1 /* 2 * Copyright (c) 2021-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 #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_HANDLER_H 17 #define BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_HANDLER_H 18 19 #include "event_runner.h" 20 #include "dumper.h" 21 #include "inner_event.h" 22 23 #ifndef __has_builtin 24 #define __has_builtin(x) 0 25 #endif 26 27 namespace OHOS { 28 namespace AppExecFwk { 29 enum class EventType { 30 SYNC_EVENT = 0, 31 DELAY_EVENT = 1, 32 TIMING_EVENT = 2, 33 }; 34 35 template<typename T> 36 class ThreadLocalData; 37 38 struct TaskOptions { 39 std::string dfxName_; 40 int64_t delayTime_; 41 EventQueue::Priority priority_; 42 uintptr_t taskId_; TaskOptionsTaskOptions43 TaskOptions(std::string dfxName, int64_t delayTime, EventQueue::Priority priority, uintptr_t taskId) 44 : dfxName_(dfxName), delayTime_(delayTime), priority_(priority), taskId_(taskId) {} 45 }; 46 47 struct PendingTaskInfo { 48 int32_t MaxPendingTime = 0; 49 int32_t taskCount = 0; 50 }; 51 class EventHandler : public std::enable_shared_from_this<EventHandler> { 52 public: 53 using CallbackTimeout = std::function<void()>; 54 using Callback = InnerEvent::Callback; 55 using Priority = EventQueue::Priority; 56 57 /** 58 * Constructor, set 'EventRunner' automatically. 59 * 60 * @param runner The 'EventRunner'. 61 */ 62 explicit EventHandler(const std::shared_ptr<EventRunner> &runner = nullptr); 63 virtual ~EventHandler(); 64 DISALLOW_COPY_AND_MOVE(EventHandler); 65 66 /** 67 * Get event handler that running on current thread. 68 * 69 * @return Returns shared pointer of the current 'EventHandler'. 70 */ 71 static std::shared_ptr<EventHandler> Current(); 72 73 /** 74 * Send an event. 75 * 76 * @param event Event which should be handled. 77 * @param delayTime Process the event after 'delayTime' milliseconds. 78 * @param priority Priority of the event queue for this event. 79 * @return Returns true if event has been sent successfully. If returns false, event should be released manually. 80 */ 81 bool SendEvent(InnerEvent::Pointer &event, int64_t delayTime = 0, Priority priority = Priority::LOW); 82 83 /** 84 * Send an event. 85 * 86 * @param event Event which should be handled. 87 * @param taskTime Process the event at taskTime. 88 * @param priority Priority of the event queue for this event. 89 * @return Returns true if event has been sent successfully. If returns false, event should be released manually. 90 */ 91 bool SendTimingEvent(InnerEvent::Pointer &event, int64_t taskTime, Priority priority = Priority::LOW); 92 93 /** 94 * Send an event. 95 * 96 * @param event Event which should be handled. 97 * @param priority Priority of the event queue for this event. 98 * @return Returns true if event has been sent successfully. If returns false, event should be released manually. 99 */ SendEvent(InnerEvent::Pointer & event,Priority priority)100 inline bool SendEvent(InnerEvent::Pointer &event, Priority priority) 101 { 102 return SendEvent(event, 0, priority); 103 } 104 105 /** 106 * Send an event. 107 * 108 * @param event Event which should be handled. 109 * @param delayTime Process the event after 'delayTime' milliseconds. 110 * @param priority Priority of the event queue for this event. 111 * @return Returns true if event has been sent successfully. 112 */ 113 inline bool SendEvent(InnerEvent::Pointer &&event, int64_t delayTime = 0, Priority priority = Priority::LOW) 114 { 115 return SendEvent(event, delayTime, priority); 116 } 117 118 /** 119 * Send an event. 120 * 121 * @param innerEventId The id of the event. 122 * @param param Basic parameter of the event, default is 0. 123 * @param delayTime Process the event after 'delayTime' milliseconds. 124 * @param caller Caller info of the event, default is caller's file, func and line. 125 * @return Returns true if event has been sent successfully. 126 */ 127 inline bool SendEvent(uint32_t innerEventId, int64_t param, int64_t delayTime, const Caller &caller = {}) 128 { 129 return SendEvent(InnerEvent::Get(innerEventId, param, caller), delayTime); 130 } 131 132 /** 133 * Send an event. 134 * 135 * @param innerEventId The id of the event. 136 * @param delayTime Process the event after 'delayTime' milliseconds. 137 * @param priority Priority of the event queue for this event. 138 * @param caller Caller info of the event, default is caller's file, func and line. 139 * @return Returns true if event has been sent successfully. 140 */ 141 inline bool SendEvent(uint32_t innerEventId, int64_t delayTime = 0, 142 Priority priority = Priority::LOW, const Caller &caller = {}) 143 { 144 return SendEvent(InnerEvent::Get(innerEventId, 0, caller), delayTime, priority); 145 } 146 147 /** 148 * Send an event. 149 * 150 * @param innerEventId The id of the event. 151 * @param priority Priority of the event queue for this event. 152 * @param caller Caller info of the event, default is caller's file, func and line. 153 * @return Returns true if event has been sent successfully. 154 */ 155 inline bool SendEvent(uint32_t innerEventId, Priority priority, const Caller &caller = {}) 156 { 157 return SendEvent(InnerEvent::Get(innerEventId, 0, caller), 0, priority); 158 } 159 160 /** 161 * Send an event. 162 * 163 * @param innerEventId The id of the event. 164 * @param object Shared pointer of object. 165 * @param delayTime Process the event after 'delayTime' milliseconds. 166 * @param caller Caller info of the event, default is caller's file, func and line. 167 * @return Returns true if event has been sent successfully. 168 */ 169 template<typename T> 170 inline bool SendEvent(uint32_t innerEventId, const std::shared_ptr<T> &object, 171 int64_t delayTime = 0, const Caller &caller = {}) 172 { 173 return SendEvent(InnerEvent::Get(innerEventId, object, 0, caller), delayTime); 174 } 175 176 /** 177 * Send an event. 178 * 179 * @param innerEventId The id of the event. 180 * @param object Weak pointer of object. 181 * @param delayTime Process the event after 'delayTime' milliseconds. 182 * @param caller Caller info of the event, default is caller's file, func and line. 183 * @return Returns true if event has been sent successfully. 184 */ 185 template<typename T> 186 inline bool SendEvent(uint32_t innerEventId, const std::weak_ptr<T> &object, 187 int64_t delayTime = 0, const Caller &caller = {}) 188 { 189 return SendEvent(InnerEvent::Get(innerEventId, object, 0, caller), delayTime); 190 } 191 192 /** 193 * Send an event. 194 * 195 * @param innerEventId The id of the event. 196 * @param object Unique pointer of object. 197 * @param delayTime Process the event after 'delayTime' milliseconds. 198 * @param caller Caller info of the event, default is caller's file, func and line. 199 * @return Returns true if event has been sent successfully. 200 */ 201 template<typename T, typename D> 202 inline bool SendEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, 203 int64_t delayTime = 0, const Caller &caller = {}) 204 { 205 return SendEvent(InnerEvent::Get(innerEventId, object, 0, caller), delayTime); 206 } 207 208 /** 209 * Send an event. 210 * 211 * @param innerEventId The id of the event. 212 * @param object Unique pointer of object. 213 * @param delayTime Process the event after 'delayTime' milliseconds. 214 * @param caller Caller info of the event, default is caller's file, func and line. 215 * @return Returns true if event has been sent successfully. 216 */ 217 template<typename T, typename D> 218 inline bool SendEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object, 219 int64_t delayTime = 0, const Caller &caller = {}) 220 { 221 return SendEvent(InnerEvent::Get(innerEventId, object, 0, caller), delayTime); 222 } 223 224 /** 225 * Send an immediate event. 226 * 227 * @param event Event which should be handled. 228 * @param caller Caller info of the event, default is caller's file, func and line. 229 * @return Returns true if event has been sent successfully. 230 */ SendImmediateEvent(InnerEvent::Pointer & event)231 inline bool SendImmediateEvent(InnerEvent::Pointer &event) 232 { 233 return SendEvent(event, 0, Priority::IMMEDIATE); 234 } 235 236 /** 237 * Send an immediate event. 238 * 239 * @param event Event which should be handled. 240 * @return Returns true if event has been sent successfully. 241 */ SendImmediateEvent(InnerEvent::Pointer && event)242 inline bool SendImmediateEvent(InnerEvent::Pointer &&event) 243 { 244 return SendImmediateEvent(event); 245 } 246 247 /** 248 * Send an immediate event. 249 * 250 * @param innerEventId The id of the event. 251 * @param param Basic parameter of the event, default is 0. 252 * @param caller Caller info of the event, default is caller's file, func and line. 253 * @return Returns true if event has been sent successfully. 254 */ 255 inline bool SendImmediateEvent(uint32_t innerEventId, int64_t param = 0, const Caller &caller = {}) 256 { 257 return SendImmediateEvent(InnerEvent::Get(innerEventId, param, caller)); 258 } 259 260 /** 261 * Send an immediate event. 262 * 263 * @param innerEventId The id of the event. 264 * @param object Shared pointer of object. 265 * @param caller Caller info of the event, default is caller's file, func and line. 266 * @return Returns true if event has been sent successfully. 267 */ 268 template<typename T> 269 inline bool SendImmediateEvent(uint32_t innerEventId, const std::shared_ptr<T> &object, 270 const Caller &caller = {}) 271 { 272 return SendImmediateEvent(InnerEvent::Get(innerEventId, object, 0, caller)); 273 } 274 275 /** 276 * Send an immediate event. 277 * 278 * @param innerEventId The id of the event. 279 * @param object Weak pointer of object. 280 * @param caller Caller info of the event, default is caller's file, func and line. 281 * @return Returns true if event has been sent successfully. 282 */ 283 template<typename T> 284 inline bool SendImmediateEvent(uint32_t innerEventId, const std::weak_ptr<T> &object, 285 const Caller &caller = {}) 286 { 287 return SendImmediateEvent(InnerEvent::Get(innerEventId, object, 0, caller)); 288 } 289 290 /** 291 * Send an immediate event. 292 * 293 * @param innerEventId The id of the event. 294 * @param object Unique pointer of object. 295 * @param caller Caller info of the event, default is caller's file, func and line. 296 * @return Returns true if event has been sent successfully. 297 */ 298 template<typename T, typename D> 299 inline bool SendImmediateEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, 300 const Caller &caller = {}) 301 { 302 return SendImmediateEvent(InnerEvent::Get(innerEventId, object, 0, caller)); 303 } 304 305 /** 306 * Send an immediate event. 307 * 308 * @param innerEventId The id of the event. 309 * @param object Unique pointer of object. 310 * @param caller Caller info of the event, default is caller's file, func and line. 311 * @return Returns true if event has been sent successfully. 312 */ 313 template<typename T, typename D> 314 inline bool SendImmediateEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object, 315 const Caller &caller = {}) 316 { 317 return SendImmediateEvent(InnerEvent::Get(innerEventId, object, 0, caller)); 318 } 319 320 /** 321 * Send an high priority event. 322 * 323 * @param event Event which should be handled. 324 * @param delayTime Process the event after 'delayTime' milliseconds. 325 * @return Returns true if event has been sent successfully. 326 */ 327 inline bool SendHighPriorityEvent(InnerEvent::Pointer &event, int64_t delayTime = 0) 328 { 329 return SendEvent(event, delayTime, Priority::HIGH); 330 } 331 332 /** 333 * Send an high priority event. 334 * 335 * @param event Event which should be handled. 336 * @param delayTime Process the event after 'delayTime' milliseconds. 337 * @return Returns true if event has been sent successfully. 338 */ 339 inline bool SendHighPriorityEvent(InnerEvent::Pointer &&event, int64_t delayTime = 0) 340 { 341 return SendHighPriorityEvent(event, delayTime); 342 } 343 344 /** 345 * Send an high priority event. 346 * 347 * @param innerEventId The id of the event. 348 * @param param Basic parameter of the event, default is 0. 349 * @param delayTime Process the event after 'delayTime' milliseconds. 350 * @param caller Caller info of the event, default is caller's file, func and line. 351 * @return Returns true if event has been sent successfully. 352 */ 353 inline bool SendHighPriorityEvent(uint32_t innerEventId, int64_t param = 0, 354 int64_t delayTime = 0, const Caller &caller = {}) 355 { 356 return SendHighPriorityEvent(InnerEvent::Get(innerEventId, param, caller), delayTime); 357 } 358 359 /** 360 * Send an high priority event. 361 * 362 * @param innerEventId The id of the event. 363 * @param object Shared pointer of object. 364 * @param delayTime Process the event after 'delayTime' milliseconds. 365 * @param caller Caller info of the event, default is caller's file, func and line. 366 * @return Returns true if event has been sent successfully. 367 */ 368 template<typename T> 369 inline bool SendHighPriorityEvent(uint32_t innerEventId, const std::shared_ptr<T> &object, 370 int64_t delayTime = 0, const Caller &caller = {}) 371 { 372 return SendHighPriorityEvent(InnerEvent::Get(innerEventId, object, 0, caller), delayTime); 373 } 374 375 /** 376 * Send an high priority event. 377 * 378 * @param innerEventId The id of the event. 379 * @param object Weak pointer of object. 380 * @param delayTime Process the event after 'delayTime' milliseconds. 381 * @param caller Caller info of the event, default is caller's file, func and line. 382 * @return Returns true if event has been sent successfully. 383 */ 384 template<typename T> 385 inline bool SendHighPriorityEvent(uint32_t innerEventId, const std::weak_ptr<T> &object, 386 int64_t delayTime = 0, const Caller &caller = {}) 387 { 388 return SendHighPriorityEvent(InnerEvent::Get(innerEventId, object, 0, caller), delayTime); 389 } 390 391 /** 392 * Send an high priority event. 393 * 394 * @param innerEventId The id of the event. 395 * @param object Unique pointer of object. 396 * @param delayTime Process the event after 'delayTime' milliseconds. 397 * @param caller Caller info of the event, default is caller's file, func and line. 398 * @return Returns true if event has been sent successfully. 399 */ 400 template<typename T, typename D> 401 inline bool SendHighPriorityEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, 402 int64_t delayTime = 0, const Caller &caller = {}) 403 { 404 return SendHighPriorityEvent(InnerEvent::Get(innerEventId, object, 0, caller), delayTime); 405 } 406 407 /** 408 * Send an high priority event. 409 * 410 * @param innerEventId The id of the event. 411 * @param object Unique pointer of object. 412 * @param delayTime Process the event after 'delayTime' milliseconds. 413 * @param caller Caller info of the event, default is caller's file, func and line. 414 * @return Returns true if event has been sent successfully. 415 */ 416 template<typename T, typename D> 417 inline bool SendHighPriorityEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object, 418 int64_t delayTime = 0, const Caller &caller = {}) 419 { 420 return SendHighPriorityEvent(InnerEvent::Get(innerEventId, object, 0, caller), delayTime); 421 } 422 423 /** 424 * Post a task. 425 * 426 * @param callback Task callback. 427 * @param name Name of the task. 428 * @param delayTime Process the event after 'delayTime' milliseconds. 429 * @param priority Priority of the event queue for this event. 430 * @param caller Caller info of the event, default is caller's file, func and line. 431 * @return Returns true if task has been sent successfully. 432 */ 433 inline bool PostTask(const Callback &callback, const std::string &name = std::string(), 434 int64_t delayTime = 0, Priority priority = Priority::LOW, const Caller &caller = {}) 435 { 436 return SendEvent(InnerEvent::Get(callback, name, caller), delayTime, priority); 437 } 438 439 /** 440 * Post a task at front of queue. 441 * 442 * @param callback Task callback. 443 * @param name Name of the task. 444 * @param priority Priority of the event queue for this event. 445 * @param caller Caller info of the event, default is caller's file, func and line. 446 * @return Returns true if task has been sent successfully. 447 */ 448 bool PostTaskAtFront(const Callback &callback, const std::string &name = std::string(), 449 Priority priority = Priority::LOW, const Caller &caller = {}); 450 451 /** 452 * Set delivery time out callback. 453 * 454 * @param callback Delivery Time out callback. 455 */ SetDeliveryTimeoutCallback(const Callback & callback)456 void SetDeliveryTimeoutCallback(const Callback &callback) 457 { 458 deliveryTimeoutCallback_ = callback; 459 } 460 461 /** 462 * Set distribute time out callback. 463 * 464 * @param callback Distribute Time out callback. 465 */ SetDistributeTimeoutCallback(const Callback & callback)466 void SetDistributeTimeoutCallback(const Callback &callback) 467 { 468 distributeTimeoutCallback_ = callback; 469 } 470 471 /** 472 * Post a task. 473 * 474 * @param callback Task callback. 475 * @param priority Priority of the event queue for this event. 476 * @param caller Caller info of the event, default is caller's file, func and line. 477 * @return Returns true if task has been sent successfully. 478 */ 479 inline bool PostTask(const Callback &callback, Priority priority, const Caller &caller = {}) 480 { 481 return PostTask(callback, std::string(), 0, priority, caller); 482 } 483 484 /** 485 * Post a task. 486 * 487 * @param callback Task callback. 488 * @param delayTime Process the event after 'delayTime' milliseconds. 489 * @param priority Priority of the event queue for this event. 490 * @param caller Caller info of the event, default is caller's file, func and line. 491 * @return Returns true if task has been sent successfully. 492 */ 493 inline bool PostTask(const Callback &callback, int64_t delayTime, Priority priority = Priority::LOW, 494 const Caller &caller = {}) 495 { 496 return PostTask(callback, std::string(), delayTime, priority, caller); 497 } 498 499 /** 500 * Post an immediate task. 501 * 502 * @param callback Task callback. 503 * @param name Remove events by name of the task. 504 * @param caller Caller info of the event, default is caller's file, func and line. 505 * @return Returns true if task has been sent successfully. 506 */ 507 inline bool PostImmediateTask(const Callback &callback, const std::string &name = std::string(), 508 const Caller &caller = {}) 509 { 510 return SendEvent(InnerEvent::Get(callback, name, caller), 0, Priority::IMMEDIATE); 511 } 512 513 /** 514 * Post a high priority task. 515 * 516 * @param callback Task callback. 517 * @param name Name of the task. 518 * @param delayTime Process the event after 'delayTime' milliseconds. 519 * @param caller Caller info of the event, default is caller's file, func and line. 520 * @return Returns true if task has been sent successfully. 521 */ 522 inline bool PostHighPriorityTask(const Callback &callback, const std::string &name = std::string(), 523 int64_t delayTime = 0, const Caller &caller = {}) 524 { 525 return PostTask(callback, name, delayTime, Priority::HIGH, caller); 526 } 527 528 /** 529 * Post a high priority task. 530 * 531 * @param callback Task callback. 532 * @param delayTime Process the event after 'delayTime' milliseconds. 533 * @param caller Caller info of the event, default is caller's file, func and line. 534 * @return Returns true if task has been sent successfully. 535 */ 536 inline bool PostHighPriorityTask(const Callback &callback, int64_t delayTime, const Caller &caller = {}) 537 { 538 return PostHighPriorityTask(callback, std::string(), delayTime, caller); 539 } 540 541 /** 542 * Post a idle task. 543 * 544 * @param callback task callback. 545 * @param name Name of the task. 546 * @param delayTime Process the event after 'delayTime' milliseconds. 547 * @param caller Caller info of the event, default is caller's file, func and line. 548 * @return Returns true if task has been sent successfully. 549 */ 550 inline bool PostIdleTask(const Callback &callback, const std::string &name = std::string(), 551 int64_t delayTime = 0, const Caller &caller = {}) 552 { 553 return PostTask(callback, name, delayTime, Priority::IDLE, caller); 554 } 555 556 /** 557 * Post a idle task. 558 * 559 * @param callback Task callback. 560 * @param delayTime Process the event after 'delayTime' milliseconds. 561 * @param caller Caller info of the event, default is caller's file, func and line. 562 * @return Returns true if task has been sent successfully. 563 */ 564 inline bool PostIdleTask(const Callback &callback, int64_t delayTime, const Caller &caller = {}) 565 { 566 return PostIdleTask(callback, std::string(), delayTime, caller); 567 } 568 569 /** 570 * Send an event, and wait until this event has been handled. 571 * 572 * @param event Event which should be handled. 573 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 574 * @return Returns true if event has been sent successfully. If returns false, event should be released manually. 575 */ 576 bool SendSyncEvent(InnerEvent::Pointer &event, Priority priority = Priority::LOW); 577 578 /** 579 * Send an event. 580 * 581 * @param event Event which should be handled. 582 * @param priority Priority of the event queue for this event. 583 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 584 * @return Returns true if event has been sent successfully. 585 */ 586 inline bool SendSyncEvent(InnerEvent::Pointer &&event, Priority priority = Priority::LOW) 587 { 588 return SendSyncEvent(event, priority); 589 } 590 591 /** 592 * Send an event, and wait until this event has been handled. 593 * 594 * @param innerEventId The id of the event. 595 * @param param Basic parameter of the event, default is 0. 596 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 597 * @param caller Caller info of the event, default is caller's file, func and line. 598 * @return Returns true if event has been sent successfully. 599 */ 600 inline bool SendSyncEvent(uint32_t innerEventId, int64_t param = 0, 601 Priority priority = Priority::LOW, const Caller &caller = {}) 602 { 603 return SendSyncEvent(InnerEvent::Get(innerEventId, param, caller), priority); 604 } 605 606 /** 607 * Send an event, and wait until this event has been handled. 608 * 609 * @param innerEventId The id of the event. 610 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 611 * @param caller Caller info of the event, default is caller's file, func and line. 612 * @return Returns true if event has been sent successfully. 613 */ 614 inline bool SendSyncEvent(uint32_t innerEventId, Priority priority, const Caller &caller = {}) 615 { 616 return SendSyncEvent(InnerEvent::Get(innerEventId, 0, caller), priority); 617 } 618 619 /** 620 * Send an event, and wait until this event has been handled. 621 * 622 * @param innerEventId The id of the event. 623 * @param object Shared pointer of object. 624 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 625 * @param caller Caller info of the event, default is caller's file, func and line. 626 * @return Returns true if event has been sent successfully. 627 */ 628 template<typename T> 629 inline bool SendSyncEvent(uint32_t innerEventId, const std::shared_ptr<T> &object, 630 Priority priority = Priority::LOW, const Caller &caller = {}) 631 { 632 return SendSyncEvent(InnerEvent::Get(innerEventId, object, 0, caller), priority); 633 } 634 635 /** 636 * Send an event, and wait until this event has been handled. 637 * 638 * @param innerEventId The id of the event. 639 * @param object Weak pointer of object. 640 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 641 * @param caller Caller info of the event, default is caller's file, func and line. 642 * @return Returns true if event has been sent successfully. 643 */ 644 template<typename T> 645 inline bool SendSyncEvent(uint32_t innerEventId, const std::weak_ptr<T> &object, 646 Priority priority = Priority::LOW, const Caller &caller = {}) 647 { 648 return SendSyncEvent(InnerEvent::Get(innerEventId, object, 0, caller), priority); 649 } 650 651 /** 652 * Send an event, and wait until this event has been handled. 653 * 654 * @param innerEventId The id of the event. 655 * @param object Unique pointer of object. 656 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 657 * @param caller Caller info of the event, default is caller's file, func and line. 658 * @return Returns true if event has been sent successfully. 659 */ 660 template<typename T, typename D> 661 inline bool SendSyncEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, 662 Priority priority = Priority::LOW, const Caller &caller = {}) 663 { 664 return SendSyncEvent(InnerEvent::Get(innerEventId, object, 0, caller), priority); 665 } 666 667 /** 668 * Send an event, and wait until this event has been handled. 669 * 670 * @param innerEventId The id of the event. 671 * @param object Unique pointer of object. 672 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 673 * @param caller Caller info of the event, default is caller's file, func and line. 674 * @return Returns true if event has been sent successfully. 675 */ 676 template<typename T, typename D> 677 inline bool SendSyncEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object, 678 Priority priority = Priority::LOW, const Caller &caller = {}) 679 { 680 return SendSyncEvent(InnerEvent::Get(innerEventId, object, 0, caller), priority); 681 } 682 683 /** 684 * Post a task, and wait until this task has been handled. 685 * 686 * @param callback Task callback. 687 * @param name Name of the task. 688 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 689 * @param caller Caller info of the event, default is caller's file, func and line. 690 * @return Returns true if task has been sent successfully. 691 */ 692 inline bool PostSyncTask(const Callback &callback, const std::string &name, 693 Priority priority = Priority::LOW, const Caller &caller = {}) 694 { 695 return SendSyncEvent(InnerEvent::Get(callback, name, caller), priority); 696 } 697 698 /** 699 * Post a task, and wait until this task has been handled. 700 * 701 * @param callback Task callback. 702 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 703 * @param caller Caller info of the event, default is caller's file, func and line. 704 * @return Returns true if task has been sent successfully. 705 */ 706 inline bool PostSyncTask(const Callback &callback, Priority priority = Priority::LOW, 707 const Caller &caller = {}) 708 { 709 return PostSyncTask(callback, std::string(), priority, caller); 710 } 711 712 /** 713 * Send a timing event. 714 * 715 * @param event Event which should be handled. 716 * @param taskTime Process the event at taskTime. 717 * @param priority Priority of the event queue for this event. 718 * @return Returns true if event has been sent successfully. 719 */ SendTimingEvent(InnerEvent::Pointer && event,int64_t taskTime,Priority priority)720 inline bool SendTimingEvent(InnerEvent::Pointer &&event, int64_t taskTime, Priority priority) 721 { 722 return SendTimingEvent(event, taskTime, priority); 723 } 724 725 /** 726 * Send a timing event. 727 * 728 * @param event Event which should be handled. 729 * @param taskTime Process the event at taskTime. 730 * @return Returns true if event has been sent successfully. 731 */ SendTimingEvent(InnerEvent::Pointer && event,int64_t taskTime)732 inline bool SendTimingEvent(InnerEvent::Pointer &&event, int64_t taskTime) 733 { 734 return SendTimingEvent(event, taskTime, Priority::LOW); 735 } 736 737 /** 738 * Send a timing event. 739 * 740 * @param innerEventId The id of the event. 741 * @param taskTime Process the event at taskTime. 742 * @param param Basic parameter of the event. 743 * @param caller Caller info of the event, default is caller's file, func and line. 744 * @return Returns true if event has been sent successfully. 745 */ 746 inline bool SendTimingEvent(uint32_t innerEventId, int64_t taskTime, int64_t param, 747 const Caller &caller = {}) 748 { 749 return SendTimingEvent(InnerEvent::Get(innerEventId, param, caller), taskTime); 750 } 751 752 /** 753 * Send a timing event. 754 * 755 * @param innerEventId The id of the event. 756 * @param taskTime Process the event at taskTime. 757 * @param priority Priority of the event queue for this event. 758 * @param caller Caller info of the event, default is caller's file, func and line. 759 * @return Returns true if event has been sent successfully. 760 */ 761 inline bool SendTimingEvent(uint32_t innerEventId, int64_t taskTime, Priority priority, 762 const Caller &caller = {}) 763 { 764 return SendTimingEvent(InnerEvent::Get(innerEventId, 0, caller), taskTime, priority); 765 } 766 767 /** 768 * Send a timing event. 769 * 770 * @param innerEventId The id of the event. 771 * @param taskTime Process the event at taskTime. 772 * @param priority Priority of the event queue for this event. 773 * @param caller Caller info of the event, default is caller's file, func and line. 774 * @return Returns true if event has been sent successfully. 775 */ 776 inline bool SendTimingEvent(uint32_t innerEventId, int64_t taskTime, const Caller &caller = {}) 777 { 778 return SendTimingEvent(InnerEvent::Get(innerEventId, 0, caller), taskTime, Priority::LOW); 779 } 780 781 /** 782 * Send a timing event. 783 * 784 * @param innerEventId The id of the event. 785 * @param object Shared pointer of object. 786 * @param taskTime Process the event at taskTime. 787 * @param priority Priority of the event queue for this event 788 * @param caller Caller info of the event, default is caller's file, func and line. 789 * @return Returns true if event has been sent successfully. 790 */ 791 template<typename T> 792 inline bool SendTimingEvent(uint32_t innerEventId, const std::shared_ptr<T> &object, int64_t taskTime, 793 Priority priority = Priority::LOW, const Caller &caller = {}) 794 { 795 return SendTimingEvent(InnerEvent::Get(innerEventId, object, 0, caller), taskTime, priority); 796 } 797 798 /** 799 * Send a timing event. 800 * 801 * @param innerEventId The id of the event. 802 * @param object Weak pointer of object. 803 * @param taskTime Process the event at taskTime. 804 * @param priority Priority of the event queue for this event 805 * @param caller Caller info of the event, default is caller's file, func and line. 806 * @return Returns true if event has been sent successfully. 807 */ 808 template<typename T> 809 inline bool SendTimingEvent(uint32_t innerEventId, const std::weak_ptr<T> &object, int64_t taskTime, 810 Priority priority = Priority::LOW, const Caller &caller = {}) 811 { 812 return SendTimingEvent(InnerEvent::Get(innerEventId, object, 0, caller), taskTime, priority); 813 } 814 815 /** 816 * Send a timing event. 817 * 818 * @param innerEventId The id of the event. 819 * @param object Unique pointer of object. 820 * @param taskTime Process the event at taskTime. 821 * @param priority Priority of the event queue for this event 822 * @param caller Caller info of the event, default is caller's file, func and line. 823 * @return Returns true if event has been sent successfully. 824 */ 825 template<typename T, typename D> 826 inline bool SendTimingEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, int64_t taskTime, 827 Priority priority = Priority::LOW, const Caller &caller = {}) 828 { 829 return SendTimingEvent(InnerEvent::Get(innerEventId, object, 0, caller), taskTime, priority); 830 } 831 832 /** 833 * Send a timing event. 834 * 835 * @param innerEventId The id of the event. 836 * @param object Unique pointer of object. 837 * @param taskTime Process the event at taskTime. 838 * @param priority Priority of the event queue for this event 839 * @param caller Caller info of the event, default is caller's file, func and line. 840 * @return Returns true if event has been sent successfully. 841 */ 842 template<typename T, typename D> 843 inline bool SendTimingEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object, int64_t taskTime, 844 Priority priority = Priority::LOW, const Caller &caller = {}) 845 { 846 return SendTimingEvent(InnerEvent::Get(innerEventId, object, 0, caller), taskTime, priority); 847 } 848 849 /** 850 * Post a timing task. 851 * 852 * @param callback Task callback. 853 * @param taskTime Process the event at taskTime. 854 * @param name Name of the task. 855 * @param priority Priority of the event queue for this event. 856 * @param caller Caller info of the event, default is caller's file, func and line. 857 * @return Returns true if task has been sent successfully. 858 */ 859 inline bool PostTimingTask(const Callback &callback, int64_t taskTime, const std::string &name = std::string(), 860 Priority priority = Priority::LOW, const Caller &caller = {}) 861 { 862 return SendTimingEvent(InnerEvent::Get(callback, name, caller), taskTime, priority); 863 } 864 865 /** 866 * Post a timing task. 867 * 868 * @param callback Task callback. 869 * @param taskTime Process the event at taskTime. 870 * @param priority Priority of the event queue for this event. 871 * @param caller Caller info of the event, default is caller's file, func and line. 872 * @return Returns true if task has been sent successfully. 873 */ 874 inline bool PostTimingTask(const Callback &callback, int64_t taskTime, Priority priority = Priority::LOW, 875 const Caller &caller = {}) 876 { 877 return PostTimingTask(callback, taskTime, std::string(), priority, caller); 878 } 879 880 /** 881 * Remove all sent events. 882 */ 883 void RemoveAllEvents(); 884 885 /** 886 * Remove sent events. 887 * 888 * @param innerEventId The id of the event. 889 */ 890 void RemoveEvent(uint32_t innerEventId); 891 892 /** 893 * Remove sent events. 894 * 895 * @param innerEventId The id of the event. 896 * @param param Basic parameter of the event. 897 */ 898 void RemoveEvent(uint32_t innerEventId, int64_t param); 899 900 /** 901 * Remove a task. 902 * 903 * @param name Name of the task. 904 */ 905 void RemoveTask(const std::string &name); 906 907 /** 908 * Remove a task. 909 * 910 * @param name Name of the task. 911 */ 912 int RemoveTaskWithRet(const std::string &name); 913 914 /** 915 * Add file descriptor listener for a file descriptor. 916 * 917 * @param fileDescriptor File descriptor. 918 * @param events Events from file descriptor, such as input, output, error 919 * @param listener Listener callback. 920 * @return Return 'ERR_OK' on success. 921 */ 922 ErrCode AddFileDescriptorListener(int32_t fileDescriptor, uint32_t events, 923 const std::shared_ptr<FileDescriptorListener> &listener, const std::string &taskName); 924 925 /** 926 * Add file descriptor listener for a file descriptor. 927 * 928 * @param fileDescriptor File descriptor. 929 * @param events Events from file descriptor, such as input, output, error 930 * @param listener Listener callback. 931 * @param priority Priority of the for file descriptor. 932 * @return Return 'ERR_OK' on success. 933 */ 934 ErrCode AddFileDescriptorListener(int32_t fileDescriptor, uint32_t events, 935 const std::shared_ptr<FileDescriptorListener> &listener, const std::string &taskName, 936 EventQueue::Priority priority); 937 938 /** 939 * Remove all file descriptor listeners. 940 */ 941 void RemoveAllFileDescriptorListeners(); 942 943 /** 944 * Remove file descriptor listener for a file descriptor. 945 * 946 * @param fileDescriptor File descriptor. 947 */ 948 void RemoveFileDescriptorListener(int32_t fileDescriptor); 949 950 /** 951 * Set the 'EventRunner' to the 'EventHandler'. 952 * 953 * @param runner The 'EventRunner'. 954 */ 955 void SetEventRunner(const std::shared_ptr<EventRunner> &runner); 956 957 /** 958 * Get the 'EventRunner' of the 'EventHandler'. 959 * 960 * @return Return the 'EventRunner'. 961 */ GetEventRunner()962 inline const std::shared_ptr<EventRunner> &GetEventRunner() const 963 { 964 return eventRunner_; 965 } 966 967 /** 968 * Distribute time out handler. 969 * 970 * @param beginTime Dotting before distribution. 971 */ 972 void DistributeTimeoutHandler(const InnerEvent::TimePoint& beginTime); 973 974 /** 975 * Distribute the event. 976 * 977 * @param event The event should be distributed. 978 */ 979 void DistributeEvent(const InnerEvent::Pointer &event); 980 981 /** 982 * Distribute time out action. 983 * 984 * @param event The event should be distribute. 985 * @param nowStart Dotting before distribution. 986 */ 987 void DistributeTimeAction(const InnerEvent::Pointer &event, InnerEvent::TimePoint nowStart); 988 989 /** 990 * Delivery time out action. 991 * 992 * @param event The event should be distribute. 993 * @param nowStart Dotting before distribution. 994 */ 995 void DeliveryTimeAction(const InnerEvent::Pointer &event, InnerEvent::TimePoint nowStart); 996 997 /** 998 * Print out the internal information about an object in the specified format, 999 * helping you diagnose internal errors of the object. 1000 * 1001 * @param dumpr The Dumper object you have implemented to process the output internal information. 1002 */ 1003 void Dump(Dumper &dumper); 1004 1005 /** 1006 * Check whether an event with the given ID can be found among the events that have been sent but not processed. 1007 * 1008 * @param innerEventId The id of the event. 1009 */ 1010 bool HasInnerEvent(uint32_t innerEventId); 1011 1012 /** 1013 * Check whether an event carrying the given param can be found among the events that have been sent but not 1014 * processed. 1015 * 1016 * @param param Basic parameter of the event. 1017 */ 1018 bool HasInnerEvent(int64_t param); 1019 1020 /** 1021 * Check whether an event carrying the given param can be found among the events that have been sent but not 1022 * processed. 1023 * 1024 * @param event InnerEvent whose name is to be obtained. 1025 * @return Returns the task name if the given event contains a specific task; returns the event ID otherwise. 1026 */ 1027 std::string GetEventName(const InnerEvent::Pointer &event); 1028 1029 /** 1030 * Check whether there are events which priority higher than basePrio in subevent queue. 1031 * 1032 * @param basePrio base priority 1033 * @return Return true if there are higher priority events, ohtherwise return false. 1034 */ 1035 bool HasPreferEvent(int basePrio); 1036 1037 /** 1038 * Checks whether the current event handler is idle 1039 * @return Returns true if current event handler is idle otherwise return false. 1040 */ 1041 bool IsIdle(); 1042 1043 /** 1044 * @param enableEventLog dump event log handle time. 1045 */ 1046 void EnableEventLog(bool enableEventLog = false); 1047 1048 /** 1049 * Get handler id, only for inner use 1050 */ GetHandlerId()1051 inline std::string GetHandlerId() 1052 { 1053 return handlerId_; 1054 } 1055 1056 /** 1057 * Get pending task info 1058 */ 1059 PendingTaskInfo QueryPendingTaskInfo(int32_t fileDescriptor); 1060 1061 /** 1062 * queue_cancel_and_wait 1063 */ 1064 void TaskCancelAndWait(); 1065 1066 protected: 1067 /** 1068 * Process the event. Developers should override this method. 1069 * 1070 * @param event The event should be processed. 1071 */ 1072 virtual void ProcessEvent(const InnerEvent::Pointer &event); 1073 1074 private: 1075 std::string handlerId_; 1076 bool enableEventLog_ {false}; 1077 std::shared_ptr<EventRunner> eventRunner_; 1078 CallbackTimeout deliveryTimeoutCallback_; 1079 CallbackTimeout distributeTimeoutCallback_; 1080 static thread_local std::weak_ptr<EventHandler> currentEventHandler; 1081 }; 1082 } // namespace AppExecFwk 1083 namespace EventHandling = AppExecFwk; 1084 } // namespace OHOS 1085 1086 #endif // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_HANDLER_H 1087