From 9dc75eff451994c6f1d4cb2c04cbdf7b3792d072 Mon Sep 17 00:00:00 2001 From: PS Date: Sat, 27 Mar 2021 16:07:31 -0700 Subject: [PATCH] message queue cleanup --- src/app/ss_blumen_lumen/blumen_lumen.cpp | 77 +++-------------------- src/app/ss_blumen_lumen/blumen_lumen.h | 11 +--- src/app/ss_blumen_lumen/message_queue.cpp | 66 +++++++++++++++++++ src/app/ss_blumen_lumen/message_queue.h | 17 +++++ 4 files changed, 93 insertions(+), 78 deletions(-) create mode 100644 src/app/ss_blumen_lumen/message_queue.cpp create mode 100644 src/app/ss_blumen_lumen/message_queue.h diff --git a/src/app/ss_blumen_lumen/blumen_lumen.cpp b/src/app/ss_blumen_lumen/blumen_lumen.cpp index 563adbe..803d86d 100644 --- a/src/app/ss_blumen_lumen/blumen_lumen.cpp +++ b/src/app/ss_blumen_lumen/blumen_lumen.cpp @@ -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 -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++) - { - 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); - } + gs_string TempStr = PushStringF(Ctx.Transient, 256, + "\nTemperature: %d\n", + Temp.Temperature); + NullTerminate(&TempStr); + OutputDebugStringA(TempStr.Str); } internal void @@ -394,11 +335,7 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context) BLState->BrightnessPercent = 1.f; } - gs_string TempStr = PushStringF(State->Transient, 256, - "\nTemperature: %d\n", - Temp.Temperature); - NullTerminate(&TempStr); - OutputDebugStringA(TempStr.Str); + DEBUG_ReceivedTemperature(Temp, Context->ThreadContext); }break; InvalidDefaultCase; diff --git a/src/app/ss_blumen_lumen/blumen_lumen.h b/src/app/ss_blumen_lumen/blumen_lumen.h index b23378d..57666dd 100644 --- a/src/app/ss_blumen_lumen/blumen_lumen.h +++ b/src/app/ss_blumen_lumen/blumen_lumen.h @@ -5,6 +5,8 @@ // #ifndef BLUMEN_LUMEN_H +#include "message_queue.h" + enum bl_python_packet_type { PacketType_Invalid = 0, @@ -90,14 +92,6 @@ typedef struct blumen_packet #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 struct mic_listen_job_data { @@ -191,6 +185,7 @@ struct blumen_lumen_state motor_packet DEBUG_PendingMotorPacket; }; +#include "message_queue.cpp" diff --git a/src/app/ss_blumen_lumen/message_queue.cpp b/src/app/ss_blumen_lumen/message_queue.cpp new file mode 100644 index 0000000..241b864 --- /dev/null +++ b/src/app/ss_blumen_lumen/message_queue.cpp @@ -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); + } +} diff --git a/src/app/ss_blumen_lumen/message_queue.h b/src/app/ss_blumen_lumen/message_queue.h new file mode 100644 index 0000000..a450b54 --- /dev/null +++ b/src/app/ss_blumen_lumen/message_queue.h @@ -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