• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2011-2013, 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_NDDEBUG 0
30 #define LOG_TAG "LocSvc_MsgTask"
31 
32 #include <cutils/sched_policy.h>
33 #include <unistd.h>
34 #include <MsgTask.h>
35 #include <msg_q.h>
36 #include <log_util.h>
37 #include <loc_log.h>
38 
39 namespace loc_core {
40 
41 #define MAX_TASK_COMM_LEN 15
42 
LocMsgDestroy(void * msg)43 static void LocMsgDestroy(void* msg) {
44     delete (LocMsg*)msg;
45 }
46 
MsgTask(tCreate tCreator,const char * threadName)47 MsgTask::MsgTask(tCreate tCreator, const char* threadName) :
48     mQ(msg_q_init2()), mAssociator(NULL){
49     if (tCreator) {
50         tCreator(threadName, loopMain,
51                  (void*)new MsgTask(mQ, mAssociator));
52     } else {
53         createPThread(threadName);
54     }
55 }
56 
MsgTask(tAssociate tAssociator,const char * threadName)57 MsgTask::MsgTask(tAssociate tAssociator, const char* threadName) :
58     mQ(msg_q_init2()), mAssociator(tAssociator){
59     createPThread(threadName);
60 }
61 
62 inline
MsgTask(const void * q,tAssociate associator)63 MsgTask::MsgTask(const void* q, tAssociate associator) :
64     mQ(q), mAssociator(associator){
65 }
66 
~MsgTask()67 MsgTask::~MsgTask() {
68     msg_q_unblock((void*)mQ);
69 }
70 
associate(tAssociate tAssociator) const71 void MsgTask::associate(tAssociate tAssociator) const {
72     struct LocAssociateMsg : public LocMsg {
73         tAssociate mAssociator;
74         inline LocAssociateMsg(tAssociate associator) :
75             LocMsg(), mAssociator(associator) {}
76         inline virtual void proc() const {
77             if (mAssociator) {
78                 LOC_LOGD("MsgTask::associate");
79                 mAssociator();
80             }
81         }
82     };
83     sendMsg(new LocAssociateMsg(tAssociator));
84 }
85 
createPThread(const char * threadName)86 void MsgTask::createPThread(const char* threadName) {
87     pthread_attr_t attr;
88     pthread_attr_init(&attr);
89     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
90 
91     pthread_t tid;
92     // create the thread here, then if successful
93     // and a name is given, we set the thread name
94     if (!pthread_create(&tid, &attr, loopMain,
95                         (void*)new MsgTask(mQ, mAssociator)) &&
96         NULL != threadName) {
97         char lname[MAX_TASK_COMM_LEN+1];
98         memcpy(lname, threadName, MAX_TASK_COMM_LEN);
99         lname[MAX_TASK_COMM_LEN] = 0;
100         pthread_setname_np(tid, lname);
101     }
102 }
103 
sendMsg(const LocMsg * msg) const104 void MsgTask::sendMsg(const LocMsg* msg) const {
105     msg_q_snd((void*)mQ, (void*)msg, LocMsgDestroy);
106 }
107 
loopMain(void * arg)108 void* MsgTask::loopMain(void* arg) {
109     MsgTask* copy = (MsgTask*)arg;
110 
111     // make sure we do not run in background scheduling group
112     set_sched_policy(gettid(), SP_FOREGROUND);
113 
114     if (NULL != copy->mAssociator) {
115         copy->mAssociator();
116     }
117 
118     LocMsg* msg;
119     int cnt = 0;
120 
121     while (1) {
122         LOC_LOGD("MsgTask::loop() %d listening ...\n", cnt++);
123 
124         msq_q_err_type result = msg_q_rcv((void*)copy->mQ, (void **)&msg);
125 
126         if (eMSG_Q_SUCCESS != result) {
127             LOC_LOGE("%s:%d] fail receiving msg: %s\n", __func__, __LINE__,
128                      loc_get_msg_q_status(result));
129             // destroy the Q and exit
130             msg_q_destroy((void**)&(copy->mQ));
131             delete copy;
132             return NULL;
133         }
134 
135         msg->log();
136         // there is where each individual msg handling is invoked
137         msg->proc();
138 
139         delete msg;
140     }
141 
142     delete copy;
143 
144     return NULL;
145 }
146 
147 }
148