Re-organized sub-directory by category
[staging/basesystem.git] / service / native / framework_unified / client / NS_FrameworkCore / src / statemachine / frameworkunified_sm_multithreading.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 //////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_NSFramework
19 /// \brief   This file has the function implementation for creating state machine child thread
20 ///
21 ///
22 //////////////////////////////////////////////////////////////////////////////////////////////////
23 #include <sys/prctl.h>
24
25 #include <native_service/frameworkunified_sm_hsmframework.h>
26 #include <native_service/frameworkunified_sm_framework_dispatch.h>
27 #include <native_service/frameworkunified_framework_if.h>
28 #include <native_service/ns_utility.hpp>
29 #include <native_service/ns_system_mode.h>
30 #include <native_service/frameworkunified_sm_multithreading.h>
31 #include <native_service/frameworkunified_multithreading.h>
32
33 #include <iostream>
34 #include <string>
35
36 #include "frameworkunified_framework_utility.h"
37 #include "frameworkunified_framework_core.h"
38 #include "frameworkunified_framework_internal.h"
39
40 ////////////////////////////////////////////////////////////////////////////////////////////
41 /// FrameworkunifiedCreateHSMDispatcherChild
42 ////////////////////////////////////////////////////////////////////////////////////////////
43 EFrameworkunifiedStatus FrameworkunifiedCreateHSMDispatcherChild(PCSTR childName,
44                                        PCSTR parentName,
45                                        HANDLE &hChildApp,  // NOLINT  (readability/nolint)
46                                        CbFuncPtr pOnThreadStart,
47                                        CbFuncPtr pOnThreadStop) {
48   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
49
50   if ((NULL != childName) && (NULL != parentName) && (strlen(childName) <= MAX_NAME_SIZE_APP) &&
51       (strlen(parentName) <= MAX_NAME_SIZE_APP)) {
52     if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedCreateHSMDispatcher(childName, hChildApp, TRUE))) {
53       if (frameworkunifiedCheckValidAppHandle(hChildApp)) {
54         CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast< CFrameworkunifiedFrameworkApp * >(hChildApp);
55
56         (reinterpret_cast<CFrameworkunifiedHSMChildFramework *>(pApp->m_pFrameworkunifiedStateMachine))->m_fpStartThread
57           = pOnThreadStart;
58
59         (reinterpret_cast<CFrameworkunifiedHSMChildFramework *>(pApp->m_pFrameworkunifiedStateMachine))->m_fpStopThread
60           = pOnThreadStop;
61
62         pApp->hParentSndMsgQ = McOpenSender(parentName);
63         if (NULL == pApp->hParentSndMsgQ) {
64             eStatus = eFrameworkunifiedStatusNullPointer;
65             FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "McOpenSender failed");
66             return eStatus;
67         }
68         pApp->uiSessionId = THREAD_SESSION_ID;
69
70         memset(pApp->cParentAppName, 0, sizeof(pApp->cParentAppName));
71         memcpy(pApp->cParentAppName, parentName, strlen(parentName));
72       } else {
73         eStatus = eFrameworkunifiedStatusNullPointer;
74         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "hChildApp is NULL");
75       }
76     } else {
77       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
78       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateHSMDispatcher error, status=%d", eStatus);
79       // LCOV_EXCL_BR_STOP
80     }
81   } else {
82     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Invalid Param received");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
83     eStatus = eFrameworkunifiedStatusInvldParam;
84   }
85
86   return eStatus;
87 }
88
89 void *child_hsm_thread_proc(void *args) {
90   if (args == NULL) {
91     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "args is NULL");
92     return NULL;
93   }
94   PCData pcdata = *reinterpret_cast< PCData * >(args);   // Create a local copy of data
95
96   try {
97     EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
98
99     HANDLE hFrameworkApp = NULL;
100
101     if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedCreateHSMDispatcherChild(pcdata.childName.c_str(),
102                                                                pcdata.parentName.c_str(),
103                                                                hFrameworkApp,
104                                                                pcdata.initFn,
105                                                                pcdata.shdnFn))) {
106       if (NULL != hFrameworkApp) {
107         THApp hChildApp(hFrameworkApp);
108
109         if (pcdata.CbCreateStateMachine) {
110           pcdata.CbCreateStateMachine(hChildApp);
111           FRAMEWORKUNIFIED_PRINT_HSM(hChildApp)
112         }
113         const FrameworkunifiedProtocolEvent pcbhs[] = { { SYSTEM_ON_INITIALIZATION,  FRAMEWORKUNIFIED_EVENT(evFrameworkunifiedStart) },
114           { SYSTEM_ON_SHUTDOWN,    FRAMEWORKUNIFIED_EVENT(evFrameworkunifiedStop) },
115           { SYSTEM_ON_DESTROY,         FRAMEWORKUNIFIED_EVENT(evFrameworkunifiedDestroy) }
116         };
117
118         if (eFrameworkunifiedStatusOK != FrameworkunifiedAttachHSMEventsToDispatcher(hChildApp,
119                                                            pcdata.parentName.c_str(),
120                                                            &pcbhs[ 0 ],
121                                                            static_cast<UI_32>(_countof(pcbhs)),
122                                                            NULL)) {
123           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to attach hsm events to child thread %s",
124             pcdata.childName.c_str());
125         }
126
127         // set child thread name
128         char thread_name[16];
129         strncpy(thread_name, pcdata.childName.c_str(), sizeof(thread_name));
130         prctl(PR_SET_NAME, thread_name);
131         thread_name[15] = '\0';
132
133         setChildThreadSched(pcdata.schedPolicy, pcdata.schedPriority);
134
135         (FrameworkunifiedGetStateMachine(hChildApp))->FrameworkunifiedStart();
136
137         *pcdata.childStatus = eFrameworkunifiedStatusOK;
138         if (IsValidWaitBarrier(pthread_barrier_wait(pcdata.barrier))) {
139           RunChildDispatcher(hChildApp);
140         }
141       } else {
142         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "hFrameworkApp is NULL");
143       }
144     } else {
145       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
146       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateHSMDispatcherChild error, status=%d", eStatus);
147       // LCOV_EXCL_BR_STOP
148
149       *pcdata.childStatus = eFrameworkunifiedStatusFail;
150       pthread_barrier_wait(pcdata.barrier);
151     }
152   } catch (const THApp::Exception &) {
153     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to create child %s", pcdata.childName.c_str());
154   }
155
156   return NULL;
157 }
158
159
160
161 ////////////////////////////////////////////////////////////////////////////////////////////
162 /// FrameworkunifiedCreateHSMChildThread
163 ////////////////////////////////////////////////////////////////////////////////////////////
164 HANDLE FrameworkunifiedCreateHSMChildThread(HANDLE hApp, PCSTR childName, CbFuncPtr CbInitialize,
165                                CbFuncPtr CbShutdown, CbFuncPtr CbCreateStateMachine) {
166   HANDLE hChildQ = NULL;
167   FrameworkunifiedChildThreadAttr attr;
168
169   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != childName && NULL != CbInitialize && NULL != CbShutdown &&
170       NULL != CbCreateStateMachine) {
171     CreateChildThreadAttrInit(&attr);
172     hChildQ = CreateChildThread(hApp, childName, CbInitialize, CbShutdown, &attr, CbCreateStateMachine);
173   }
174
175   return hChildQ;
176 }
177
178 ////////////////////////////////////////////////////////////////////////////////////////////
179 /// FrameworkunifiedCreateHSMChildThreadWithPriority
180 ////////////////////////////////////////////////////////////////////////////////////////////
181 HANDLE FrameworkunifiedCreateHSMChildThreadWithPriority(HANDLE hApp, PCSTR childName, CbFuncPtr CbInitialize,
182                                            CbFuncPtr CbShutdown, CbFuncPtr CbCreateStateMachine, SI_32 schedPrio) {
183   HANDLE hChildQ = NULL;
184   FrameworkunifiedChildThreadAttr attr;
185
186   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != childName && NULL != CbInitialize && NULL != CbShutdown &&
187       NULL != CbCreateStateMachine) {
188     CreateChildThreadAttrInit(&attr);
189     CreateChildThreadAttrSetSched(&attr, eFrameworkunifiedSchedPolicyRR, schedPrio);
190     hChildQ = CreateChildThread(hApp, childName, CbInitialize, CbShutdown, &attr, CbCreateStateMachine);
191   }
192   return hChildQ;
193 }
194
195 ////////////////////////////////////////////////////////////////////////////////////////////
196 /// FrameworkunifiedCreateHSMChildThreadWithAttribute
197 ////////////////////////////////////////////////////////////////////////////////////////////
198 HANDLE FrameworkunifiedCreateHSMChildThreadWithAttribute(HANDLE hApp,
199                                             PCSTR childName,
200                                             CbFuncPtr CbInitialize,
201                                             CbFuncPtr CbShutdown,
202                                             CbFuncPtr CbCreateStateMachine,
203                                             const FrameworkunifiedChildThreadAttr *attr) {
204   HANDLE hChildQ = NULL;
205
206   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != childName && NULL != CbInitialize && NULL != CbShutdown &&
207       NULL != CbCreateStateMachine) {
208     hChildQ = CreateChildThread(hApp, childName, CbInitialize, CbShutdown, attr, CbCreateStateMachine);
209   }
210   return hChildQ;
211 }