1
0

LockFreeLIFO.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. #pragma once
  2. #include "nu/queue_node.h"
  3. #include <windows.h>
  4. #include <malloc.h>
  5. /* lock free stack object
  6. multiple threads can push and pop without locking
  7. note that order is not guaranteed. that is, if Thread 1 calls Insert before Thread 2, Thread 2's item might still make it in before.
  8. */
  9. #ifdef __cplusplus
  10. #define NU_LIFO_INLINE inline
  11. extern "C" {
  12. #else
  13. #define NX_ATOMIC_INLINE
  14. #endif
  15. typedef SLIST_HEADER lifo_t;
  16. /* use this to allocate an object that will go into this */
  17. NU_LIFO_INLINE static queue_node_t *lifo_malloc(size_t bytes) { return (queue_node_t *)_aligned_malloc(bytes, MEMORY_ALLOCATION_ALIGNMENT); }
  18. NU_LIFO_INLINE static void lifo_free(queue_node_t *ptr) { _aligned_free(ptr); }
  19. NU_LIFO_INLINE static lifo_t *lifo_create() { return (lifo_t *)_aligned_malloc(sizeof(SLIST_HEADER), MEMORY_ALLOCATION_ALIGNMENT); }
  20. NU_LIFO_INLINE static void lifo_destroy(lifo_t *lifo) { _aligned_free(lifo); }
  21. NU_LIFO_INLINE static void lifo_init(lifo_t *lifo) { InitializeSListHead(lifo); }
  22. NU_LIFO_INLINE static void lifo_push(lifo_t *lifo, queue_node_t *cl) { InterlockedPushEntrySList(lifo, cl); }
  23. NU_LIFO_INLINE static queue_node_t *lifo_pop(lifo_t *lifo) { return InterlockedPopEntrySList(lifo); }
  24. #ifdef __cplusplus
  25. }
  26. #endif