brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame^] | 1 | /* |
| 2 | FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd. |
| 3 | |
| 4 | *************************************************************************** |
| 5 | * * |
| 6 | * If you are: * |
| 7 | * * |
| 8 | * + New to FreeRTOS, * |
| 9 | * + Wanting to learn FreeRTOS or multitasking in general quickly * |
| 10 | * + Looking for basic training, * |
| 11 | * + Wanting to improve your FreeRTOS skills and productivity * |
| 12 | * * |
| 13 | * then take a look at the FreeRTOS eBook * |
| 14 | * * |
| 15 | * "Using the FreeRTOS Real Time Kernel - a Practical Guide" * |
| 16 | * http://www.FreeRTOS.org/Documentation * |
| 17 | * * |
| 18 | * A pdf reference manual is also available. Both are usually delivered * |
| 19 | * to your inbox within 20 minutes to two hours when purchased between 8am * |
| 20 | * and 8pm GMT (although please allow up to 24 hours in case of * |
| 21 | * exceptional circumstances). Thank you for your support! * |
| 22 | * * |
| 23 | *************************************************************************** |
| 24 | |
| 25 | This file is part of the FreeRTOS distribution. |
| 26 | |
| 27 | FreeRTOS is free software; you can redistribute it and/or modify it under |
| 28 | the terms of the GNU General Public License (version 2) as published by the |
| 29 | Free Software Foundation AND MODIFIED BY the FreeRTOS exception. |
| 30 | ***NOTE*** The exception to the GPL is included to allow you to distribute |
| 31 | a combined work that includes FreeRTOS without being obliged to provide the |
| 32 | source code for proprietary components outside of the FreeRTOS kernel. |
| 33 | FreeRTOS is distributed in the hope that it will be useful, but WITHOUT |
| 34 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 35 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 36 | more details. You should have received a copy of the GNU General Public |
| 37 | License and the FreeRTOS license exception along with FreeRTOS; if not it |
| 38 | can be viewed here: http://www.freertos.org/a00114.html and also obtained |
| 39 | by writing to Richard Barry, contact details for whom are available on the |
| 40 | FreeRTOS WEB site. |
| 41 | |
| 42 | 1 tab == 4 spaces! |
| 43 | |
| 44 | http://www.FreeRTOS.org - Documentation, latest information, license and |
| 45 | contact details. |
| 46 | |
| 47 | http://www.SafeRTOS.com - A version that is certified for use in safety |
| 48 | critical systems. |
| 49 | |
| 50 | http://www.OpenRTOS.com - Commercial support, development, porting, |
| 51 | licensing and training services. |
| 52 | */ |
| 53 | |
| 54 | /* |
| 55 | The tasks defined on this page demonstrate the use of recursive mutexes. |
| 56 | |
| 57 | For recursive mutex functionality the created mutex should be created using |
| 58 | xSemaphoreCreateRecursiveMutex(), then be manipulated |
| 59 | using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API |
| 60 | functions. |
| 61 | |
| 62 | This demo creates three tasks all of which access the same recursive mutex: |
| 63 | |
| 64 | prvRecursiveMutexControllingTask() has the highest priority so executes |
| 65 | first and grabs the mutex. It then performs some recursive accesses - |
| 66 | between each of which it sleeps for a short period to let the lower |
| 67 | priority tasks execute. When it has completed its demo functionality |
| 68 | it gives the mutex back before suspending itself. |
| 69 | |
| 70 | prvRecursiveMutexBlockingTask() attempts to access the mutex by performing |
| 71 | a blocking 'take'. The blocking task has a lower priority than the |
| 72 | controlling task so by the time it executes the mutex has already been |
| 73 | taken by the controlling task, causing the blocking task to block. It |
| 74 | does not unblock until the controlling task has given the mutex back, |
| 75 | and it does not actually run until the controlling task has suspended |
| 76 | itself (due to the relative priorities). When it eventually does obtain |
| 77 | the mutex all it does is give the mutex back prior to also suspending |
| 78 | itself. At this point both the controlling task and the blocking task are |
| 79 | suspended. |
| 80 | |
| 81 | prvRecursiveMutexPollingTask() runs at the idle priority. It spins round |
| 82 | a tight loop attempting to obtain the mutex with a non-blocking call. As |
| 83 | the lowest priority task it will not successfully obtain the mutex until |
| 84 | both the controlling and blocking tasks are suspended. Once it eventually |
| 85 | does obtain the mutex it first unsuspends both the controlling task and |
| 86 | blocking task prior to giving the mutex back - resulting in the polling |
| 87 | task temporarily inheriting the controlling tasks priority. |
| 88 | */ |
| 89 | |
| 90 | /* Scheduler include files. */ |
| 91 | #include "FreeRTOS.h" |
| 92 | #include "task.h" |
| 93 | #include "semphr.h" |
| 94 | |
| 95 | /* Demo app include files. */ |
| 96 | #include "recmutex.h" |
| 97 | |
| 98 | /* Priorities assigned to the three tasks. */ |
| 99 | #define recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 ) |
| 100 | #define recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 ) |
| 101 | #define recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 ) |
| 102 | |
| 103 | /* The recursive call depth. */ |
| 104 | #define recmuMAX_COUNT ( 10 ) |
| 105 | |
| 106 | /* Misc. */ |
| 107 | #define recmuSHORT_DELAY ( 20 / portTICK_RATE_MS ) |
| 108 | #define recmuNO_DELAY ( ( portTickType ) 0 ) |
| 109 | #define recmuTWO_TICK_DELAY ( ( portTickType ) 2 ) |
| 110 | |
| 111 | /* The three tasks as described at the top of this file. */ |
| 112 | static void prvRecursiveMutexControllingTask(void *pvParameters); |
| 113 | static void prvRecursiveMutexBlockingTask(void *pvParameters); |
| 114 | static void prvRecursiveMutexPollingTask(void *pvParameters); |
| 115 | |
| 116 | /* The mutex used by the demo. */ |
| 117 | static xSemaphoreHandle xMutex; |
| 118 | |
| 119 | /* Variables used to detect and latch errors. */ |
| 120 | static volatile portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE; |
| 121 | static volatile unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles, uxPollingCycles = 0; |
| 122 | |
| 123 | /* Handles of the two higher priority tasks, required so they can be resumed |
| 124 | (unsuspended). */ |
| 125 | static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle; |
| 126 | |
| 127 | /*-----------------------------------------------------------*/ |
| 128 | |
| 129 | void vStartRecursiveMutexTasks(void) |
| 130 | { |
| 131 | /* Just creates the mutex and the three tasks. */ |
| 132 | |
| 133 | xMutex = xSemaphoreCreateRecursiveMutex(); |
| 134 | |
| 135 | /* vQueueAddToRegistry() adds the mutex to the registry, if one is |
| 136 | in use. The registry is provided as a means for kernel aware |
| 137 | debuggers to locate mutex and has no purpose if a kernel aware debugger |
| 138 | is not being used. The call to vQueueAddToRegistry() will be removed |
| 139 | by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is |
| 140 | defined to be less than 1. */ |
| 141 | vQueueAddToRegistry((xQueueHandle) xMutex, (signed portCHAR *) "Recursive_Mutex"); |
| 142 | |
| 143 | |
| 144 | if (xMutex != NULL) { |
| 145 | xTaskCreate(prvRecursiveMutexControllingTask, (signed portCHAR *) "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle); |
| 146 | xTaskCreate(prvRecursiveMutexBlockingTask, (signed portCHAR *) "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle); |
| 147 | xTaskCreate(prvRecursiveMutexPollingTask, (signed portCHAR *) "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL); |
| 148 | } |
| 149 | } |
| 150 | /*-----------------------------------------------------------*/ |
| 151 | |
| 152 | static void prvRecursiveMutexControllingTask(void *pvParameters) |
| 153 | { |
| 154 | unsigned portBASE_TYPE ux; |
| 155 | |
| 156 | /* Just to remove compiler warning. */ |
| 157 | (void) pvParameters; |
| 158 | |
| 159 | for (;;) { |
| 160 | /* Should not be able to 'give' the mutex, as we have not yet 'taken' |
| 161 | it. */ |
| 162 | if (xSemaphoreGiveRecursive(xMutex) == pdPASS) { |
| 163 | xErrorOccurred = pdTRUE; |
| 164 | } |
| 165 | |
| 166 | for (ux = 0; ux < recmuMAX_COUNT; ux++) { |
| 167 | /* We should now be able to take the mutex as many times as |
| 168 | we like. A one tick delay is used so the polling task will |
| 169 | inherit our priority on all but the first cycle of this task. |
| 170 | If we did not block attempting to receive the mutex then no |
| 171 | priority inheritance would occur. */ |
| 172 | if (xSemaphoreTakeRecursive(xMutex, recmuTWO_TICK_DELAY) != pdPASS) { |
| 173 | xErrorOccurred = pdTRUE; |
| 174 | } |
| 175 | |
| 176 | /* Ensure the other task attempting to access the mutex (and the |
| 177 | other demo tasks) are able to execute. */ |
| 178 | vTaskDelay(recmuSHORT_DELAY); |
| 179 | } |
| 180 | |
| 181 | /* For each time we took the mutex, give it back. */ |
| 182 | for (ux = 0; ux < recmuMAX_COUNT; ux++) { |
| 183 | /* Ensure the other task attempting to access the mutex (and the |
| 184 | other demo tasks) are able to execute. */ |
| 185 | vTaskDelay(recmuSHORT_DELAY); |
| 186 | |
| 187 | /* We should now be able to give the mutex as many times as we |
| 188 | took it. */ |
| 189 | if (xSemaphoreGiveRecursive(xMutex) != pdPASS) { |
| 190 | xErrorOccurred = pdTRUE; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /* Having given it back the same number of times as it was taken, we |
| 195 | should no longer be the mutex owner, so the next give sh ould fail. */ |
| 196 | if (xSemaphoreGiveRecursive(xMutex) == pdPASS) { |
| 197 | xErrorOccurred = pdTRUE; |
| 198 | } |
| 199 | |
| 200 | /* Keep count of the number of cycles this task has performed so a |
| 201 | stall can be detected. */ |
| 202 | uxControllingCycles++; |
| 203 | |
| 204 | /* Suspend ourselves to the blocking task can execute. */ |
| 205 | xControllingIsSuspended = pdTRUE; |
| 206 | vTaskSuspend(NULL); |
| 207 | xControllingIsSuspended = pdFALSE; |
| 208 | } |
| 209 | } |
| 210 | /*-----------------------------------------------------------*/ |
| 211 | |
| 212 | static void prvRecursiveMutexBlockingTask(void *pvParameters) |
| 213 | { |
| 214 | /* Just to remove compiler warning. */ |
| 215 | (void) pvParameters; |
| 216 | |
| 217 | for (;;) { |
| 218 | /* Attempt to obtain the mutex. We should block until the |
| 219 | controlling task has given up the mutex, and not actually execute |
| 220 | past this call until the controlling task is suspended. */ |
| 221 | if (xSemaphoreTakeRecursive(xMutex, portMAX_DELAY) == pdPASS) { |
| 222 | if (xControllingIsSuspended != pdTRUE) { |
| 223 | /* Did not expect to execute until the controlling task was |
| 224 | suspended. */ |
| 225 | xErrorOccurred = pdTRUE; |
| 226 | } else { |
| 227 | /* Give the mutex back before suspending ourselves to allow |
| 228 | the polling task to obtain the mutex. */ |
| 229 | if (xSemaphoreGiveRecursive(xMutex) != pdPASS) { |
| 230 | xErrorOccurred = pdTRUE; |
| 231 | } |
| 232 | |
| 233 | xBlockingIsSuspended = pdTRUE; |
| 234 | vTaskSuspend(NULL); |
| 235 | xBlockingIsSuspended = pdFALSE; |
| 236 | } |
| 237 | } else { |
| 238 | /* We should not leave the xSemaphoreTakeRecursive() function |
| 239 | until the mutex was obtained. */ |
| 240 | xErrorOccurred = pdTRUE; |
| 241 | } |
| 242 | |
| 243 | /* The controlling and blocking tasks should be in lock step. */ |
| 244 | if (uxControllingCycles != (uxBlockingCycles + 1)) { |
| 245 | xErrorOccurred = pdTRUE; |
| 246 | } |
| 247 | |
| 248 | /* Keep count of the number of cycles this task has performed so a |
| 249 | stall can be detected. */ |
| 250 | uxBlockingCycles++; |
| 251 | } |
| 252 | } |
| 253 | /*-----------------------------------------------------------*/ |
| 254 | |
| 255 | static void prvRecursiveMutexPollingTask(void *pvParameters) |
| 256 | { |
| 257 | /* Just to remove compiler warning. */ |
| 258 | (void) pvParameters; |
| 259 | |
| 260 | for (;;) { |
| 261 | /* Keep attempting to obtain the mutex. We should only obtain it when |
| 262 | the blocking task has suspended itself. */ |
| 263 | if (xSemaphoreTakeRecursive(xMutex, recmuNO_DELAY) == pdPASS) { |
| 264 | /* Is the blocking task suspended? */ |
| 265 | if (xBlockingIsSuspended != pdTRUE) { |
| 266 | xErrorOccurred = pdTRUE; |
| 267 | } else { |
| 268 | /* Keep count of the number of cycles this task has performed so |
| 269 | a stall can be detected. */ |
| 270 | uxPollingCycles++; |
| 271 | |
| 272 | /* We can resume the other tasks here even though they have a |
| 273 | higher priority than the polling task. When they execute they |
| 274 | will attempt to obtain the mutex but fail because the polling |
| 275 | task is still the mutex holder. The polling task (this task) |
| 276 | will then inherit the higher priority. */ |
| 277 | vTaskResume(xBlockingTaskHandle); |
| 278 | vTaskResume(xControllingTaskHandle); |
| 279 | |
| 280 | /* Release the mutex, disinheriting the higher priority again. */ |
| 281 | if (xSemaphoreGiveRecursive(xMutex) != pdPASS) { |
| 282 | xErrorOccurred = pdTRUE; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | #if configUSE_PREEMPTION == 0 |
| 288 | { |
| 289 | taskYIELD(); |
| 290 | } |
| 291 | #endif |
| 292 | } |
| 293 | } |
| 294 | /*-----------------------------------------------------------*/ |
| 295 | |
| 296 | /* This is called to check that all the created tasks are still running. */ |
| 297 | portBASE_TYPE xAreRecursiveMutexTasksStillRunning(void) |
| 298 | { |
| 299 | portBASE_TYPE xReturn; |
| 300 | static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0; |
| 301 | |
| 302 | /* Is the controlling task still cycling? */ |
| 303 | if (uxLastControllingCycles == uxControllingCycles) { |
| 304 | xErrorOccurred = pdTRUE; |
| 305 | } else { |
| 306 | uxLastControllingCycles = uxControllingCycles; |
| 307 | } |
| 308 | |
| 309 | /* Is the blocking task still cycling? */ |
| 310 | if (uxLastBlockingCycles == uxBlockingCycles) { |
| 311 | xErrorOccurred = pdTRUE; |
| 312 | } else { |
| 313 | uxLastBlockingCycles = uxBlockingCycles; |
| 314 | } |
| 315 | |
| 316 | /* Is the polling task still cycling? */ |
| 317 | if (uxLastPollingCycles == uxPollingCycles) { |
| 318 | xErrorOccurred = pdTRUE; |
| 319 | } else { |
| 320 | uxLastPollingCycles = uxPollingCycles; |
| 321 | } |
| 322 | |
| 323 | if (xErrorOccurred == pdTRUE) { |
| 324 | xReturn = pdFAIL; |
| 325 | } else { |
| 326 | xReturn = pdTRUE; |
| 327 | } |
| 328 | |
| 329 | return xReturn; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | |
| 334 | |