FastLED 3.9.15
Loading...
Searching...
No Matches
async_log_queue.h
Go to the documentation of this file.
1#pragma once
2
5
6#include "fl/stl/int.h"
7#include "fl/stl/noexcept.h"
9
10namespace fl {
11
12class string;
13
17template <fl::size DescriptorCount = 128, fl::size ArenaSize = 4096>
19 // Compile-time assertions for power-of-2 sizes (enables cheap modulo with &)
20 FL_STATIC_ASSERT((DescriptorCount & (DescriptorCount - 1)) == 0,
21 "DescriptorCount must be power of 2");
22 FL_STATIC_ASSERT((ArenaSize & (ArenaSize - 1)) == 0,
23 "ArenaSize must be power of 2");
24 FL_STATIC_ASSERT(DescriptorCount >= 4, "DescriptorCount must be >= 4");
25 FL_STATIC_ASSERT(ArenaSize >= 32, "ArenaSize must be >= 32");
26
27public:
29 enum { MAX_MESSAGE_LENGTH = 512 };
30
32 struct Descriptor {
33 fl::u32 mStartIdx;
34 fl::u16 mLength;
35 fl::u16 mPadding;
36
38 };
39
41
43 bool push(const fl::string& msg);
44
46 bool push(const char* str);
47
49 bool tryPop(const char** outPtr, fl::u16* outLen);
50
52 void commit();
53
55 fl::u32 droppedCount() const;
56
58 fl::size size() const;
59
61 bool empty() const;
62
65 return DescriptorCount - 1; // One slot reserved for full/empty distinction
66 }
67
68private:
70 bool push(const char* str, fl::u16 len);
71 static fl::u16 boundedStrlen(const char* str, fl::u16 maxLen);
72 bool arenaHasSpace(fl::u32 aHead, fl::u32 aTail, fl::u16 len) const;
73 fl::u32 loadHead() const;
74 fl::u32 loadTail() const;
75 fl::u32 loadArenaTail() const;
76 void atomicIncDropped();
77
78 // Member variables
79 Descriptor mDescriptors[DescriptorCount];
80 char mArena[ArenaSize];
81
82 // Ring indices (modified under critical section for memory ordering)
83 volatile fl::u32 mHead;
84 volatile fl::u32 mTail;
85 volatile fl::u32 mArenaHead;
86 volatile fl::u32 mArenaTail;
87
88 volatile fl::u32 mDropped;
89};
90
91} // namespace fl
FL_STATIC_ASSERT(ArenaSize >=32, "ArenaSize must be >= 32")
bool push(const fl::string &msg)
Push a message from fl::string (ISR-safe)
volatile fl::u32 mArenaHead
Producer write position (arena)
volatile fl::u32 mTail
Consumer read position (descriptor ring)
static fl::u16 boundedStrlen(const char *str, fl::u16 maxLen)
volatile fl::u32 mDropped
Count of dropped messages (overflow)
FL_STATIC_ASSERT((DescriptorCount &(DescriptorCount - 1))==0, "DescriptorCount must be power of 2")
fl::u32 loadArenaTail() const
FL_STATIC_ASSERT(DescriptorCount >=4, "DescriptorCount must be >= 4")
bool empty() const
Check if queue is empty.
fl::size size() const
Get current number of messages in queue.
bool tryPop(const char **outPtr, fl::u16 *outLen)
Consumer: Try to pop one message (main thread only)
FL_STATIC_ASSERT((ArenaSize &(ArenaSize - 1))==0, "ArenaSize must be power of 2")
constexpr fl::size capacity() const
Get maximum descriptor capacity.
fl::u32 droppedCount() const
Get number of messages dropped due to overflow.
bool arenaHasSpace(fl::u32 aHead, fl::u32 aTail, fl::u16 len) const
void commit()
Consumer: Commit the popped message to free space (main thread only)
Descriptor mDescriptors[DescriptorCount]
Ring of message descriptors.
volatile fl::u32 mHead
Producer write position (descriptor ring)
volatile fl::u32 mArenaTail
Consumer read position (arena)
char mArena[ArenaSize]
String storage arena.
#define constexpr
Declares that it is possible to evaluate a value at compile time, introduced in C++11.
Definition cpp_compat.h:15
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Portable compile-time assertion wrapper.
fl::u32 mStartIdx
Offset into arena where message starts.
fl::u16 mLength
Length of message in bytes.
fl::u16 mPadding
Reserved for alignment (unused)
Descriptor for one log message.