Skip to content

Instantly share code, notes, and snippets.

@porcoesphino
Last active December 23, 2015 12:59
Show Gist options
  • Save porcoesphino/6639144 to your computer and use it in GitHub Desktop.
Save porcoesphino/6639144 to your computer and use it in GitHub Desktop.
#define LEN 20
void volatile* raw_queue[LEN];
int volatile reader_ctr = 0;
int volatile writer_ctr = 0;
_Bool push_packet(void* pointer) {
ASSERT(pointer != NULL);
bool result = false;
DISABLED_INTERRUPTS(
int nextElement = (writer_ctr + 1) % LEN;
if(nextElement != reader_ctr) {
raw_queue[writer_ctr] = pointer;
writer_ctr = nextElement;
result = true;
}
)
return result;
}
void* pop_packet(void) {
if(reader_ctr == writer_ctr) {
return (void*) NULL;
}
int nextElement = (reader_ctr + 1) % LEN;
void* result = (void*) raw_queue[reader_ctr];
raw_queue[reader_ctr] = NULL;
reader_ctr = nextElement;
return (void*) current_packet_head;
}
_Bool is_empty() {
return reader_ctr == writer_ctr;
}
int get_approx_size() {
int result = writer_ctr - reader_ctr;
if (result >= 0) {
return result;
} else {
return LEN + result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment