Project reordering and mpmc code
This commit is contained in:
62
lf_mpmc.h
62
lf_mpmc.h
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "base.h"
|
||||
|
||||
// Cache align abstraction
|
||||
#define CACHELINE 64
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
@@ -10,6 +11,7 @@
|
||||
#define CACHE_ALIGN __attribute__((aligned(CACHELINE)))
|
||||
#endif
|
||||
|
||||
// Compiler hints
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#define likely(x) __builtin_expect((x), 1)
|
||||
#define unlikely(x) __builtin_expect((x), 0)
|
||||
@@ -24,6 +26,65 @@ static void cpu_pause(void) {
|
||||
#endif
|
||||
}
|
||||
|
||||
// Semaphores
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
typedef struct plat_sem {
|
||||
HANDLE handle;
|
||||
} plat_sem;
|
||||
|
||||
static b32 plat_sem_init(plat_sem *s, u32 initial) {
|
||||
s->handle = CreateSemaphore(NULL, initial, LONG_MAX, NULL);
|
||||
return s->handle != NULL;
|
||||
}
|
||||
|
||||
static void plat_sem_wait(plat_sem *s) {
|
||||
WaitForSingleObject(s->handle, INFINITE);
|
||||
}
|
||||
|
||||
// static b32 plat_sem_trywait(HANDLE sem) { // Comment to prevent warning: unused function
|
||||
// DWORD r = WaitForSingleObject(sem, 0);
|
||||
// return r == WAIT_OBJECT_0;
|
||||
// }
|
||||
|
||||
static void plat_sem_post(plat_sem *s, u32 count) {
|
||||
ReleaseSemaphore(s->handle, count, NULL);
|
||||
}
|
||||
|
||||
// static void plat_sem_destroy(plat_sem *s) { // Comment to prevent warning: unused function
|
||||
// if (s->handle) {
|
||||
// CloseHandle(s->handle);
|
||||
// s->handle = NULL;
|
||||
// }
|
||||
// }
|
||||
|
||||
#elif defined(__linux__)
|
||||
#include <semaphore.h>
|
||||
|
||||
typedef struct plat_sem {
|
||||
sem_t sem;
|
||||
} plat_sem;
|
||||
|
||||
static b32 plat_sem_init(plat_sem *s, u32 initial) {
|
||||
return sem_init(&s->sem, 0, initial) == 0;
|
||||
}
|
||||
|
||||
static void plat_sem_wait(plat_sem *s) {
|
||||
while (sem_wait(&s->sem) == -1 && errno == EINTR) {
|
||||
}
|
||||
}
|
||||
|
||||
// static b32 plat_sem_trywait(sem_t *sem) { return sem_trywait(sem) == 0; } // Comment to prevent warning: unused function
|
||||
|
||||
static void plat_sem_post(plat_sem *s, u32 count) {
|
||||
for (u32 i = 0; i < count; i++) {
|
||||
sem_post(&s->sem);
|
||||
}
|
||||
}
|
||||
|
||||
// static void plat_sem_destroy(plat_sem *s) { sem_destroy(&s->sem); } // Comment to prevent warning: unused function
|
||||
|
||||
#endif
|
||||
|
||||
typedef struct plat_sem plat_sem;
|
||||
|
||||
typedef struct CACHE_ALIGN {
|
||||
@@ -235,6 +296,7 @@ static void mpmc_push_work(MPMCQueue *q, void *item) {
|
||||
atomic_fetch_add(&q->work_count, 1);
|
||||
plat_sem_post(&q->items_sem, 1);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------- */
|
||||
/* POP */
|
||||
/* ----------------------------------------------------------- */
|
||||
|
||||
Reference in New Issue
Block a user