locks.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // locks.h - Thread synchronization primitives. AArch64 implementation.
  2. #ifndef __SYSDEP_LOCKS_H__
  3. #define __SYSDEP_LOCKS_H__
  4. typedef size_t obj_addr_t; /* Integer type big enough for object */
  5. /* address. */
  6. // Atomically replace *addr by new_val if it was initially equal to old.
  7. // Return true if the comparison succeeded.
  8. // Assumed to have acquire semantics, i.e. later memory operations
  9. // cannot execute before the compare_and_swap finishes.
  10. inline static bool
  11. compare_and_swap(volatile obj_addr_t *addr,
  12. obj_addr_t old,
  13. obj_addr_t new_val)
  14. {
  15. return __sync_bool_compare_and_swap(addr, old, new_val);
  16. }
  17. // Set *addr to new_val with release semantics, i.e. making sure
  18. // that prior loads and stores complete before this
  19. // assignment.
  20. inline static void
  21. release_set(volatile obj_addr_t *addr, obj_addr_t new_val)
  22. {
  23. __sync_synchronize();
  24. *addr = new_val;
  25. }
  26. // Compare_and_swap with release semantics instead of acquire semantics.
  27. // On many architecture, the operation makes both guarantees, so the
  28. // implementation can be the same.
  29. inline static bool
  30. compare_and_swap_release(volatile obj_addr_t *addr,
  31. obj_addr_t old,
  32. obj_addr_t new_val)
  33. {
  34. return __sync_bool_compare_and_swap(addr, old, new_val);
  35. }
  36. // Ensure that subsequent instructions do not execute on stale
  37. // data that was loaded from memory before the barrier.
  38. inline static void
  39. read_barrier()
  40. {
  41. __sync_synchronize();
  42. }
  43. // Ensure that prior stores to memory are completed with respect to other
  44. // processors.
  45. inline static void
  46. write_barrier()
  47. {
  48. __sync_synchronize();
  49. }
  50. #endif