#include “FreeRTOS.h” #include “task.h” #include “eventgroups.h” typedef struct…
- User
#include “FreeRTOS.h” #include “task.h” #include “event_groups.h”
typedef struct wait_queue_entry { TaskHandle_t task; struct wait_queue_entry* next; } WaitQueueEntry_t;
typedef struct wait_queue_head { WaitQueueEntry_t* head; WaitQueueEntry_t* tail; EventGroupHandle_t eventGroup; SemaphoreHandle_t mutex; } WaitQueueHead_t;
void init_waitqueue_head(WaitQueueHead_t* waitqueue) { waitqueue->head = NULL; waitqueue->tail = NULL; waitqueue->eventGroup = xEventGroupCreate(); waitqueue->mutex = xSemaphoreCreateMutex(); }
WaitQueueEntry_t* create_waitqueue_entry(TaskHandle_t task) { WaitQueueEntry_t* entry = pvPortMalloc(sizeof(WaitQueueEntry_t)); if (entry != NULL) { entry->task = task; entry->next = NULL; } return entry; }
void destroy_waitqueue_entry(WaitQueueEntry_t* entry) { vPortFree(entry); }
void add_wait_queue(WaitQueueHead_t* waitqueue, WaitQueueEntry_t* waitqueue_entry) { xSemaphoreTake(waitqueue->mutex, portMAX_DELAY);
waitqueue_entry->next = NULL; if (waitqueue->head == NULL) { waitqueue->head = waitqueue_entry; waitqueue->tail = waitqueue_entry; } else { waitqueue->tail->next = waitqueue_entry; waitqueue->tail = waitqueue_entry; } xSemaphoreGive(waitqueue->mutex);}
void wake_up(WaitQueueHead_t* waitqueue) { xSemaphoreTake(waitqueue->mutex, portMAX_DELAY);
if (waitqueue->head != NULL) { WaitQueueEntry_t* first_entry = waitqueue->head; waitqueue->head = first_entry->next; if (waitqueue->head == NULL) { waitqueue->tail = NULL; } xEventGroupSetBits(waitqueue->eventGroup, (1 << 0)); } xSemaphoreGive(waitqueue->mutex);}
void wake_up_all(WaitQueueHead_t* waitqueue) { xSemaphoreTake(waitqueue->mutex, portMAX_DELAY);
WaitQueueEntry_t* current_entry = waitqueue->head; waitqueue->head = NULL; waitqueue->tail = NULL; while (current_entry != NULL) { WaitQueueEntry_t* next_entry = current_entry->next; xEventGroupSetBits(waitqueue->eventGroup, (1 << 0)); destroy_waitqueue_entry(current_entry); current_entry = next_entry; } xSemaphoreGive(waitqueue->mutex);}
- Assistant
This code defines a simple wait queue implementation using FreeRTOS.
The
WaitQueueEntry_tstructure represents each entry in the wait queue. It contains aTaskHandle_tmember to store the handle of the task that is waiting, and anextmember to link the entries together.The
WaitQueueHead_tstructure represents the wait queue itself. It contains pointers to the head and tail of the queue, anEventGroupHandle_tto signal the waiting tasks when they are woken up, and a to protect the queue operations. - User
improve this code
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.