#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);
codewaitqueue_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);
codeif (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);
codeWaitQueueEntry_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 implements a wait queue data structure using FreeRTOS.
The wait queue consists of wait queue entries, which contain a task handle and a pointer to the next entry in the queue.
The wait queue itself is represented by a wait queue head, which contains a pointer to the first entry (head) and last entry (tail) in the queue, as well as an event group and a mutex semaphore.
The init_waitqueue_head function initializes a wait queue head by creating an event group and a mutex semaphore.
The create_waitqueue_entry function creates a wait queue entry by allocating memory for it and setting the task handle and next pointer.
The destroy_waitqueue_entry function frees the memory allocated for a wait queue entry.
The add_wait_queue function adds a wait queue entry to the end of the wait queue. It takes a mutex semaphore to ensure thread safety.
The wake_up function wakes up the first task in the wait queue by removing it from the queue and setting a bit in the event group to signal that the task can continue. It also takes a mutex semaphore to ensure thread safety.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.