LockFreeItem.h 695 B

1234567891011121314151617181920212223242526272829303132333435
  1. #pragma once
  2. #include "foundation/atomics.h"
  3. #include "lfitem.h"
  4. /* This data structure holds one item, and returns you the old item when you replace it
  5. it's sort of a "stack of 1" */
  6. template <class value_t>
  7. class LockFreeItem
  8. {
  9. public:
  10. typedef value_t *ptr_t;
  11. LockFreeItem()
  12. {
  13. lfitem_init(&item);
  14. }
  15. value_t *GetItem()
  16. {
  17. return (value_t *)lfitem_get(&item);
  18. }
  19. // returns the previous value
  20. value_t *SetItem(value_t *new_item)
  21. {
  22. return (value_t *)lfitem_set(&item, new_item);
  23. }
  24. // if there's already a value, returns what you passed in
  25. value_t *SetItemIfZero(value_t *new_item)
  26. {
  27. return (value_t *)lfitem_set_if_zero(&item, new_item);
  28. }
  29. lfitem_value_t item;
  30. };