Advertisement
ebak32

StrangeMutex.c

Apr 18th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include "FreeRTOS.h"
  2. #include "task.h"
  3. #include "semphr.h"
  4.  
  5. #include "Console.h"
  6. #include "config.h"
  7.  
  8. static SemaphoreHandle_t mutex = NULL;
  9.  
  10. static void taskStrangeMutexDemo(void * params);
  11.  
  12. void initStrangeMutexDemo() {
  13.     configASSERT(mutex == NULL);    /* should be called only once */
  14.     configASSERT(mutex = xSemaphoreCreateMutex());
  15.     xTaskCreate(
  16.         taskStrangeMutexDemo, "Strange0", configMINIMAL_STACK_SIZE, NULL, BASE_TASK_PRIO, NULL);
  17.     xTaskCreate(
  18.         taskStrangeMutexDemo, "Strange1", configMINIMAL_STACK_SIZE, NULL, BASE_TASK_PRIO, NULL);
  19. }
  20.  
  21. static void guardedMethod() {
  22.     con_printf("Task %s is taking the mutex.\n", pcTaskGetTaskName(NULL));
  23.     configASSERT(
  24.         xSemaphoreTake(mutex, portMAX_DELAY));
  25.     con_printf("Task %s has taken the mutex.\n", pcTaskGetTaskName(NULL));
  26.     vTaskDelay(pdMS_TO_TICKS(1000));
  27.     configASSERT(
  28.         xSemaphoreGive(mutex));
  29.     con_printf("Task %s has released the mutex.\n", pcTaskGetTaskName(NULL));
  30. }
  31.  
  32. static void taskStrangeMutexDemo(void * params) {
  33.     while (1) {
  34.         guardedMethod();
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement