Fix source file permissions in the project
[platform/core/security/tef-simulator.git] / osal / OsaTask.c
1 /*
2  * =====================================================================================
3  *
4  *       Filename:  OsaTask.c
5  *
6  *    Description:  Thread implementation
7  *
8  *        Version:  1.0
9  *        Created:  26 March 2015 12:41:39  IST
10  *       Revision:  Original
11  *       Compiler:  gcc
12  *
13  *         Author:  CHERYL (cb), cheryl.b@samsung.com
14  *   Organization:  Samsung Electronics
15  *
16  * =====================================================================================
17  */
18
19 /*-----------------------------------------------------------------------------
20  *  Include files
21  *-----------------------------------------------------------------------------*/
22 #include "OsaLinuxUser.h"
23
24 /*-----------------------------------------------------------------------------
25  *  MACROS
26  *-----------------------------------------------------------------------------*/
27 #define TASK_COMM_LEN  16
28
29 /*-----------------------------------------------------------------------------
30  *  Globals
31  *-----------------------------------------------------------------------------*/
32 typedef void (*pEntry_f)(void*);
33
34 typedef struct {
35         char aName[TASK_COMM_LEN];
36         pEntry_f pEntryFunc;
37         void* pArg;
38 } ThreadParam_t;
39
40 /*-----------------------------------------------------------------------------
41  *  Functions
42  *-----------------------------------------------------------------------------*/
43 static void* _thread_start_handler(void* pArg) {
44         int iRet;
45         ThreadParam_t sThreadParam;
46
47         if (NULL == pArg) {
48                 return 0;
49         }
50         sThreadParam = *((ThreadParam_t*)pArg);
51         free(pArg);
52
53         iRet = prctl(PR_SET_NAME, sThreadParam.aName, 0, 0, 0);
54         if (iRet) {
55                 perror("In OsaTaskSpawn() :  prctl() Failed\n ");
56                 //PrintError("In OsaTaskSpawn() : prctl() error no. : %d\n", iRet);
57         }
58
59         (*sThreadParam.pEntryFunc)(sThreadParam.pArg);
60         return 0;
61 }
62
63 /*
64  // $$$
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.
70  //
71  // Return Data Type   : ErrorType
72  //
73  // Programming Note   :        None.
74  //------------------------------------------------------------------------------
75  // $$$
76  */
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) {
80
81 #define OSAL_SCHED_POLICY SCHED_FIFO
82
83         int iRet;
84         pthread_t curThread = pthread_self();
85         pthread_t createThread;
86         pthread_attr_t tattr_t;
87         struct sched_param param_t;
88         int curThreadPolicy;
89         int createThreadPolicy = OSAL_SCHED_POLICY;
90         ThreadParam_t* pThreadParam;
91
92         pthread_getschedparam(curThread, &curThreadPolicy, &param_t);
93
94         if (iPriority == 0) {
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);
100         }
101
102         if (iStacksize < OSAL_DEFAULT_STSZ) {
103                 iStacksize = OSAL_DEFAULT_STSZ;
104         }
105
106         /* set thread parameters: name, entry func and argument. */
107         pThreadParam = (ThreadParam_t*)malloc(sizeof(ThreadParam_t));
108
109         if (NULL == pThreadParam) {
110                 perror("Memory Allocation Failed : \n");
111                 return OSAL_ERROR;
112         }
113
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;
118
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);
123                 if (iRet) {
124                         *puiTid = (unsigned int)NULL;
125                         free(pThreadParam);
126                         perror("In OsaTaskSpawn() :  pthread create Failed\n ");
127                         //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
128                         return ((int)iRet);
129                 }
130         }
131         // set Stakc size by iStacksize, using pthread_attr_t
132         else {
133                 iRet = pthread_attr_init(&tattr_t);
134                 if (iRet) {
135                         *puiTid = (unsigned int)NULL;
136                         free(pThreadParam);
137                         perror("In OsaTaskSpawn() : pthread attr init  Failed\n ");
138                         //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
139                         return ((int)iRet);
140                 }
141                 iRet = pthread_attr_setstacksize(&tattr_t, (unsigned int)iStacksize);
142                 if (iRet) {
143                         *puiTid = (unsigned int)NULL;
144                         free(pThreadParam);
145                         perror("In OsaTaskSpawn() : pthread attr setstacksize Failed\n ");
146                         //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
147                         pthread_attr_destroy(&tattr_t);
148                         return ((int)iRet);
149                 }
150                 iRet = pthread_create(&createThread, (pthread_attr_t *)&tattr_t,
151                     _thread_start_handler, (void*)pThreadParam);
152                 if (iRet) {
153                         *puiTid = (unsigned int)NULL;
154                         free(pThreadParam);
155                         perror("In OsaTaskSpawn() :  pthread create Failed\n ");
156                         //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet);
157                         pthread_attr_destroy(&tattr_t);
158                         return ((int)iRet);
159                 }
160
161                 pthread_attr_destroy(&tattr_t);
162         }
163
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, &param_t);
168                 if (iRet) {
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);
173                         return ((int)iRet);
174                 }
175         }
176
177         iRet = pthread_detach(createThread);
178         if (iRet) {
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);
183                 return ((int)iRet);
184         }
185         *puiTid = (unsigned int)createThread;
186
187         //PrintDbg("%s thread created policy: %d, priority %d\n", pName, createThreadPolicy, iPriority);
188         return OSAL_OK;
189 }
190
191 /*
192  // $$$
193  //-----------------------------------------------------------------------------
194  // Function Name      :        OsaExit 
195  // Detail Description : This function terminates the calling thread.
196  //
197  // Return Data Type   : Void
198  //
199  // Programming Note   : None. 
200  //------------------------------------------------------------------------------
201  // $$$
202  */
203 void OsaExit(int iStatus) {
204         pthread_exit(0);
205 }
206
207 /*
208  // $$$
209  //-----------------------------------------------------------------------------
210  // Function Name      :        OsaTaskDelete 
211  // Detail Description : This function terminates the thread having the 
212  //                             corresponding thread ID .  
213  //
214  // Return Data Type   : ErrorType
215  //
216  // Programming Note   : None
217  //------------------------------------------------------------------------------
218  // $$$
219  */
220 int OsaTaskDelete(unsigned int uiTid) {
221         int iRet = 0;
222         iRet = pthread_cancel((pthread_t)uiTid);
223         if (iRet) {
224                 perror("In OsaTaskDelete() : TaskDelete  Failed ");
225                 //PrintError("In OsaTaskDelete() : error no. : %d\n",errno);
226                 return ((int)errno);
227         }
228         return OSAL_OK;
229 }
230
231 /*
232  // $$$
233  //-----------------------------------------------------------------------------
234  // Function Name      :        OsaTaskSetPriority 
235  // Detail Description : This function sets the Priority for the created thread.
236  //
237  // Return Data Type   : ErrorType
238  //
239  // Programming Note   : None.  
240  //------------------------------------------------------------------------------
241  // $$$
242  */
243 int OsaTaskSetPriority(unsigned int uiTid, int iNewpriority) {
244         int iRet, iPolicy;
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, &param_t);
252         if (iRet) {
253                 perror("In OsaTaskSetPriority() : TaskSetPriority  set Failed ");
254                 //PrintError("In  OsaTaskSetPriority() : error no. : %d\n",errno);
255                 return ((int)errno);
256         }
257         return OSAL_OK;
258 }
259
260 /*
261  // $$$
262  //-----------------------------------------------------------------------------
263  // Function Name      : OsaTaskGetPriority 
264  // Detail Description : This function gets the corresponding priority of the
265  //                             supplied thread ID .  
266  //
267  // Return Data Type   : ErrorType
268  //
269  // Programming Note   :        None.
270  //------------------------------------------------------------------------------
271  // $$$
272  */
273
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, &param_t);
278         if (iRet) {
279                 piPriority = NULL;
280                 perror("In OsaTaskGetPriority() : TaskGetPriority Failed ");
281                 //PrintError("In OsaTaskGetPriority() : error no. : %d\n",errno);
282                 return ((int)errno);
283         }
284         *piPriority = param_t.sched_priority;
285         return OSAL_OK;
286 }
287
288 /*
289  // $$$
290  //-----------------------------------------------------------------------------
291  // Function Name      :        OsaTaskNanosleep 
292  // Detail Description : This function makes the thread to sleep for nanosec
293  //                     precision.  
294  //
295  // Return Data Type   :        ErrorType
296  //
297  // Programming Note   : None.
298  //------------------------------------------------------------------------------
299  // $$$
300  */
301
302 int OsaTaskNanosleep(int iNanosec) {
303         struct timespec timeSpec_t, timeSpecRem_t;
304         int iRetval;
305
306         timeSpec_t.tv_sec = 0;
307
308         if (iNanosec > OSAL_NSEC_MAX) {
309                 iNanosec = OSAL_NSEC_MAX;
310         }
311
312         timeSpec_t.tv_nsec = iNanosec;
313
314         iRetval = nanosleep(&timeSpec_t, &timeSpecRem_t);
315
316         if (iRetval) {
317                 perror("TaskNanoSleep: TaskNanoSleep Failed ");
318                 //PrintError("Error No. : %d\n",errno);
319                 return ((int)errno);
320
321         }
322
323         return OSAL_OK;
324 }
325
326 /*
327  // $$$
328  //-----------------------------------------------------------------------------
329  // Function Name      :        OsaTaskGetId 
330  // Detail Description : This function get the current thread ID.  
331  //
332  // Return Data Type   : Current thread ID .
333  //
334  // Programming Note   :        None.
335  //------------------------------------------------------------------------------
336  // $$$
337  */
338 int OsaTaskGetId(void) {
339         return ((pthread_t)pthread_self());
340 }
341
342 /*
343  // $$$
344  //-----------------------------------------------------------------------------
345  // Function Name      :        OsaTaskDelaymsecs 
346  // Detail Description : This function makes the thread to sleep for millisec
347  //                     precision.  
348  //
349  // Return Data Type   :        ErrorType
350  //
351  // Programming Note   :        None.
352  //------------------------------------------------------------------------------
353  // $$$
354  */
355 int OsaTaskDelaymsecs(unsigned int uiMsec) {
356         unsigned int uiDiv, uiRem;
357         struct timespec timeSpec_t, timeSpecRem_t;
358         int iRetval;
359
360         uiDiv = uiMsec / 1000;
361         uiRem = uiMsec % 1000;
362
363         timeSpec_t.tv_sec = uiDiv;
364         timeSpec_t.tv_nsec = uiRem * 1000000;
365
366         iRetval = nanosleep(&timeSpec_t, &timeSpecRem_t);
367
368         if (iRetval) {
369                 perror("In OsaTaskDelaymsecs() : TaskNanoSleep Failed ");
370                 //PrintError("In OsaTaskDelaymsecs() : error no. : %d\n",errno);
371                 return ((int)errno);
372         }
373         return OSAL_OK;
374 }
375
376 /*****************************************************************************
377  ** OsaTaskDelayticks -
378  *****************************************************************************/
379 int OsaTaskDelayticks(int iTicks) {
380         struct timespec ts, tsr;
381         unsigned int hz;
382         int ret;
383
384         hz = OsaGetTicksPerSecond();
385         ts.tv_sec = iTicks / hz;
386         ts.tv_nsec = (iTicks % hz) * (1000 * 1000 * 1000 / hz);
387
388         for (;;) {
389                 ret = nanosleep(&ts, &tsr);
390                 if (ret == 0) {
391                         return OSAL_OK;
392                 } else if (errno == EINTR) {
393                         ts = tsr;
394                 } else {
395                         return OSAL_ERROR;
396                 }
397         }
398 }
399
400 /*****************************************************************************
401  ** OsaTaskSuspend-
402  *****************************************************************************/
403 int OsaTaskSuspend(unsigned int uiTid) {
404         //Not Implemented
405         return OSAL_OK;
406
407 }
408
409 /*****************************************************************************
410  ** OsaTaskResume-
411  *****************************************************************************/
412 int OsaTaskResume(unsigned int uiTid) {
413         //Not Implemented
414         return OSAL_OK;
415
416 }
417
418 /*****************************************************************************
419  ** OsaTaskRestart-
420  *****************************************************************************/
421 int OsaTaskRestart(unsigned int uiTid) {
422         //Not Implemented
423         return OSAL_OK;
424 }