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
39namespace loc_core {
40
41#define MAX_TASK_COMM_LEN 15
42
43static void LocMsgDestroy(void* msg) {
44    delete (LocMsg*)msg;
45}
46
47MsgTask::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
57MsgTask::MsgTask(tAssociate tAssociator, const char* threadName) :
58    mQ(msg_q_init2()), mAssociator(tAssociator){
59    createPThread(threadName);
60}
61
62inline
63MsgTask::MsgTask(const void* q, tAssociate associator) :
64    mQ(q), mAssociator(associator){
65}
66
67MsgTask::~MsgTask() {
68    msg_q_unblock((void*)mQ);
69}
70
71void MsgTask::createPThread(const char* threadName) {
72    pthread_attr_t attr;
73    pthread_attr_init(&attr);
74    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
75
76    pthread_t tid;
77    // create the thread here, then if successful
78    // and a name is given, we set the thread name
79    if (!pthread_create(&tid, &attr, loopMain,
80                        (void*)new MsgTask(mQ, mAssociator)) &&
81        NULL != threadName) {
82        char lname[MAX_TASK_COMM_LEN+1];
83        memcpy(lname, threadName, MAX_TASK_COMM_LEN);
84        lname[MAX_TASK_COMM_LEN] = 0;
85        pthread_setname_np(tid, lname);
86    }
87}
88
89void MsgTask::sendMsg(const LocMsg* msg) const {
90    msg_q_snd((void*)mQ, (void*)msg, LocMsgDestroy);
91}
92
93void* MsgTask::loopMain(void* arg) {
94    MsgTask* copy = (MsgTask*)arg;
95
96    // make sure we do not run in background scheduling group
97    set_sched_policy(gettid(), SP_FOREGROUND);
98
99    if (NULL != copy->mAssociator) {
100        copy->mAssociator();
101    }
102
103    LocMsg* msg;
104    int cnt = 0;
105
106    while (1) {
107        LOC_LOGD("MsgTask::loop() %d listening ...\n", cnt++);
108
109        msq_q_err_type result = msg_q_rcv((void*)copy->mQ, (void **)&msg);
110
111        if (eMSG_Q_SUCCESS != result) {
112            LOC_LOGE("%s:%d] fail receiving msg: %s\n", __func__, __LINE__,
113                     loc_get_msg_q_status(result));
114            // destroy the Q and exit
115            msg_q_destroy((void**)&(copy->mQ));
116            delete copy;
117            return NULL;
118        }
119
120        msg->log();
121        // there is where each individual msg handling is invoked
122        msg->proc();
123
124        delete msg;
125    }
126
127    delete copy;
128
129    return NULL;
130}
131
132}
133