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 = {}, bool noBarrier = false); 450 451 bool PostTaskAtTail(const Callback &callback, const std::string &name = std::string(), 452 Priority priority = Priority::LOW, const Caller &caller = {}, bool noBarrier = false); 453 454 /** 455 * Set delivery time out callback. 456 * 457 * @param callback Delivery Time out callback. 458 */ SetDeliveryTimeoutCallback(const Callback & callback)459 void SetDeliveryTimeoutCallback(const Callback &callback) 460 { 461 deliveryTimeoutCallback_ = callback; 462 } 463 464 /** 465 * Set distribute time out callback. 466 * 467 * @param callback Distribute Time out callback. 468 */ SetDistributeTimeoutCallback(const Callback & callback)469 void SetDistributeTimeoutCallback(const Callback &callback) 470 { 471 distributeTimeoutCallback_ = callback; 472 } 473 474 /** 475 * Post a task. 476 * 477 * @param callback Task callback. 478 * @param priority Priority of the event queue for this event. 479 * @param caller Caller info of the event, default is caller's file, func and line. 480 * @return Returns true if task has been sent successfully. 481 */ 482 inline bool PostTask(const Callback &callback, Priority priority, const Caller &caller = {}) 483 { 484 return PostTask(callback, std::string(), 0, priority, caller); 485 } 486 487 /** 488 * Post a task. 489 * 490 * @param callback Task callback. 491 * @param delayTime Process the event after 'delayTime' milliseconds. 492 * @param priority Priority of the event queue for this event. 493 * @param caller Caller info of the event, default is caller's file, func and line. 494 * @return Returns true if task has been sent successfully. 495 */ 496 inline bool PostTask(const Callback &callback, int64_t delayTime, Priority priority = Priority::LOW, 497 const Caller &caller = {}) 498 { 499 return PostTask(callback, std::string(), delayTime, priority, caller); 500 } 501 502 /** 503 * Post an immediate task. 504 * 505 * @param callback Task callback. 506 * @param name Remove events by name of the task. 507 * @param caller Caller info of the event, default is caller's file, func and line. 508 * @return Returns true if task has been sent successfully. 509 */ 510 inline bool PostImmediateTask(const Callback &callback, const std::string &name = std::string(), 511 const Caller &caller = {}) 512 { 513 return SendEvent(InnerEvent::Get(callback, name, caller), 0, Priority::IMMEDIATE); 514 } 515 516 /** 517 * Post a high priority task. 518 * 519 * @param callback Task callback. 520 * @param name Name of the task. 521 * @param delayTime Process the event after 'delayTime' milliseconds. 522 * @param caller Caller info of the event, default is caller's file, func and line. 523 * @return Returns true if task has been sent successfully. 524 */ 525 inline bool PostHighPriorityTask(const Callback &callback, const std::string &name = std::string(), 526 int64_t delayTime = 0, const Caller &caller = {}) 527 { 528 return PostTask(callback, name, delayTime, Priority::HIGH, caller); 529 } 530 531 /** 532 * Post a high priority task. 533 * 534 * @param callback Task callback. 535 * @param delayTime Process the event after 'delayTime' milliseconds. 536 * @param caller Caller info of the event, default is caller's file, func and line. 537 * @return Returns true if task has been sent successfully. 538 */ 539 inline bool PostHighPriorityTask(const Callback &callback, int64_t delayTime, const Caller &caller = {}) 540 { 541 return PostHighPriorityTask(callback, std::string(), delayTime, caller); 542 } 543 544 /** 545 * Post a idle task. 546 * 547 * @param callback task callback. 548 * @param name Name of the task. 549 * @param delayTime Process the event after 'delayTime' milliseconds. 550 * @param caller Caller info of the event, default is caller's file, func and line. 551 * @return Returns true if task has been sent successfully. 552 */ 553 inline bool PostIdleTask(const Callback &callback, const std::string &name = std::string(), 554 int64_t delayTime = 0, const Caller &caller = {}) 555 { 556 return PostTask(callback, name, delayTime, Priority::IDLE, caller); 557 } 558 559 /** 560 * Post a idle task. 561 * 562 * @param callback Task callback. 563 * @param delayTime Process the event after 'delayTime' milliseconds. 564 * @param caller Caller info of the event, default is caller's file, func and line. 565 * @return Returns true if task has been sent successfully. 566 */ 567 inline bool PostIdleTask(const Callback &callback, int64_t delayTime, const Caller &caller = {}) 568 { 569 return PostIdleTask(callback, std::string(), delayTime, caller); 570 } 571 572 /** 573 * Send an event, and wait until this event has been handled. 574 * 575 * @param event Event which should be handled. 576 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 577 * @return Returns true if event has been sent successfully. If returns false, event should be released manually. 578 */ 579 bool SendSyncEvent(InnerEvent::Pointer &event, Priority priority = Priority::LOW); 580 581 /** 582 * Send an event. 583 * 584 * @param event Event which should be handled. 585 * @param priority Priority of the event queue for this event. 586 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 587 * @return Returns true if event has been sent successfully. 588 */ 589 inline bool SendSyncEvent(InnerEvent::Pointer &&event, Priority priority = Priority::LOW) 590 { 591 return SendSyncEvent(event, priority); 592 } 593 594 /** 595 * Send an event, and wait until this event has been handled. 596 * 597 * @param innerEventId The id of the event. 598 * @param param Basic parameter of the event, default is 0. 599 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 600 * @param caller Caller info of the event, default is caller's file, func and line. 601 * @return Returns true if event has been sent successfully. 602 */ 603 inline bool SendSyncEvent(uint32_t innerEventId, int64_t param = 0, 604 Priority priority = Priority::LOW, const Caller &caller = {}) 605 { 606 return SendSyncEvent(InnerEvent::Get(innerEventId, param, caller), priority); 607 } 608 609 /** 610 * Send an event, and wait until this event has been handled. 611 * 612 * @param innerEventId The id of the event. 613 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 614 * @param caller Caller info of the event, default is caller's file, func and line. 615 * @return Returns true if event has been sent successfully. 616 */ 617 inline bool SendSyncEvent(uint32_t innerEventId, Priority priority, const Caller &caller = {}) 618 { 619 return SendSyncEvent(InnerEvent::Get(innerEventId, 0, caller), priority); 620 } 621 622 /** 623 * Send an event, and wait until this event has been handled. 624 * 625 * @param innerEventId The id of the event. 626 * @param object Shared pointer of object. 627 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 628 * @param caller Caller info of the event, default is caller's file, func and line. 629 * @return Returns true if event has been sent successfully. 630 */ 631 template<typename T> 632 inline bool SendSyncEvent(uint32_t innerEventId, const std::shared_ptr<T> &object, 633 Priority priority = Priority::LOW, const Caller &caller = {}) 634 { 635 return SendSyncEvent(InnerEvent::Get(innerEventId, object, 0, caller), priority); 636 } 637 638 /** 639 * Send an event, and wait until this event has been handled. 640 * 641 * @param innerEventId The id of the event. 642 * @param object Weak pointer of object. 643 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 644 * @param caller Caller info of the event, default is caller's file, func and line. 645 * @return Returns true if event has been sent successfully. 646 */ 647 template<typename T> 648 inline bool SendSyncEvent(uint32_t innerEventId, const std::weak_ptr<T> &object, 649 Priority priority = Priority::LOW, const Caller &caller = {}) 650 { 651 return SendSyncEvent(InnerEvent::Get(innerEventId, object, 0, caller), priority); 652 } 653 654 /** 655 * Send an event, and wait until this event has been handled. 656 * 657 * @param innerEventId The id of the event. 658 * @param object Unique pointer of object. 659 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 660 * @param caller Caller info of the event, default is caller's file, func and line. 661 * @return Returns true if event has been sent successfully. 662 */ 663 template<typename T, typename D> 664 inline bool SendSyncEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, 665 Priority priority = Priority::LOW, const Caller &caller = {}) 666 { 667 return SendSyncEvent(InnerEvent::Get(innerEventId, object, 0, caller), priority); 668 } 669 670 /** 671 * Send an event, and wait until this event has been handled. 672 * 673 * @param innerEventId The id of the event. 674 * @param object Unique pointer of object. 675 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 676 * @param caller Caller info of the event, default is caller's file, func and line. 677 * @return Returns true if event has been sent successfully. 678 */ 679 template<typename T, typename D> 680 inline bool SendSyncEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object, 681 Priority priority = Priority::LOW, const Caller &caller = {}) 682 { 683 return SendSyncEvent(InnerEvent::Get(innerEventId, object, 0, caller), priority); 684 } 685 686 /** 687 * Post a task, and wait until this task has been handled. 688 * 689 * @param callback Task callback. 690 * @param name Name of the task. 691 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 692 * @param caller Caller info of the event, default is caller's file, func and line. 693 * @return Returns true if task has been sent successfully. 694 */ 695 inline bool PostSyncTask(const Callback &callback, const std::string &name, 696 Priority priority = Priority::LOW, const Caller &caller = {}) 697 { 698 return SendSyncEvent(InnerEvent::Get(callback, name, caller), priority); 699 } 700 701 /** 702 * Post a task, and wait until this task has been handled. 703 * 704 * @param callback Task callback. 705 * @param priority Priority of the event queue for this event, IDLE is not permitted for sync event. 706 * @param caller Caller info of the event, default is caller's file, func and line. 707 * @return Returns true if task has been sent successfully. 708 */ 709 inline bool PostSyncTask(const Callback &callback, Priority priority = Priority::LOW, 710 const Caller &caller = {}) 711 { 712 return PostSyncTask(callback, std::string(), priority, caller); 713 } 714 715 /** 716 * Send a timing event. 717 * 718 * @param event Event which should be handled. 719 * @param taskTime Process the event at taskTime. 720 * @param priority Priority of the event queue for this event. 721 * @return Returns true if event has been sent successfully. 722 */ SendTimingEvent(InnerEvent::Pointer && event,int64_t taskTime,Priority priority)723 inline bool SendTimingEvent(InnerEvent::Pointer &&event, int64_t taskTime, Priority priority) 724 { 725 return SendTimingEvent(event, taskTime, priority); 726 } 727 728 /** 729 * Send a timing event. 730 * 731 * @param event Event which should be handled. 732 * @param taskTime Process the event at taskTime. 733 * @return Returns true if event has been sent successfully. 734 */ SendTimingEvent(InnerEvent::Pointer && event,int64_t taskTime)735 inline bool SendTimingEvent(InnerEvent::Pointer &&event, int64_t taskTime) 736 { 737 return SendTimingEvent(event, taskTime, Priority::LOW); 738 } 739 740 /** 741 * Send a timing event. 742 * 743 * @param innerEventId The id of the event. 744 * @param taskTime Process the event at taskTime. 745 * @param param Basic parameter of the event. 746 * @param caller Caller info of the event, default is caller's file, func and line. 747 * @return Returns true if event has been sent successfully. 748 */ 749 inline bool SendTimingEvent(uint32_t innerEventId, int64_t taskTime, int64_t param, 750 const Caller &caller = {}) 751 { 752 return SendTimingEvent(InnerEvent::Get(innerEventId, param, caller), taskTime); 753 } 754 755 /** 756 * Send a timing event. 757 * 758 * @param innerEventId The id of the event. 759 * @param taskTime Process the event at taskTime. 760 * @param priority Priority of the event queue for this event. 761 * @param caller Caller info of the event, default is caller's file, func and line. 762 * @return Returns true if event has been sent successfully. 763 */ 764 inline bool SendTimingEvent(uint32_t innerEventId, int64_t taskTime, Priority priority, 765 const Caller &caller = {}) 766 { 767 return SendTimingEvent(InnerEvent::Get(innerEventId, 0, caller), taskTime, priority); 768 } 769 770 /** 771 * Send a timing event. 772 * 773 * @param innerEventId The id of the event. 774 * @param taskTime Process the event at taskTime. 775 * @param priority Priority of the event queue for this event. 776 * @param caller Caller info of the event, default is caller's file, func and line. 777 * @return Returns true if event has been sent successfully. 778 */ 779 inline bool SendTimingEvent(uint32_t innerEventId, int64_t taskTime, const Caller &caller = {}) 780 { 781 return SendTimingEvent(InnerEvent::Get(innerEventId, 0, caller), taskTime, Priority::LOW); 782 } 783 784 /** 785 * Send a timing event. 786 * 787 * @param innerEventId The id of the event. 788 * @param object Shared pointer of object. 789 * @param taskTime Process the event at taskTime. 790 * @param priority Priority of the event queue for this event 791 * @param caller Caller info of the event, default is caller's file, func and line. 792 * @return Returns true if event has been sent successfully. 793 */ 794 template<typename T> 795 inline bool SendTimingEvent(uint32_t innerEventId, const std::shared_ptr<T> &object, int64_t taskTime, 796 Priority priority = Priority::LOW, const Caller &caller = {}) 797 { 798 return SendTimingEvent(InnerEvent::Get(innerEventId, object, 0, caller), taskTime, priority); 799 } 800 801 /** 802 * Send a timing event. 803 * 804 * @param innerEventId The id of the event. 805 * @param object Weak pointer of object. 806 * @param taskTime Process the event at taskTime. 807 * @param priority Priority of the event queue for this event 808 * @param caller Caller info of the event, default is caller's file, func and line. 809 * @return Returns true if event has been sent successfully. 810 */ 811 template<typename T> 812 inline bool SendTimingEvent(uint32_t innerEventId, const std::weak_ptr<T> &object, int64_t taskTime, 813 Priority priority = Priority::LOW, const Caller &caller = {}) 814 { 815 return SendTimingEvent(InnerEvent::Get(innerEventId, object, 0, caller), taskTime, priority); 816 } 817 818 /** 819 * Send a timing event. 820 * 821 * @param innerEventId The id of the event. 822 * @param object Unique pointer of object. 823 * @param taskTime Process the event at taskTime. 824 * @param priority Priority of the event queue for this event 825 * @param caller Caller info of the event, default is caller's file, func and line. 826 * @return Returns true if event has been sent successfully. 827 */ 828 template<typename T, typename D> 829 inline bool SendTimingEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, int64_t taskTime, 830 Priority priority = Priority::LOW, const Caller &caller = {}) 831 { 832 return SendTimingEvent(InnerEvent::Get(innerEventId, object, 0, caller), taskTime, priority); 833 } 834 835 /** 836 * Send a timing event. 837 * 838 * @param innerEventId The id of the event. 839 * @param object Unique pointer of object. 840 * @param taskTime Process the event at taskTime. 841 * @param priority Priority of the event queue for this event 842 * @param caller Caller info of the event, default is caller's file, func and line. 843 * @return Returns true if event has been sent successfully. 844 */ 845 template<typename T, typename D> 846 inline bool SendTimingEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object, int64_t taskTime, 847 Priority priority = Priority::LOW, const Caller &caller = {}) 848 { 849 return SendTimingEvent(InnerEvent::Get(innerEventId, object, 0, caller), taskTime, priority); 850 } 851 852 /** 853 * Post a timing task. 854 * 855 * @param callback Task callback. 856 * @param taskTime Process the event at taskTime. 857 * @param name Name of the task. 858 * @param priority Priority of the event queue for this event. 859 * @param caller Caller info of the event, default is caller's file, func and line. 860 * @return Returns true if task has been sent successfully. 861 */ 862 inline bool PostTimingTask(const Callback &callback, int64_t taskTime, const std::string &name = std::string(), 863 Priority priority = Priority::LOW, const Caller &caller = {}) 864 { 865 return SendTimingEvent(InnerEvent::Get(callback, name, caller), taskTime, priority); 866 } 867 868 /** 869 * Post a timing task. 870 * 871 * @param callback Task callback. 872 * @param taskTime Process the event at taskTime. 873 * @param priority Priority of the event queue for this event. 874 * @param caller Caller info of the event, default is caller's file, func and line. 875 * @return Returns true if task has been sent successfully. 876 */ 877 inline bool PostTimingTask(const Callback &callback, int64_t taskTime, Priority priority = Priority::LOW, 878 const Caller &caller = {}) 879 { 880 return PostTimingTask(callback, taskTime, std::string(), priority, caller); 881 } 882 883 /** 884 * Remove all sent events. 885 */ 886 void RemoveAllEvents(); 887 888 /** 889 * Remove sent events. 890 * 891 * @param innerEventId The id of the event. 892 */ 893 void RemoveEvent(uint32_t innerEventId); 894 895 /** 896 * Remove sent events. 897 * 898 * @param innerEventId The id of the event. 899 * @param param Basic parameter of the event. 900 */ 901 void RemoveEvent(uint32_t innerEventId, int64_t param); 902 903 /** 904 * Remove a task. 905 * 906 * @param name Name of the task. 907 */ 908 void RemoveTask(const std::string &name); 909 910 /** 911 * Remove a task. 912 * 913 * @param name Name of the task. 914 */ 915 int RemoveTaskWithRet(const std::string &name); 916 917 /** 918 * Add file descriptor listener for a file descriptor. 919 * 920 * @param fileDescriptor File descriptor. 921 * @param events Events from file descriptor, such as input, output, error 922 * @param listener Listener callback. 923 * @return Return 'ERR_OK' on success. 924 */ 925 ErrCode AddFileDescriptorListener(int32_t fileDescriptor, uint32_t events, 926 const std::shared_ptr<FileDescriptorListener> &listener, const std::string &taskName); 927 928 /** 929 * Add file descriptor listener for a file descriptor. 930 * 931 * @param fileDescriptor File descriptor. 932 * @param events Events from file descriptor, such as input, output, error 933 * @param listener Listener callback. 934 * @param priority Priority of the for file descriptor. 935 * @return Return 'ERR_OK' on success. 936 */ 937 ErrCode AddFileDescriptorListener(int32_t fileDescriptor, uint32_t events, 938 const std::shared_ptr<FileDescriptorListener> &listener, const std::string &taskName, 939 EventQueue::Priority priority); 940 941 /** 942 * Remove all file descriptor listeners. 943 */ 944 void RemoveAllFileDescriptorListeners(); 945 946 /** 947 * Remove file descriptor listener for a file descriptor. 948 * 949 * @param fileDescriptor File descriptor. 950 */ 951 void RemoveFileDescriptorListener(int32_t fileDescriptor); 952 953 /** 954 * Set the 'EventRunner' to the 'EventHandler'. 955 * 956 * @param runner The 'EventRunner'. 957 */ 958 void SetEventRunner(const std::shared_ptr<EventRunner> &runner); 959 960 /** 961 * Get the 'EventRunner' of the 'EventHandler'. 962 * 963 * @return Return the 'EventRunner'. 964 */ GetEventRunner()965 inline const std::shared_ptr<EventRunner> &GetEventRunner() const 966 { 967 return eventRunner_; 968 } 969 970 /** 971 * Distribute time out handler. 972 * 973 * @param beginTime Dotting before distribution. 974 */ 975 void DistributeTimeoutHandler(const InnerEvent::TimePoint& beginTime); 976 977 /** 978 * Distribute the event. 979 * 980 * @param event The event should be distributed. 981 */ 982 void DistributeEvent(const InnerEvent::Pointer &event); 983 984 /** 985 * Distribute time out action. 986 * 987 * @param event The event should be distribute. 988 * @param nowStart Dotting before distribution. 989 */ 990 void DistributeTimeAction(const InnerEvent::Pointer &event, InnerEvent::TimePoint nowStart); 991 992 /** 993 * Delivery time out action. 994 * 995 * @param event The event should be distribute. 996 * @param nowStart Dotting before distribution. 997 */ 998 void DeliveryTimeAction(const InnerEvent::Pointer &event, InnerEvent::TimePoint nowStart); 999 1000 /** 1001 * Check whether there are events which priority higher than current event. Currently ONLY applicable to 1002 * the main thread, otherwise it will result in problematic returns. 1003 * 1004 * @param priority Current priority. 1005 * @return Return true if there are higher priority events, otherwise return false. 1006 */ 1007 bool HasPendingHigherEvent(int32_t priority = -1); 1008 1009 /** 1010 * Print out the internal information about an object in the specified format, 1011 * helping you diagnose internal errors of the object. 1012 * 1013 * @param dumpr The Dumper object you have implemented to process the output internal information. 1014 */ 1015 void Dump(Dumper &dumper); 1016 1017 /** 1018 * Check whether an event with the given ID can be found among the events that have been sent but not processed. 1019 * 1020 * @param innerEventId The id of the event. 1021 */ 1022 bool HasInnerEvent(uint32_t innerEventId); 1023 1024 /** 1025 * Check whether an event carrying the given param can be found among the events that have been sent but not 1026 * processed. 1027 * 1028 * @param param Basic parameter of the event. 1029 */ 1030 bool HasInnerEvent(int64_t param); 1031 1032 /** 1033 * Check whether an event carrying the given param can be found among the events that have been sent but not 1034 * processed. 1035 * 1036 * @param event InnerEvent whose name is to be obtained. 1037 * @return Returns the task name if the given event contains a specific task; returns the event ID otherwise. 1038 */ 1039 std::string GetEventName(const InnerEvent::Pointer &event); 1040 1041 /** 1042 * Check whether there are events which priority higher than basePrio in subevent queue. 1043 * 1044 * @param basePrio base priority 1045 * @return Return true if there are higher priority events, ohtherwise return false. 1046 */ 1047 bool HasPreferEvent(int basePrio); 1048 1049 /** 1050 * Checks whether the current event handler is idle 1051 * @return Returns true if current event handler is idle otherwise return false. 1052 */ 1053 bool IsIdle(); 1054 1055 /** 1056 * @param enableEventLog dump event log handle time. 1057 */ 1058 void EnableEventLog(bool enableEventLog = false); 1059 1060 /** 1061 * Get handler id, only for inner use 1062 */ GetHandlerId()1063 inline std::string GetHandlerId() 1064 { 1065 return handlerId_; 1066 } 1067 1068 /** 1069 * Get pending task info 1070 */ 1071 PendingTaskInfo QueryPendingTaskInfo(int32_t fileDescriptor); 1072 1073 /** 1074 * queue_cancel_and_wait 1075 */ 1076 void TaskCancelAndWait(); 1077 1078 /** 1079 * Set priority of event at current. 1080 * 1081 * @param priority Priority of event at current. 1082 */ 1083 void SetCurrentEventPriority(int32_t priority = -1); 1084 1085 /** 1086 * Get priority of event at current. 1087 * 1088 * @return Return priority of event at current. 1089 */ 1090 const int32_t &GetCurrentEventPriority(); 1091 1092 /** 1093 * Set the lazy mode for AppVsync. 1094 * @isLazy Lazy or not 1095 */ SetVsyncLazyMode(bool isLazy)1096 static inline void SetVsyncLazyMode(bool isLazy) 1097 { 1098 auto runner = EventRunner::GetMainEventRunner(); 1099 if (runner) { 1100 auto queue = runner->GetEventQueue(); 1101 if (queue) { 1102 queue->SetVsyncLazyMode(isLazy); 1103 } 1104 } 1105 } 1106 1107 /** 1108 * Set the policy of AppVsync. 1109 * @isDynamic dynamic policy or not 1110 */ SetVsyncPolicy(bool isDynamic)1111 static inline void SetVsyncPolicy(bool isDynamic) 1112 { 1113 auto runner = EventRunner::GetMainEventRunner(); 1114 if (runner) { 1115 auto queue = runner->GetEventQueue(); 1116 if (queue) { 1117 queue->SetVsyncWaiter(isDynamic); 1118 } 1119 } 1120 } 1121 1122 protected: 1123 /** 1124 * Process the event. Developers should override this method. 1125 * 1126 * @param event The event should be processed. 1127 */ 1128 virtual void ProcessEvent(const InnerEvent::Pointer &event); 1129 1130 private: 1131 InnerEvent::Pointer CreateTask(const Callback &callback, const std::string &name, 1132 Priority priority, const Caller &caller); 1133 std::string handlerId_; 1134 bool enableEventLog_ {false}; 1135 std::shared_ptr<EventRunner> eventRunner_; 1136 CallbackTimeout deliveryTimeoutCallback_; 1137 CallbackTimeout distributeTimeoutCallback_; 1138 static thread_local std::weak_ptr<EventHandler> currentEventHandler; 1139 static thread_local int32_t currentEventPriority; 1140 }; 1141 } // namespace AppExecFwk 1142 namespace EventHandling = AppExecFwk; 1143 } // namespace OHOS 1144 1145 #endif // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_HANDLER_H 1146