• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2011-2013, 2015, 2017The 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 
LocMsgDestroy(void * msg)39 static void LocMsgDestroy(void* msg) {
40     delete (LocMsg*)msg;
41 }
42 
MsgTask(LocThread::tCreate tCreator,const char * threadName,bool joinable)43 MsgTask::MsgTask(LocThread::tCreate tCreator,
44                  const char* threadName, bool joinable) :
45     mQ(msg_q_init2()), mThread(new LocThread()) {
46     if (!mThread->start(tCreator, threadName, this, joinable)) {
47         delete mThread;
48         mThread = NULL;
49     }
50 }
51 
MsgTask(const char * threadName,bool joinable)52 MsgTask::MsgTask(const char* threadName, bool joinable) :
53     mQ(msg_q_init2()), mThread(new LocThread()) {
54     if (!mThread->start(threadName, this, joinable)) {
55         delete mThread;
56         mThread = NULL;
57     }
58 }
59 
~MsgTask()60 MsgTask::~MsgTask() {
61     msg_q_flush((void*)mQ);
62     msg_q_destroy((void**)&mQ);
63 }
64 
destroy()65 void MsgTask::destroy() {
66     LocThread* thread = mThread;
67     msg_q_unblock((void*)mQ);
68     if (thread) {
69         mThread = NULL;
70         delete thread;
71     } else {
72         delete this;
73     }
74 }
75 
sendMsg(const LocMsg * msg) const76 void MsgTask::sendMsg(const LocMsg* msg) const {
77     if (msg && this) {
78         msg_q_snd((void*)mQ, (void*)msg, LocMsgDestroy);
79     } else {
80         LOC_LOGE("%s: msg is %p and this is %p",
81                  __func__, msg, this);
82     }
83 }
84 
prerun()85 void MsgTask::prerun() {
86 #ifndef FEATURE_EXTERNAL_AP
87     // make sure we do not run in background scheduling group
88      set_sched_policy(gettid(), SP_FOREGROUND);
89 #endif /* FEATURE_EXTERNAL_AP */
90 }
91 
run()92 bool MsgTask::run() {
93     LocMsg* msg;
94     msq_q_err_type result = msg_q_rcv((void*)mQ, (void **)&msg);
95     if (eMSG_Q_SUCCESS != result) {
96         LOC_LOGE("%s:%d] fail receiving msg: %s\n", __func__, __LINE__,
97                  loc_get_msg_q_status(result));
98         return false;
99     }
100 
101     msg->log();
102     // there is where each individual msg handling is invoked
103     msg->proc();
104 
105     delete msg;
106 
107     return true;
108 }
109