locks.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // locks.h - Thread synchronization primitives. MIPS implementation.
  2. /* Copyright (C) 2003 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. /* Integer type big enough for object address. */
  10. typedef unsigned obj_addr_t __attribute__((__mode__(__pointer__)));
  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 __sync_bool_compare_and_swap(addr, old, new_val);
  21. }
  22. // Set *addr to new_val with release semantics, i.e. making sure
  23. // that prior loads and stores complete before this
  24. // assignment.
  25. inline static void
  26. release_set(volatile obj_addr_t *addr, obj_addr_t new_val)
  27. {
  28. __sync_synchronize();
  29. *(addr) = new_val;
  30. }
  31. // Compare_and_swap with release semantics instead of acquire semantics.
  32. // On many architecture, the operation makes both guarantees, so the
  33. // implementation can be the same.
  34. inline static bool
  35. compare_and_swap_release(volatile obj_addr_t *addr,
  36. obj_addr_t old,
  37. obj_addr_t new_val)
  38. {
  39. return __sync_bool_compare_and_swap(addr, old, new_val);
  40. }
  41. // Ensure that subsequent instructions do not execute on stale
  42. // data that was loaded from memory before the barrier.
  43. // On X86, the hardware ensures that reads are properly ordered.
  44. inline static void
  45. read_barrier()
  46. {
  47. __sync_synchronize();
  48. }
  49. // Ensure that prior stores to memory are completed with respect to other
  50. // processors.
  51. inline static void
  52. write_barrier()
  53. {
  54. __sync_synchronize();
  55. }
  56. #endif // __SYSDEP_LOCKS_H__