Techneck/code/source-c/world/update/queue.c

77 lines
2.3 KiB
C
Raw Permalink Normal View History

2023-10-17 20:13:54 +00:00
#include <world/update/internal.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
tc_world_updater_queue_s tc_new_world_updater_queue(u32_t capacity)
{
tc_world_updater_queue_s queue;
queue.capacity = capacity;
queue.commands = calloc(sizeof(tc_world_updater_command_s), capacity);
queue.first = NULL;
queue.last = NULL;
queue.in_action = FALSE;
return queue;
}
// tc_allocate_world_updater_command: Returns a pointer to a command-slot in the given queue.
// Sub-function of tc_issue_world_updater_command
tc_world_updater_command_s * tc_allocate_world_updater_command(tc_world_updater_queue_s *queue)
{
u32_t command_index = 0;
while(command_index < queue->capacity)
{
if(!queue->commands[command_index].is_used)
return &queue->commands[command_index];
++command_index;
}
return NULL;
}
bool_t tc_issue_world_updater_command(tc_world_updater_queue_s *queue, tc_world_updater_command_s command)
{
while(queue->in_action) continue;
queue->in_action = TRUE;
tc_world_updater_command_s *queue_command = tc_allocate_world_updater_command(queue);
if(queue_command == NULL) return FALSE;
command.is_used = TRUE;
(*queue_command) = command;
queue_command->previous = queue->last;
queue_command->next = NULL;
if(queue->last != NULL) queue->last->next = queue_command;
if(queue->first == NULL) queue->first = queue_command;
queue->last = queue_command;
queue->in_action = FALSE;
return TRUE;
}
tc_world_updater_command_s tc_world_updater_query_next_command(tc_world_updater_queue_s *queue)
{
while(queue->in_action) continue;
queue->in_action = TRUE;
tc_world_updater_command_s *queue_command = queue->first;
tc_world_updater_command_s command;
memset(&command, 0x00, sizeof(tc_world_updater_command_s));
if(queue_command != NULL)
{
command = *queue_command;
queue_command->is_used = FALSE;
}
queue->in_action = FALSE;
return command;
}