locks.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* locks.h - Thread synchronization primitives. X86/x86-64 implementation.
  2. Copyright (C) 2002, 2011 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 __sync_bool_compare_and_swap (addr, old, new_val);
  21. }
  22. // Ensure that subsequent instructions do not execute on stale
  23. // data that was loaded from memory before the barrier.
  24. // On X86/x86-64, the hardware ensures that reads are properly ordered.
  25. inline static void
  26. read_barrier()
  27. {
  28. }
  29. // Ensure that prior stores to memory are completed with respect to other
  30. // processors.
  31. inline static void
  32. write_barrier()
  33. {
  34. /* x86-64/X86 does not reorder writes. We just need to ensure that
  35. gcc also doesn't. */
  36. __asm__ __volatile__(" " : : : "memory");
  37. }
  38. // Set *addr to new_val with release semantics, i.e. making sure
  39. // that prior loads and stores complete before this
  40. // assignment.
  41. // On X86/x86-64, the hardware shouldn't reorder reads and writes,
  42. // so we just have to convince gcc not to do it either.
  43. inline static void
  44. release_set(volatile obj_addr_t *addr, obj_addr_t new_val)
  45. {
  46. write_barrier ();
  47. *(addr) = new_val;
  48. }
  49. // Compare_and_swap with release semantics instead of acquire semantics.
  50. // On many architecture, the operation makes both guarantees, so the
  51. // implementation can be the same.
  52. inline static bool
  53. compare_and_swap_release(volatile obj_addr_t *addr,
  54. obj_addr_t old,
  55. obj_addr_t new_val)
  56. {
  57. return compare_and_swap(addr, old, new_val);
  58. }
  59. #endif