locks.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // locks.h - Thread synchronization primitives. PowerPC implementation.
  2. /* Copyright (C) 2002,2008 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. #ifndef __SYSDEP_LOCKS_H__
  8. #define __SYSDEP_LOCKS_H__
  9. typedef size_t obj_addr_t; /* Integer type big enough for object */
  10. /* address. */
  11. // Atomically replace *addr by new_val if it was initially equal to old.
  12. // Return true if the comparison succeeded.
  13. // Assumed to have acquire semantics, i.e. later memory operations
  14. // cannot execute before the compare_and_swap finishes.
  15. inline static bool
  16. compare_and_swap (volatile obj_addr_t *addr,
  17. obj_addr_t old,
  18. obj_addr_t new_val)
  19. {
  20. return __atomic_compare_exchange_n (addr, &old, new_val, 0,
  21. __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
  22. }
  23. // Set *addr to new_val with release semantics, i.e. making sure
  24. // that prior loads and stores complete before this
  25. // assignment.
  26. inline static void
  27. release_set (volatile obj_addr_t *addr, obj_addr_t new_val)
  28. {
  29. __atomic_store_n(addr, new_val, __ATOMIC_RELEASE);
  30. }
  31. // Compare_and_swap with release semantics instead of acquire semantics.
  32. inline static bool
  33. compare_and_swap_release (volatile obj_addr_t *addr, obj_addr_t old,
  34. obj_addr_t new_val)
  35. {
  36. return __atomic_compare_exchange_n (addr, &old, new_val, 0,
  37. __ATOMIC_RELEASE, __ATOMIC_RELAXED);
  38. }
  39. // Ensure that subsequent instructions do not execute on stale
  40. // data that was loaded from memory before the barrier.
  41. inline static void
  42. read_barrier ()
  43. {
  44. __atomic_thread_fence (__ATOMIC_ACQUIRE);
  45. }
  46. // Ensure that prior stores to memory are completed with respect to other
  47. // processors.
  48. inline static void
  49. write_barrier ()
  50. {
  51. __atomic_thread_fence (__ATOMIC_RELEASE);
  52. }
  53. #endif