1 /* 2 * Copyright (c) 2010, Texas Instruments Incorporated 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of Texas Instruments Incorporated nor the names of 17 * its contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * @file timm_osal_task.c 35 * This file contains methods that provides the functionality 36 * for creating/destroying tasks. 37 * 38 * @path \ 39 * 40 */ 41 /* -------------------------------------------------------------------------- */ 42 /* ========================================================================= 43 *! 44 *! Revision History 45 *! =================================== 46 *! 21-Oct-2008 Maiya ShreeHarsha: Linux specific changes 47 *! 0.1: Created the first draft version, ksrini (at) ti.com 48 * ========================================================================= */ 49 50 /****************************************************************************** 51 * Includes 52 ******************************************************************************/ 53 54 #include <stdio.h> 55 #include <pthread.h> /*for POSIX calls */ 56 #include <sched.h> /*for sched structure */ 57 #include <unistd.h> 58 59 60 61 #include "timm_osal_types.h" 62 #include "timm_osal_trace.h" 63 #include "timm_osal_error.h" 64 #include "timm_osal_memory.h" 65 #include "timm_osal_task.h" 66 67 68 69 70 /** 71 * TIMM_OSAL_TASK describe the different task information 72 */ 73 typedef struct TIMM_OSAL_TASK 74 { 75 pthread_t threadID; /*SHM check */ 76 /* To set the priority and stack size */ 77 pthread_attr_t ThreadAttr; /*For setting the priority and stack size */ 78 /** Name of the task */ 79 /* TIMM_OSAL_S8 name[8];*//* eight character plus null char */ 80 /** Pointer to the task stack memory */ 81 /* TIMM_OSAL_PTR stackPtr;*/ 82 /** Size of the task stack */ 83 /* TIMM_OSAL_S32 stackSize;*/ 84 /*parameters to the task */ 85 TIMM_OSAL_U32 uArgc; 86 TIMM_OSAL_PTR pArgv; 87 /** task priority */ 88 /* TIMM_OSAL_S32 priority;*/ 89 /** flag to check if task got created */ 90 TIMM_OSAL_BOOL isCreated; 91 } TIMM_OSAL_TASK; 92 93 94 /****************************************************************************** 95 * Function Prototypes 96 ******************************************************************************/ 97 98 99 /* ========================================================================== */ 100 /** 101 * @fn TIMM_OSAL_CreateTask function 102 * 103 * @see 104 */ 105 /* ========================================================================== */ 106 107 TIMM_OSAL_ERRORTYPE TIMM_OSAL_CreateTask(TIMM_OSAL_PTR * pTask, 108 TIMM_OSAL_TaskProc pFunc, 109 TIMM_OSAL_U32 uArgc, 110 TIMM_OSAL_PTR pArgv, 111 TIMM_OSAL_U32 uStackSize, TIMM_OSAL_U32 uPriority, TIMM_OSAL_S8 * pName) 112 { 113 114 TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN; 115 TIMM_OSAL_TASK *pHandle = TIMM_OSAL_NULL; 116 struct sched_param sched; 117 size_t stackSize; 118 *pTask = TIMM_OSAL_NULL; 119 120 121 /*Task structure allocation */ 122 pHandle = 123 (TIMM_OSAL_TASK *) TIMM_OSAL_Malloc(sizeof(TIMM_OSAL_TASK), 0, 0, 124 0); 125 if (pHandle == TIMM_OSAL_NULL) 126 { 127 bReturnStatus = TIMM_OSAL_ERR_ALLOC; 128 goto EXIT; 129 } 130 131 /* Initial cleaning of the task structure */ 132 TIMM_OSAL_Memset((TIMM_OSAL_PTR) pHandle, 0, sizeof(TIMM_OSAL_TASK)); 133 134 /*Arguments for task */ 135 pHandle->uArgc = uArgc; 136 pHandle->pArgv = pArgv; 137 138 pHandle->isCreated = TIMM_OSAL_FALSE; 139 140 141 if (SUCCESS != pthread_attr_init(&pHandle->ThreadAttr)) 142 { 143 /*TIMM_OSAL_Error("Task Init Attr Init failed!"); */ 144 goto EXIT; 145 } 146 /* Updation of the priority and the stack size */ 147 148 if (SUCCESS != pthread_attr_getschedparam(&pHandle->ThreadAttr, 149 &sched)) 150 { 151 /*TIMM_OSAL_Error("Task Init Get Sched Params failed!"); */ 152 goto EXIT; 153 } 154 155 sched.sched_priority = uPriority; /* relative to the default priority */ 156 if (SUCCESS != pthread_attr_setschedparam(&pHandle->ThreadAttr, 157 &sched)) 158 { 159 /*TIMM_OSAL_Error("Task Init Set Sched Paramsfailed!"); */ 160 goto EXIT; 161 } 162 163 /*First get the default stack size */ 164 if (SUCCESS != pthread_attr_getstacksize(&pHandle->ThreadAttr, 165 &stackSize)) 166 { 167 /*TIMM_OSAL_Error("Task Init Set Stack Size failed!"); */ 168 goto EXIT; 169 } 170 171 /*Check if requested stack size is larger than the current default stack size */ 172 if (uStackSize > stackSize) 173 { 174 stackSize = uStackSize; 175 if (SUCCESS != pthread_attr_setstacksize(&pHandle->ThreadAttr, 176 stackSize)) 177 { 178 /*TIMM_OSAL_Error("Task Init Set Stack Size failed!"); */ 179 goto EXIT; 180 } 181 } 182 183 184 185 if (SUCCESS != pthread_create(&pHandle->threadID, 186 &pHandle->ThreadAttr, pFunc, pArgv)) 187 { 188 /*TIMM_OSAL_Error ("Create_Task failed !"); */ 189 goto EXIT; 190 } 191 192 193 /* Task was successfully created */ 194 pHandle->isCreated = TIMM_OSAL_TRUE; 195 *pTask = (TIMM_OSAL_PTR) pHandle; 196 bReturnStatus = TIMM_OSAL_ERR_NONE; 197 /**pTask = (TIMM_OSAL_PTR *)pHandle;*/ 198 199 EXIT: 200 /* if((TIMM_OSAL_ERR_NONE != bReturnStatus) && (TIMM_OSAL_NULL != pHandle)) { 201 TIMM_OSAL_Free (pHandle->stackPtr);*/ 202 if ((TIMM_OSAL_ERR_NONE != bReturnStatus)) 203 { 204 TIMM_OSAL_Free(pHandle); 205 } 206 return bReturnStatus; 207 208 } 209 210 /* ========================================================================== */ 211 /** 212 * @fn TIMM_OSAL_DeleteTask 213 * 214 * @see 215 */ 216 /* ========================================================================== */ 217 218 TIMM_OSAL_ERRORTYPE TIMM_OSAL_DeleteTask(TIMM_OSAL_PTR pTask) 219 { 220 TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN; 221 222 TIMM_OSAL_TASK *pHandle = (TIMM_OSAL_TASK *) pTask; 223 void *retVal; 224 225 if ((NULL == pHandle) || (TIMM_OSAL_TRUE != pHandle->isCreated)) 226 { 227 /* this task was never created */ 228 bReturnStatus = TIMM_OSAL_ERR_PARAMETER; 229 goto EXIT; 230 } 231 if (pthread_attr_destroy(&pHandle->ThreadAttr)) 232 { 233 /*TIMM_OSAL_Error("Delete_Task failed !"); */ 234 goto EXIT; 235 } 236 if (pthread_join(pHandle->threadID, &retVal)) 237 { 238 /*TIMM_OSAL_Error("Delete_Task failed !"); */ 239 goto EXIT; 240 /* bReturnStatus = TIMM_OSAL_ERR_CREATE(TIMM_OSAL_ERR, TIMM_OSAL_COMP_TASK, status);*//*shm to be done */ 241 } 242 bReturnStatus = TIMM_OSAL_ERR_NONE; 243 TIMM_OSAL_Free(pHandle); 244 EXIT: 245 return bReturnStatus; 246 } 247 248 249 TIMM_OSAL_ERRORTYPE TIMM_OSAL_SleepTask(TIMM_OSAL_U32 mSec) 250 { 251 TIMM_OSAL_S32 nReturn = 0; 252 253 #ifdef _POSIX_VERSION_1_ 254 usleep(1000 * mSec); 255 #else 256 nReturn = usleep(1000 * mSec); 257 #endif 258 if (nReturn == 0) 259 return TIMM_OSAL_ERR_NONE; 260 else 261 return TIMM_OSAL_ERR_UNKNOWN; 262 } 263