#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "recmutex.h"
Go to the source code of this file.
Defines | |
#define | recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 ) |
#define | recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 ) |
#define | recmuMAX_COUNT ( 10 ) |
#define | recmuNO_DELAY ( ( portTickType ) 0 ) |
#define | recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 ) |
#define | recmuSHORT_DELAY ( 20 / portTICK_RATE_MS ) |
#define | recmuTWO_TICK_DELAY ( ( portTickType ) 2 ) |
Functions | |
static void | prvRecursiveMutexBlockingTask (void *pvParameters) |
static void | prvRecursiveMutexControllingTask (void *pvParameters) |
static void | prvRecursiveMutexPollingTask (void *pvParameters) |
void | vStartRecursiveMutexTasks (void) |
portBASE_TYPE | xAreRecursiveMutexTasksStillRunning (void) |
Variables | |
static volatile unsigned portBASE_TYPE | uxBlockingCycles |
static volatile unsigned portBASE_TYPE | uxControllingCycles = 0 |
static volatile unsigned portBASE_TYPE | uxPollingCycles = 0 |
static volatile portBASE_TYPE | xBlockingIsSuspended = pdFALSE |
static xTaskHandle | xBlockingTaskHandle |
static volatile portBASE_TYPE | xControllingIsSuspended = pdFALSE |
static xTaskHandle | xControllingTaskHandle |
static volatile portBASE_TYPE | xErrorOccurred = pdFALSE |
static xSemaphoreHandle | xMutex |
#define recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 ) |
#define recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 ) |
#define recmuMAX_COUNT ( 10 ) |
#define recmuNO_DELAY ( ( portTickType ) 0 ) |
#define recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 ) |
#define recmuSHORT_DELAY ( 20 / portTICK_RATE_MS ) |
#define recmuTWO_TICK_DELAY ( ( portTickType ) 2 ) |
static void prvRecursiveMutexBlockingTask | ( | void * | pvParameters | ) | [static] |
Definition at line 220 of file recmutex.c.
References pdFALSE, pdPASS, pdTRUE, portMAX_DELAY, uxBlockingCycles, uxControllingCycles, vTaskSuspend(), xBlockingIsSuspended, xControllingIsSuspended, xErrorOccurred, xMutex, xSemaphoreGiveRecursive, and xSemaphoreTakeRecursive.
Referenced by vStartRecursiveMutexTasks().
00221 { 00222 /* Just to remove compiler warning. */ 00223 ( void ) pvParameters; 00224 00225 for( ;; ) 00226 { 00227 /* Attempt to obtain the mutex. We should block until the 00228 controlling task has given up the mutex, and not actually execute 00229 past this call until the controlling task is suspended. */ 00230 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS ) 00231 { 00232 if( xControllingIsSuspended != pdTRUE ) 00233 { 00234 /* Did not expect to execute until the controlling task was 00235 suspended. */ 00236 xErrorOccurred = pdTRUE; 00237 } 00238 else 00239 { 00240 /* Give the mutex back before suspending ourselves to allow 00241 the polling task to obtain the mutex. */ 00242 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS ) 00243 { 00244 xErrorOccurred = pdTRUE; 00245 } 00246 00247 xBlockingIsSuspended = pdTRUE; 00248 vTaskSuspend( NULL ); 00249 xBlockingIsSuspended = pdFALSE; 00250 } 00251 } 00252 else 00253 { 00254 /* We should not leave the xSemaphoreTakeRecursive() function 00255 until the mutex was obtained. */ 00256 xErrorOccurred = pdTRUE; 00257 } 00258 00259 /* The controlling and blocking tasks should be in lock step. */ 00260 if( uxControllingCycles != ( uxBlockingCycles + 1 ) ) 00261 { 00262 xErrorOccurred = pdTRUE; 00263 } 00264 00265 /* Keep count of the number of cycles this task has performed so a 00266 stall can be detected. */ 00267 uxBlockingCycles++; 00268 } 00269 }
static void prvRecursiveMutexControllingTask | ( | void * | pvParameters | ) | [static] |
Definition at line 153 of file recmutex.c.
References pdFALSE, pdPASS, pdTRUE, portBASE_TYPE, recmuMAX_COUNT, recmuSHORT_DELAY, recmuTWO_TICK_DELAY, uxControllingCycles, vTaskDelay(), vTaskSuspend(), xControllingIsSuspended, xErrorOccurred, xMutex, xSemaphoreGiveRecursive, and xSemaphoreTakeRecursive.
Referenced by vStartRecursiveMutexTasks().
00154 { 00155 unsigned portBASE_TYPE ux; 00156 00157 /* Just to remove compiler warning. */ 00158 ( void ) pvParameters; 00159 00160 for( ;; ) 00161 { 00162 /* Should not be able to 'give' the mutex, as we have not yet 'taken' 00163 it. */ 00164 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS ) 00165 { 00166 xErrorOccurred = pdTRUE; 00167 } 00168 00169 for( ux = 0; ux < recmuMAX_COUNT; ux++ ) 00170 { 00171 /* We should now be able to take the mutex as many times as 00172 we like. A one tick delay is used so the polling task will 00173 inherit our priority on all but the first cycle of this task. 00174 If we did not block attempting to receive the mutex then no 00175 priority inheritance would occur. */ 00176 if( xSemaphoreTakeRecursive( xMutex, recmuTWO_TICK_DELAY ) != pdPASS ) 00177 { 00178 xErrorOccurred = pdTRUE; 00179 } 00180 00181 /* Ensure the other task attempting to access the mutex (and the 00182 other demo tasks) are able to execute. */ 00183 vTaskDelay( recmuSHORT_DELAY ); 00184 } 00185 00186 /* For each time we took the mutex, give it back. */ 00187 for( ux = 0; ux < recmuMAX_COUNT; ux++ ) 00188 { 00189 /* Ensure the other task attempting to access the mutex (and the 00190 other demo tasks) are able to execute. */ 00191 vTaskDelay( recmuSHORT_DELAY ); 00192 00193 /* We should now be able to give the mutex as many times as we 00194 took it. */ 00195 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS ) 00196 { 00197 xErrorOccurred = pdTRUE; 00198 } 00199 } 00200 00201 /* Having given it back the same number of times as it was taken, we 00202 should no longer be the mutex owner, so the next give sh ould fail. */ 00203 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS ) 00204 { 00205 xErrorOccurred = pdTRUE; 00206 } 00207 00208 /* Keep count of the number of cycles this task has performed so a 00209 stall can be detected. */ 00210 uxControllingCycles++; 00211 00212 /* Suspend ourselves to the blocking task can execute. */ 00213 xControllingIsSuspended = pdTRUE; 00214 vTaskSuspend( NULL ); 00215 xControllingIsSuspended = pdFALSE; 00216 } 00217 }
static void prvRecursiveMutexPollingTask | ( | void * | pvParameters | ) | [static] |
Definition at line 272 of file recmutex.c.
References pdPASS, pdTRUE, recmuNO_DELAY, taskYIELD, uxPollingCycles, vTaskResume(), xBlockingIsSuspended, xBlockingTaskHandle, xControllingTaskHandle, xErrorOccurred, xMutex, xSemaphoreGiveRecursive, and xSemaphoreTakeRecursive.
Referenced by vStartRecursiveMutexTasks().
00273 { 00274 /* Just to remove compiler warning. */ 00275 ( void ) pvParameters; 00276 00277 for( ;; ) 00278 { 00279 /* Keep attempting to obtain the mutex. We should only obtain it when 00280 the blocking task has suspended itself. */ 00281 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS ) 00282 { 00283 /* Is the blocking task suspended? */ 00284 if( xBlockingIsSuspended != pdTRUE ) 00285 { 00286 xErrorOccurred = pdTRUE; 00287 } 00288 else 00289 { 00290 /* Keep count of the number of cycles this task has performed so 00291 a stall can be detected. */ 00292 uxPollingCycles++; 00293 00294 /* We can resume the other tasks here even though they have a 00295 higher priority than the polling task. When they execute they 00296 will attempt to obtain the mutex but fail because the polling 00297 task is still the mutex holder. The polling task (this task) 00298 will then inherit the higher priority. */ 00299 vTaskResume( xBlockingTaskHandle ); 00300 vTaskResume( xControllingTaskHandle ); 00301 00302 /* Release the mutex, disinheriting the higher priority again. */ 00303 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS ) 00304 { 00305 xErrorOccurred = pdTRUE; 00306 } 00307 } 00308 } 00309 00310 #if configUSE_PREEMPTION == 0 00311 { 00312 taskYIELD(); 00313 } 00314 #endif 00315 } 00316 }
void vStartRecursiveMutexTasks | ( | void | ) |
Definition at line 129 of file recmutex.c.
References configMINIMAL_STACK_SIZE, portCHAR, prvRecursiveMutexBlockingTask(), prvRecursiveMutexControllingTask(), prvRecursiveMutexPollingTask(), recmuBLOCKING_TASK_PRIORITY, recmuCONTROLLING_TASK_PRIORITY, recmuPOLLING_TASK_PRIORITY, vQueueAddToRegistry, xBlockingTaskHandle, xControllingTaskHandle, xMutex, xSemaphoreCreateRecursiveMutex, and xTaskCreate.
00130 { 00131 /* Just creates the mutex and the three tasks. */ 00132 00133 xMutex = xSemaphoreCreateRecursiveMutex(); 00134 00135 /* vQueueAddToRegistry() adds the mutex to the registry, if one is 00136 in use. The registry is provided as a means for kernel aware 00137 debuggers to locate mutex and has no purpose if a kernel aware debugger 00138 is not being used. The call to vQueueAddToRegistry() will be removed 00139 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is 00140 defined to be less than 1. */ 00141 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Recursive_Mutex" ); 00142 00143 00144 if( xMutex != NULL ) 00145 { 00146 xTaskCreate( prvRecursiveMutexControllingTask, ( signed portCHAR * ) "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle ); 00147 xTaskCreate( prvRecursiveMutexBlockingTask, ( signed portCHAR * ) "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle ); 00148 xTaskCreate( prvRecursiveMutexPollingTask, ( signed portCHAR * ) "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL ); 00149 } 00150 }
portBASE_TYPE xAreRecursiveMutexTasksStillRunning | ( | void | ) |
Definition at line 320 of file recmutex.c.
References pdFAIL, pdTRUE, portBASE_TYPE, uxBlockingCycles, uxControllingCycles, uxPollingCycles, and xErrorOccurred.
00321 { 00322 portBASE_TYPE xReturn; 00323 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0; 00324 00325 /* Is the controlling task still cycling? */ 00326 if( uxLastControllingCycles == uxControllingCycles ) 00327 { 00328 xErrorOccurred = pdTRUE; 00329 } 00330 else 00331 { 00332 uxLastControllingCycles = uxControllingCycles; 00333 } 00334 00335 /* Is the blocking task still cycling? */ 00336 if( uxLastBlockingCycles == uxBlockingCycles ) 00337 { 00338 xErrorOccurred = pdTRUE; 00339 } 00340 else 00341 { 00342 uxLastBlockingCycles = uxBlockingCycles; 00343 } 00344 00345 /* Is the polling task still cycling? */ 00346 if( uxLastPollingCycles == uxPollingCycles ) 00347 { 00348 xErrorOccurred = pdTRUE; 00349 } 00350 else 00351 { 00352 uxLastPollingCycles = uxPollingCycles; 00353 } 00354 00355 if( xErrorOccurred == pdTRUE ) 00356 { 00357 xReturn = pdFAIL; 00358 } 00359 else 00360 { 00361 xReturn = pdTRUE; 00362 } 00363 00364 return xReturn; 00365 }
volatile unsigned portBASE_TYPE uxBlockingCycles [static] |
Definition at line 121 of file recmutex.c.
Referenced by prvRecursiveMutexBlockingTask(), and xAreRecursiveMutexTasksStillRunning().
volatile unsigned portBASE_TYPE uxControllingCycles = 0 [static] |
Definition at line 121 of file recmutex.c.
Referenced by prvRecursiveMutexBlockingTask(), prvRecursiveMutexControllingTask(), and xAreRecursiveMutexTasksStillRunning().
volatile unsigned portBASE_TYPE uxPollingCycles = 0 [static] |
Definition at line 121 of file recmutex.c.
Referenced by prvRecursiveMutexPollingTask(), and xAreRecursiveMutexTasksStillRunning().
volatile portBASE_TYPE xBlockingIsSuspended = pdFALSE [static] |
Definition at line 120 of file recmutex.c.
Referenced by prvRecursiveMutexBlockingTask(), and prvRecursiveMutexPollingTask().
xTaskHandle xBlockingTaskHandle [static] |
Definition at line 125 of file recmutex.c.
Referenced by prvRecursiveMutexPollingTask(), and vStartRecursiveMutexTasks().
volatile portBASE_TYPE xControllingIsSuspended = pdFALSE [static] |
Definition at line 120 of file recmutex.c.
Referenced by prvRecursiveMutexBlockingTask(), and prvRecursiveMutexControllingTask().
xTaskHandle xControllingTaskHandle [static] |
Definition at line 125 of file recmutex.c.
Referenced by prvRecursiveMutexPollingTask(), and vStartRecursiveMutexTasks().
volatile portBASE_TYPE xErrorOccurred = pdFALSE [static] |
Definition at line 120 of file recmutex.c.
xSemaphoreHandle xMutex [static] |
Definition at line 117 of file recmutex.c.
Referenced by prvHighPriorityMutexTask(), prvLowPriorityMutexTask(), prvRecursiveMutexBlockingTask(), prvRecursiveMutexControllingTask(), prvRecursiveMutexPollingTask(), vStartAltGenericQueueTasks(), vStartGenericQueueTasks(), and vStartRecursiveMutexTasks().