MsgTask.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright (c) 2011-2013, 2015, 2017, 2020 The Linux Foundation. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are
  5. * met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above
  9. * copyright notice, this list of conditions and the following
  10. * disclaimer in the documentation and/or other materials provided
  11. * with the distribution.
  12. * * Neither the name of The Linux Foundation, nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  20. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  25. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  26. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. */
  29. #define LOG_NDEBUG 0
  30. #define LOG_TAG "LocSvc_MsgTask"
  31. #include <unistd.h>
  32. #include <MsgTask.h>
  33. #include <msg_q.h>
  34. #include <log_util.h>
  35. #include <loc_log.h>
  36. #include <loc_pla.h>
  37. namespace loc_util {
  38. class MTRunnable : public LocRunnable {
  39. const void* mQ;
  40. public:
  41. inline MTRunnable(const void* q) : mQ(q) {}
  42. virtual ~MTRunnable();
  43. // Overrides of LocRunnable methods
  44. // This method will be repeated called until it returns false; or
  45. // until thread is stopped.
  46. virtual bool run() override;
  47. // The method to be run before thread loop (conditionally repeatedly)
  48. // calls run()
  49. virtual void prerun() override;
  50. // to interrupt the run() method and come out of that
  51. virtual void interrupt() override;
  52. };
  53. static void LocMsgDestroy(void* msg) {
  54. delete (LocMsg*)msg;
  55. }
  56. MsgTask::MsgTask(const char* threadName) :
  57. mQ(msg_q_init2()), mThread() {
  58. mThread.start(threadName, std::make_shared<MTRunnable>(mQ));
  59. }
  60. void MsgTask::sendMsg(const LocMsg* msg) const {
  61. if (msg && this) {
  62. msg_q_snd((void*)mQ, (void*)msg, LocMsgDestroy);
  63. } else {
  64. LOC_LOGE("%s: msg is %p and this is %p",
  65. __func__, msg, this);
  66. }
  67. }
  68. void MsgTask::sendMsg(const std::function<void()> runnable) const {
  69. struct RunMsg : public LocMsg {
  70. const std::function<void()> mRunnable;
  71. public:
  72. inline RunMsg(const std::function<void()> runnable) : mRunnable(runnable) {}
  73. ~RunMsg() = default;
  74. inline virtual void proc() const override { mRunnable(); }
  75. };
  76. sendMsg(new RunMsg(runnable));
  77. }
  78. void MTRunnable::interrupt() {
  79. msg_q_unblock((void*)mQ);
  80. }
  81. void MTRunnable::prerun() {
  82. // make sure we do not run in background scheduling group
  83. set_sched_policy(gettid(), SP_FOREGROUND);
  84. }
  85. bool MTRunnable::run() {
  86. LocMsg* msg;
  87. msq_q_err_type result = msg_q_rcv((void*)mQ, (void **)&msg);
  88. if (eMSG_Q_SUCCESS != result) {
  89. LOC_LOGE("%s:%d] fail receiving msg: %s\n", __func__, __LINE__,
  90. loc_get_msg_q_status(result));
  91. return false;
  92. }
  93. msg->log();
  94. // there is where each individual msg handling is invoked
  95. msg->proc();
  96. delete msg;
  97. return true;
  98. }
  99. MTRunnable::~MTRunnable() {
  100. msg_q_flush((void*)mQ);
  101. msg_q_destroy((void**)&mQ);
  102. }
  103. } // namespace loc_util