Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagec
#include "FreeRTOS.h"
#include "semphr.h"

typedef struct {
  void *data_to_write;
  size_t data_to_write_size;
  char *filename_to_write;
  
  SemaphoreHandle_t request_complete_notification; //** new change
} file_write_request_s;

static QueueHandle_t file_write_request_queue;

void file_writer_task(void *p) {
  while(1) {
    file_write_request_s write_request = { NULL };
    if(xQueueReceive(file_write_request_queue, &write_request, portMAX_DELAY)) {
      write_file();
      xSemaphoreGive(write_request.request_complete_notification); //** new change
    }
  }
}

void send_write_request(file_write_request_s *request) {
  const char data_to_write[] = "hello";
    
  request->data_to_write = data_to_write;
  request->data_to_write_size = strlen(data_to_write);
  request->filename_to_write = "file.txt";
    
  xQueueSend(file_write_request_queue, request, portMAX_DELAY);
  
  // wait for request to complete such that data_to_write does not go out of scope 
  xSemaphoreTake(write_request.request_complete_notification); //** new change
}

void tasks_that_request_files_to_be_written(void *p) {
  SemaphoreHandle_t request_complete_notification = xSemaphoreCreateBinary();
  file_write_request_s write_request = {
    .request_complete_notification = request_complete_notification
  }

  while(1) {
    send_write_request(&request_complete_notification);
    xSemaphoreTake(write_request.request_complete_notification); //** new change
    vTaskDelay(5000);
  }
}