Created
April 19, 2015 13:50
-
-
Save nitsanw/b7b3a61d4fc022c9ee69 to your computer and use it in GitHub Desktop.
Vyukov MPSC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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