message queue cleanup

This commit is contained in:
PS 2021-03-27 16:07:31 -07:00
parent c27cd4052b
commit 9dc75eff45
4 changed files with 93 additions and 78 deletions

View File

@ -78,73 +78,14 @@ DEBUG_ReceivedMotorPositions(motor_packet NewPos,
} }
} }
// KB(1) is just bigger than any packet we send. Good for now
#define DEFAULT_QUEUE_ENTRY_SIZE KB(1)
internal void internal void
MessageQueue_Init(blumen_network_msg_queue* Queue, gs_memory_arena* Arena) DEBUG_ReceivedTemperature(temp_packet Temp, gs_thread_context Ctx)
{ {
for (u32 i = 0; i < BLUMEN_MESSAGE_QUEUE_COUNT; i++) gs_string TempStr = PushStringF(Ctx.Transient, 256,
{ "\nTemperature: %d\n",
Queue->Buffers[i] = PushSizeToData(Arena, DEFAULT_QUEUE_ENTRY_SIZE); Temp.Temperature);
} NullTerminate(&TempStr);
} OutputDebugStringA(TempStr.Str);
internal bool
MessageQueue_CanWrite(blumen_network_msg_queue Queue)
{
bool Result = ((Queue.WriteHead >= Queue.ReadHead) ||
(Queue.WriteHead < Queue.ReadHead));
return Result;
}
internal bool
MessageQueue_Write(blumen_network_msg_queue* Queue, gs_data Msg)
{
Assert(Msg.Size <= DEFAULT_QUEUE_ENTRY_SIZE);
u32 Index = Queue->WriteHead;
Assert(Index >= 0 &&
Index < BLUMEN_MESSAGE_QUEUE_COUNT);
gs_data* Dest = Queue->Buffers + Index;
CopyMemoryTo(Msg.Memory, Dest->Memory, Msg.Size);
Dest->Size = Msg.Size;
// NOTE(pjs): We increment write head at the end of writing so that
// a reader thread doesn't pull the message off before we've finished
// filling it out
Queue->WriteHead = (Queue->WriteHead + 1) % BLUMEN_MESSAGE_QUEUE_COUNT;
return true;
}
internal bool
MessageQueue_CanRead(blumen_network_msg_queue Queue)
{
bool Result = (Queue.ReadHead != Queue.WriteHead);
return Result;
}
internal gs_data
MessageQueue_Read(blumen_network_msg_queue* Queue)
{
gs_data Result = {};
u32 ReadIndex = Queue->ReadHead++;
if (Queue->ReadHead >= BLUMEN_MESSAGE_QUEUE_COUNT)
{
Queue->ReadHead = 0;
}
Result = Queue->Buffers[ReadIndex];
return Result;
}
internal void
MessageQueue_Clear(blumen_network_msg_queue* Queue)
{
while (MessageQueue_CanRead(*Queue))
{
MessageQueue_Read(Queue);
}
} }
internal void internal void
@ -394,11 +335,7 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context)
BLState->BrightnessPercent = 1.f; BLState->BrightnessPercent = 1.f;
} }
gs_string TempStr = PushStringF(State->Transient, 256, DEBUG_ReceivedTemperature(Temp, Context->ThreadContext);
"\nTemperature: %d\n",
Temp.Temperature);
NullTerminate(&TempStr);
OutputDebugStringA(TempStr.Str);
}break; }break;
InvalidDefaultCase; InvalidDefaultCase;

View File

@ -5,6 +5,8 @@
// //
#ifndef BLUMEN_LUMEN_H #ifndef BLUMEN_LUMEN_H
#include "message_queue.h"
enum bl_python_packet_type enum bl_python_packet_type
{ {
PacketType_Invalid = 0, PacketType_Invalid = 0,
@ -90,14 +92,6 @@ typedef struct blumen_packet
#pragma pack(pop) #pragma pack(pop)
#define BLUMEN_MESSAGE_QUEUE_COUNT 32
typedef struct blumen_network_msg_queue
{
gs_data Buffers[BLUMEN_MESSAGE_QUEUE_COUNT];
u32 WriteHead;
u32 ReadHead;
} blumen_network_msg_queue;
// TODO(pjs): Refactor this -> blumen_network_job_state // TODO(pjs): Refactor this -> blumen_network_job_state
struct mic_listen_job_data struct mic_listen_job_data
{ {
@ -191,6 +185,7 @@ struct blumen_lumen_state
motor_packet DEBUG_PendingMotorPacket; motor_packet DEBUG_PendingMotorPacket;
}; };
#include "message_queue.cpp"

View File

@ -0,0 +1,66 @@
internal void
MessageQueue_Init(blumen_network_msg_queue* Queue, gs_memory_arena* Arena)
{
for (u32 i = 0; i < BLUMEN_MESSAGE_QUEUE_COUNT; i++)
{
Queue->Buffers[i] = PushSizeToData(Arena, DEFAULT_QUEUE_ENTRY_SIZE);
}
}
internal bool
MessageQueue_CanWrite(blumen_network_msg_queue Queue)
{
bool Result = ((Queue.WriteHead >= Queue.ReadHead) ||
(Queue.WriteHead < Queue.ReadHead));
return Result;
}
internal bool
MessageQueue_Write(blumen_network_msg_queue* Queue, gs_data Msg)
{
Assert(Msg.Size <= DEFAULT_QUEUE_ENTRY_SIZE);
u32 Index = Queue->WriteHead;
Assert(Index >= 0 &&
Index < BLUMEN_MESSAGE_QUEUE_COUNT);
gs_data* Dest = Queue->Buffers + Index;
CopyMemoryTo(Msg.Memory, Dest->Memory, Msg.Size);
Dest->Size = Msg.Size;
// NOTE(pjs): We increment write head at the end of writing so that
// a reader thread doesn't pull the message off before we've finished
// filling it out
Queue->WriteHead = (Queue->WriteHead + 1) % BLUMEN_MESSAGE_QUEUE_COUNT;
return true;
}
internal bool
MessageQueue_CanRead(blumen_network_msg_queue Queue)
{
bool Result = (Queue.ReadHead != Queue.WriteHead);
return Result;
}
internal gs_data
MessageQueue_Read(blumen_network_msg_queue* Queue)
{
gs_data Result = {};
u32 ReadIndex = Queue->ReadHead++;
if (Queue->ReadHead >= BLUMEN_MESSAGE_QUEUE_COUNT)
{
Queue->ReadHead = 0;
}
Result = Queue->Buffers[ReadIndex];
return Result;
}
internal void
MessageQueue_Clear(blumen_network_msg_queue* Queue)
{
while (MessageQueue_CanRead(*Queue))
{
MessageQueue_Read(Queue);
}
}

View File

@ -0,0 +1,17 @@
/* date = March 27th 2021 3:07 pm */
#ifndef MESSAGE_QUEUE_H
#define MESSAGE_QUEUE_H
#define BLUMEN_MESSAGE_QUEUE_COUNT 32
typedef struct blumen_network_msg_queue
{
gs_data Buffers[BLUMEN_MESSAGE_QUEUE_COUNT];
u32 WriteHead;
u32 ReadHead;
} blumen_network_msg_queue;
// KB(1) is just bigger than any packet we send. Good for now
#define DEFAULT_QUEUE_ENTRY_SIZE KB(1)
#endif //MESSAGE_QUEUE_H