LockFreeLIFO.c 996 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "LockFreeLIFO.h"
  2. #include "foundation/atomics.h"
  3. /* win32 implementation */
  4. void lifo_init(lifo_t *lifo)
  5. {
  6. lifo->head = 0;
  7. lifo->aba = 0;
  8. }
  9. #if 0 // defined in LockFreeLIFO.asm
  10. void lifo_push(lifo_t *lifo, queue_node_t *cl)
  11. {
  12. queue_node_t *new_head = cl;
  13. queue_node_t *old_head = 0;
  14. do
  15. {
  16. old_head = (queue_node_t *)lifo->head;
  17. new_head->Next = old_head;
  18. } while (!nx_atomic_cmpxchg_pointer(old_head, new_head, (void * volatile *)&lifo->head));
  19. }
  20. queue_node_t *lifo_pop(lifo_t *lifo)
  21. {
  22. lifo_t old_head, new_head;
  23. do
  24. {
  25. old_head = *lifo;
  26. if (!old_head.head)
  27. return 0;
  28. new_head.head = old_head.head->Next;
  29. new_head.aba = old_head.aba+1;
  30. } while (!nx_atomic_cmpxchg2(*(int64_t *)&old_head, *(int64_t *)&new_head, (volatile int64_t *)&lifo->head));
  31. return (queue_node_t *)old_head.head;
  32. }
  33. #endif
  34. queue_node_t *lifo_malloc(size_t bytes)
  35. {
  36. return _aligned_malloc(bytes, MEMORY_ALLOCATION_ALIGNMENT);
  37. }
  38. void lifo_free(queue_node_t *ptr)
  39. {
  40. _aligned_free(ptr);
  41. }