2 * =====================================================================================
6 * Description: Thread implementation
9 * Created: 26 March 2015 12:41:39 IST
13 * Author: CHERYL (cb), cheryl.b@samsung.com
14 * Organization: Samsung Electronics
16 * =====================================================================================
19 /*-----------------------------------------------------------------------------
21 *-----------------------------------------------------------------------------*/
22 #include "OsaLinuxUser.h"
24 /*-----------------------------------------------------------------------------
26 *-----------------------------------------------------------------------------*/
27 #define TASK_COMM_LEN 16
29 /*-----------------------------------------------------------------------------
31 *-----------------------------------------------------------------------------*/
32 typedef void (*pEntry_f)(void*);
35 char aName[TASK_COMM_LEN];
40 /*-----------------------------------------------------------------------------
42 *-----------------------------------------------------------------------------*/
43 static void* _thread_start_handler(void* pArg) {
45 ThreadParam_t sThreadParam;
50 sThreadParam = *((ThreadParam_t*)pArg);
53 iRet = prctl(PR_SET_NAME, sThreadParam.aName, 0, 0, 0);
55 perror("In OsaTaskSpawn() : prctl() Failed\n ");
56 //PrintError("In OsaTaskSpawn() : prctl() error no. : %d\n", iRet);
59 (*sThreadParam.pEntryFunc)(sThreadParam.pArg);
65 //-----------------------------------------------------------------------------
66 // Function Name : OsaTaskSpawn
67 // Detail Description : This API function creates and activates a new task
68 // with a specified priority and options. It returns
69 // a system-assigned ID.
71 // Return Data Type : ErrorType
73 // Programming Note : None.
74 //------------------------------------------------------------------------------
77 int OsaTaskSpawn(const char* pName, unsigned int* puiTid, int iPriority,
78 int iStacksize, unsigned int uiMode, void* pEntryPt, int iArg1, int iArg2,
79 int iArg3, int iArg4, void* pvArg5) {
81 #define OSAL_SCHED_POLICY SCHED_FIFO
84 pthread_t curThread = pthread_self();
85 pthread_t createThread;
86 pthread_attr_t tattr_t;
87 struct sched_param param_t;
89 int createThreadPolicy = OSAL_SCHED_POLICY;
90 ThreadParam_t* pThreadParam;
92 pthread_getschedparam(curThread, &curThreadPolicy, ¶m_t);
95 createThreadPolicy = SCHED_OTHER;
96 } else if (iPriority > sched_get_priority_max(OSAL_SCHED_POLICY)) {
97 iPriority = sched_get_priority_max(OSAL_SCHED_POLICY);
98 } else if (iPriority < sched_get_priority_min(OSAL_SCHED_POLICY)) {
99 iPriority = sched_get_priority_min(OSAL_SCHED_POLICY);
102 if (iStacksize < OSAL_DEFAULT_STSZ) {
103 iStacksize = OSAL_DEFAULT_STSZ;
106 /* set thread parameters: name, entry func and argument. */
107 pThreadParam = (ThreadParam_t*)malloc(sizeof(ThreadParam_t));
109 if (NULL == pThreadParam) {
110 perror("Memory Allocation Failed : \n");
114 memset(pThreadParam->aName, 0x00, TASK_COMM_LEN);
115 strncpy(pThreadParam->aName, pName, (TASK_COMM_LEN - 1));
116 pThreadParam->pEntryFunc = (pEntry_f)pEntryPt;
117 pThreadParam->pArg = pvArg5;
119 /* set stack size : 16Kbyte */
120 if (iStacksize <= PTHREAD_STACK_MIN) {
121 iRet = pthread_create(&createThread, (pthread_attr_t *)NULL,
122 _thread_start_handler, (void*)pThreadParam);
124 *puiTid = (unsigned int)NULL;
126 perror("In OsaTaskSpawn() : pthread create Failed\n ");
127 //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
131 // set Stakc size by iStacksize, using pthread_attr_t
133 iRet = pthread_attr_init(&tattr_t);
135 *puiTid = (unsigned int)NULL;
137 perror("In OsaTaskSpawn() : pthread attr init Failed\n ");
138 //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
141 iRet = pthread_attr_setstacksize(&tattr_t, (unsigned int)iStacksize);
143 *puiTid = (unsigned int)NULL;
145 perror("In OsaTaskSpawn() : pthread attr setstacksize Failed\n ");
146 //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
147 pthread_attr_destroy(&tattr_t);
150 iRet = pthread_create(&createThread, (pthread_attr_t *)&tattr_t,
151 _thread_start_handler, (void*)pThreadParam);
153 *puiTid = (unsigned int)NULL;
155 perror("In OsaTaskSpawn() : pthread create Failed\n ");
156 //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
157 pthread_attr_destroy(&tattr_t);
161 pthread_attr_destroy(&tattr_t);
164 /* Set Priority by iPriority , if different from parent priority */
165 if (iPriority != param_t.__sched_priority) {
166 param_t.__sched_priority = iPriority;
167 iRet = pthread_setschedparam(createThread, createThreadPolicy, ¶m_t);
169 *puiTid = (unsigned int)NULL;
170 perror("In OsaTaskSpawn() : pthread setschedparam Failed\n ");
171 //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
172 pthread_kill(createThread, (int)NULL);
177 iRet = pthread_detach(createThread);
179 *puiTid = (unsigned int)NULL;
180 perror("In OsaTaskSpawn() : pthread_detach Failed\n ");
181 //PrintError("In OsaTaskSpawn() : detach error no. : %d\n", iRet);
182 pthread_kill(createThread, (int)NULL);
185 *puiTid = (unsigned int)createThread;
187 //PrintDbg("%s thread created policy: %d, priority %d\n", pName, createThreadPolicy, iPriority);
193 //-----------------------------------------------------------------------------
194 // Function Name : OsaExit
195 // Detail Description : This function terminates the calling thread.
197 // Return Data Type : Void
199 // Programming Note : None.
200 //------------------------------------------------------------------------------
203 void OsaExit(int iStatus) {
209 //-----------------------------------------------------------------------------
210 // Function Name : OsaTaskDelete
211 // Detail Description : This function terminates the thread having the
212 // corresponding thread ID .
214 // Return Data Type : ErrorType
216 // Programming Note : None
217 //------------------------------------------------------------------------------
220 int OsaTaskDelete(unsigned int uiTid) {
222 iRet = pthread_cancel((pthread_t)uiTid);
224 perror("In OsaTaskDelete() : TaskDelete Failed ");
225 //PrintError("In OsaTaskDelete() : error no. : %d\n",errno);
233 //-----------------------------------------------------------------------------
234 // Function Name : OsaTaskSetPriority
235 // Detail Description : This function sets the Priority for the created thread.
237 // Return Data Type : ErrorType
239 // Programming Note : None.
240 //------------------------------------------------------------------------------
243 int OsaTaskSetPriority(unsigned int uiTid, int iNewpriority) {
245 struct sched_param param_t;
246 /* sched_priority will be the priority of the thread */
247 param_t.sched_priority = iNewpriority;
248 /* only supported policy, others will result in ENOTSUP */
249 iPolicy = SCHED_FIFO;
250 /* scheduling parameters of target thread */
251 iRet = pthread_setschedparam(uiTid, iPolicy, ¶m_t);
253 perror("In OsaTaskSetPriority() : TaskSetPriority set Failed ");
254 //PrintError("In OsaTaskSetPriority() : error no. : %d\n",errno);
262 //-----------------------------------------------------------------------------
263 // Function Name : OsaTaskGetPriority
264 // Detail Description : This function gets the corresponding priority of the
265 // supplied thread ID .
267 // Return Data Type : ErrorType
269 // Programming Note : None.
270 //------------------------------------------------------------------------------
274 int OsaTaskGetPriority(unsigned int uiTid, int* piPriority) {
275 int iRet, iPolicy = 0;
276 struct sched_param param_t;
277 iRet = pthread_getschedparam(uiTid, (int *)&iPolicy, ¶m_t);
280 perror("In OsaTaskGetPriority() : TaskGetPriority Failed ");
281 //PrintError("In OsaTaskGetPriority() : error no. : %d\n",errno);
284 *piPriority = param_t.sched_priority;
290 //-----------------------------------------------------------------------------
291 // Function Name : OsaTaskNanosleep
292 // Detail Description : This function makes the thread to sleep for nanosec
295 // Return Data Type : ErrorType
297 // Programming Note : None.
298 //------------------------------------------------------------------------------
302 int OsaTaskNanosleep(int iNanosec) {
303 struct timespec timeSpec_t, timeSpecRem_t;
306 timeSpec_t.tv_sec = 0;
308 if (iNanosec > OSAL_NSEC_MAX) {
309 iNanosec = OSAL_NSEC_MAX;
312 timeSpec_t.tv_nsec = iNanosec;
314 iRetval = nanosleep(&timeSpec_t, &timeSpecRem_t);
317 perror("TaskNanoSleep: TaskNanoSleep Failed ");
318 //PrintError("Error No. : %d\n",errno);
328 //-----------------------------------------------------------------------------
329 // Function Name : OsaTaskGetId
330 // Detail Description : This function get the current thread ID.
332 // Return Data Type : Current thread ID .
334 // Programming Note : None.
335 //------------------------------------------------------------------------------
338 int OsaTaskGetId(void) {
339 return ((pthread_t)pthread_self());
344 //-----------------------------------------------------------------------------
345 // Function Name : OsaTaskDelaymsecs
346 // Detail Description : This function makes the thread to sleep for millisec
349 // Return Data Type : ErrorType
351 // Programming Note : None.
352 //------------------------------------------------------------------------------
355 int OsaTaskDelaymsecs(unsigned int uiMsec) {
356 unsigned int uiDiv, uiRem;
357 struct timespec timeSpec_t, timeSpecRem_t;
360 uiDiv = uiMsec / 1000;
361 uiRem = uiMsec % 1000;
363 timeSpec_t.tv_sec = uiDiv;
364 timeSpec_t.tv_nsec = uiRem * 1000000;
366 iRetval = nanosleep(&timeSpec_t, &timeSpecRem_t);
369 perror("In OsaTaskDelaymsecs() : TaskNanoSleep Failed ");
370 //PrintError("In OsaTaskDelaymsecs() : error no. : %d\n",errno);
376 /*****************************************************************************
377 ** OsaTaskDelayticks -
378 *****************************************************************************/
379 int OsaTaskDelayticks(int iTicks) {
380 struct timespec ts, tsr;
384 hz = OsaGetTicksPerSecond();
385 ts.tv_sec = iTicks / hz;
386 ts.tv_nsec = (iTicks % hz) * (1000 * 1000 * 1000 / hz);
389 ret = nanosleep(&ts, &tsr);
392 } else if (errno == EINTR) {
400 /*****************************************************************************
402 *****************************************************************************/
403 int OsaTaskSuspend(unsigned int uiTid) {
409 /*****************************************************************************
411 *****************************************************************************/
412 int OsaTaskResume(unsigned int uiTid) {
418 /*****************************************************************************
420 *****************************************************************************/
421 int OsaTaskRestart(unsigned int uiTid) {