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 
32 #include <unistd.h>
33 #include <MsgTask.h>
34 #include <msg_q.h>
35 #include <log_util.h>
36 #include <loc_log.h>
37 #include <loc_pla.h>
38 
39 namespace loc_util {
40 
41 class MTRunnable : public LocRunnable {
42     const void* mQ;
43 public:
MTRunnable(const void * q)44     inline MTRunnable(const void* q) : mQ(q) {}
45     virtual ~MTRunnable();
46     // Overrides of LocRunnable methods
47     // This method will be repeated called until it returns false; or
48     // until thread is stopped.
49     virtual bool run() override;
50 
51     // The method to be run before thread loop (conditionally repeatedly)
52     // calls run()
53     virtual void prerun() override;
54 
55     // to interrupt the run() method and come out of that
56     virtual void interrupt() override;
57 };
58 
LocMsgDestroy(void * msg)59 static void LocMsgDestroy(void* msg) {
60     delete (LocMsg*)msg;
61 }
62 
MsgTask(const char * threadName)63 MsgTask::MsgTask(const char* threadName) :
64     mQ(msg_q_init2()), mThread() {
65     mThread.start(threadName, std::make_shared<MTRunnable>(mQ));
66 }
67 
sendMsg(const LocMsg * msg) const68 void MsgTask::sendMsg(const LocMsg* msg) const {
69     if (msg && this) {
70         msg_q_snd((void*)mQ, (void*)msg, LocMsgDestroy);
71     } else {
72         LOC_LOGE("%s: msg is %p and this is %p",
73                  __func__, msg, this);
74     }
75 }
76 
sendMsg(const std::function<void ()> runnable) const77 void MsgTask::sendMsg(const std::function<void()> runnable) const {
78     struct RunMsg : public LocMsg {
79         const std::function<void()> mRunnable;
80     public:
81         inline RunMsg(const std::function<void()> runnable) : mRunnable(runnable) {}
82         ~RunMsg() = default;
83         inline virtual void proc() const override { mRunnable(); }
84     };
85     sendMsg(new RunMsg(runnable));
86 }
87 
interrupt()88 void MTRunnable::interrupt() {
89     msg_q_unblock((void*)mQ);
90 }
91 
prerun()92 void MTRunnable::prerun() {
93     // make sure we do not run in background scheduling group
94      set_sched_policy(gettid(), SP_FOREGROUND);
95 }
96 
run()97 bool MTRunnable::run() {
98     LocMsg* msg;
99     msq_q_err_type result = msg_q_rcv((void*)mQ, (void **)&msg);
100     if (eMSG_Q_SUCCESS != result) {
101         LOC_LOGE("%s:%d] fail receiving msg: %s\n", __func__, __LINE__,
102                  loc_get_msg_q_status(result));
103         return false;
104     }
105 
106     msg->log();
107     // there is where each individual msg handling is invoked
108     msg->proc();
109 
110     delete msg;
111 
112     return true;
113 }
114 
~MTRunnable()115 MTRunnable::~MTRunnable() {
116     msg_q_flush((void*)mQ);
117     msg_q_destroy((void**)&mQ);
118 }
119 
120 } // namespace loc_util
121