Align the MPMC queue to pagesize

This commit is contained in:
2026-03-09 18:01:11 +01:00
parent f3c4cb7b76
commit 1fa306643f
2 changed files with 11 additions and 7 deletions

View File

@@ -31,3 +31,5 @@ Making the LF MPMC queue generic and in a seperate header file
4.0: Implementing a semaphore in the LF MPMC queue to wake up consumers when there is items in the queue instead of spinning (busy waiting) or sleeping, this makes the queue spin only when the slots are transitionning (multiple consumers claiming the same slot)
Making the MPMC queue platform agnostic
Align the MPMC queue to pagesize

View File

@@ -58,15 +58,13 @@ typedef struct {
/* INIT */
/* ----------------------------------------------------------- */
static void mpmc_init(MPMCQueue *q, size_t max_capacity) {
if ((max_capacity & (max_capacity - 1)) != 0) {
fprintf(stderr, "capacity must be power of two\n");
exit(1);
}
q->capacity = max_capacity;
q->mask = max_capacity - 1;
size_t bytes = sizeof(MPMCSlot) * max_capacity;
u32 pagesize = plat_get_pagesize();
size_t bytes = ALIGN_UP_POW2(sizeof(MPMCSlot) * max_capacity, pagesize);
q->slots = (MPMCSlot *)plat_mem_reserve(bytes);
@@ -75,12 +73,16 @@ static void mpmc_init(MPMCQueue *q, size_t max_capacity) {
exit(1);
}
q->commit_step = (64ull * 1024 * 1024) / sizeof(MPMCSlot);
u64 commit_bytes = pagesize;
commit_bytes = ALIGN_UP_POW2(commit_bytes, pagesize);
q->commit_step = commit_bytes / sizeof(MPMCSlot);
atomic_flag_clear(&q->commit_lock);
q->committed = q->commit_step;
plat_mem_commit(q->slots, q->commit_step * sizeof(MPMCSlot));
plat_mem_commit(q->slots, commit_bytes);
for (size_t i = 0; i < q->committed; i++) {
atomic_init(&q->slots[i].seq, i);