diff --git a/include/metalsvm/config.h.example b/include/metalsvm/config.h.example index 783461f1..14710be2 100644 --- a/include/metalsvm/config.h.example +++ b/include/metalsvm/config.h.example @@ -30,6 +30,7 @@ extern "C" { #define DEFAULT_STACK_SIZE (32*1024) #define KMSG_SIZE (1024*1024) #define PAGE_SIZE 4096 +#define MAILBOX_SIZE 2 #define TIMER_FREQ 100 /* in HZ */ #define CLOCK_TICK_RATE 1193182 /* 8254 chip's internal oscillator frequency */ diff --git a/include/metalsvm/mailbox.h b/include/metalsvm/mailbox.h index 0179b5a5..30027503 100644 --- a/include/metalsvm/mailbox.h +++ b/include/metalsvm/mailbox.h @@ -29,17 +29,20 @@ extern "C" { #endif typedef struct { - void* mail; - sem_t consume, produce; + void* buffer[MAILBOX_SIZE]; + int wpos, rpos; + sem_t mails; + sem_t boxes; } mailbox_t; inline static int mailbox_init(mailbox_t* m) { if (BUILTIN_EXPECT(!m, 0)) return -1; - m->mail = NULL; - sem_init(&m->consume, 0); - sem_init(&m->produce, 1); + memset(m->buffer, 0x00, sizeof(void*)*MAILBOX_SIZE); + m->wpos = m->rpos = 0; + sem_init(&m->mails, 0); + sem_init(&m->boxes, MAILBOX_SIZE); return 0; } @@ -48,8 +51,8 @@ inline static int mailbox_destroy(mailbox_t* m) { if (BUILTIN_EXPECT(!m, 0)) return -1; - sem_destroy(&m->consume); - sem_destroy(&m->produce); + sem_destroy(&m->mails); + sem_destroy(&m->boxes); return 0; } @@ -58,9 +61,10 @@ inline static int mailbox_post(mailbox_t* m, void* mail) { if (BUILTIN_EXPECT(!m, 0)) return -1; - sem_wait(&m->produce); - m->mail = mail; - sem_post(&m->consume); + sem_wait(&m->boxes); + m->buffer[m->wpos] = mail; + m->wpos = (m->wpos+1) % MAILBOX_SIZE; + sem_post(&m->mails); return 0; } @@ -69,10 +73,11 @@ inline static int mailbox_fetch(mailbox_t* m, void** mail) { if (BUILTIN_EXPECT(!m || !mail, 0)) return -1; - sem_wait(&m->consume); - *mail = m->mail; - m->mail = NULL; - sem_post(&m->produce); + sem_wait(&m->mails); + *mail = m->buffer[m->rpos]; + m->buffer[m->rpos] = NULL; + m->rpos = (m->rpos+1) % MAILBOX_SIZE; + sem_post(&m->boxes); return 0; } @@ -81,11 +86,12 @@ inline static int mailbox_tryfetch(mailbox_t* m, void** mail) { if (BUILTIN_EXPECT(!m || !mail, 0)) return -1; - if (sem_trywait(&m->consume) != 0) + if (sem_trywait(&m->mails) != 0) return -1; - *mail = m->mail; - m->mail = NULL; - sem_post(&m->produce); + *mail = m->buffer[m->rpos]; + m->buffer[m->rpos] = NULL; + m->rpos = (m->rpos+1) % MAILBOX_SIZE; + sem_post(&m->boxes); return 0; }