Advertisement
Guest User

Untitled

a guest
Nov 27th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include "queue.h"
  2.  
  3.  int Initialize(locklessQueue * queue)
  4.  {
  5.      queue->Tail = (node*)malloc(sizeof(node));
  6.      if(queue->Tail == NULL)
  7.      {
  8.         fprintf(stderr, "Failed to allocate memory for initialization.");
  9.         return 1;
  10.      }
  11.      queue->Tail->next = NULL;
  12.      queue->Tail->count = 0;
  13.      queue->Head = queue->Tail;
  14.      return 0;
  15.  }
  16.  
  17. int dequeue(locklessQueue * queue, void** data)
  18.  {
  19.     node *tail, *head, *next;
  20.  
  21.     tail = NULL;
  22.     head = NULL;
  23.     next = NULL;
  24.  
  25.     while(1 == 1)
  26.     {
  27.         tail = queue->Tail;
  28.         head = queue->Head;
  29.         next = tail->next;
  30.  
  31.         if(tail == queue->Tail)
  32.         {
  33.             if(tail == head)
  34.             {
  35.                 if(next == NULL)
  36.                 {
  37.                     return 1; //List empty
  38.                 }
  39.                 CAS(&queue->Head, head, next);
  40.             }
  41.             else//Most errors in this else
  42.             {
  43.                 *data = next->data;
  44.                 if(CAS(&queue->Tail, tail, next))
  45.                 {
  46.                     free(tail);
  47.                     tail = NULL;
  48.                     return 0;
  49.                 }
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement