Skip to content

Instantly share code, notes, and snippets.

@nitsanw
Created April 19, 2015 13:50
Show Gist options
  • Select an option

  • Save nitsanw/b7b3a61d4fc022c9ee69 to your computer and use it in GitHub Desktop.

Select an option

Save nitsanw/b7b3a61d4fc022c9ee69 to your computer and use it in GitHub Desktop.
Vyukov MPSC
struct mpscq_node_t {
mpscq_node_t* volatile next;
void* state;
};
struct mpscq_t {
mpscq_node_t* volatile head;
mpscq_node_t* tail;
};
void mpscq_create(mpscq_t* self, mpscq_node_t* stub) {
stub->next = 0;
self->head = stub;
self->tail = stub;
}
void mpscq_offer(mpscq_t* self, mpscq_node_t* n) {
n->next = NULL;
mpscq_node_t* prev = XCHG(&self->head, n); // serialization-point wrt producers, acquire-release
prev->next = n; // serialization-point wrt consumer, release
}
mpscq_node_t* mpscq_poll(mpscq_t* self) {
mpscq_node_t* tail = self->tail;
mpscq_node_t* next = tail->next; // serialization-point wrt producers, acquire
if (next != NULL) {
self->tail = next;
tail->state = next->state;
return tail;
}
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment