Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "queue.h"
- int Initialize(locklessQueue * queue)
- {
- queue->Tail = (node*)malloc(sizeof(node));
- if(queue->Tail == NULL)
- {
- fprintf(stderr, "Failed to allocate memory for initialization.");
- return 1;
- }
- queue->Tail->next = NULL;
- queue->Tail->count = 0;
- queue->Head = queue->Tail;
- return 0;
- }
- int dequeue(locklessQueue * queue, void** data)
- {
- node *tail, *head, *next;
- tail = NULL;
- head = NULL;
- next = NULL;
- while(1 == 1)
- {
- tail = queue->Tail;
- head = queue->Head;
- next = tail->next;
- if(tail == queue->Tail)
- {
- if(tail == head)
- {
- if(next == NULL)
- {
- return 1; //List empty
- }
- CAS(&queue->Head, head, next);
- }
- else//Most errors in this else
- {
- *data = next->data;
- if(CAS(&queue->Tail, tail, next))
- {
- free(tail);
- tail = NULL;
- return 0;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement