pt-machine.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Machine-dependent pthreads configuration and inline functions.
  2. powerpc version.
  3. Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License as
  7. published by the Free Software Foundation; either version 2.1 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If
  15. not, write to the Free Software Foundation, Inc.,
  16. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /* These routines are from Appendix G of the 'PowerPC 601 RISC Microprocessor
  18. User's Manual', by IBM and Motorola. */
  19. #ifndef _PT_MACHINE_H
  20. #define _PT_MACHINE_H 1
  21. #include <features.h>
  22. #ifndef PT_EI
  23. # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
  24. # define PT_EI static inline __attribute__((always_inline))
  25. # else
  26. # define PT_EI extern inline __attribute__((always_inline))
  27. # endif
  28. #endif
  29. /* For multiprocessor systems, we want to ensure all memory accesses
  30. are completed before we reset a lock. On other systems, we still
  31. need to make sure that the compiler has flushed everything to memory. */
  32. #define MEMORY_BARRIER() __asm__ __volatile__ ("sync" : : : "memory")
  33. /* Get some notion of the current stack. Need not be exactly the top
  34. of the stack, just something somewhere in the current frame. */
  35. #define CURRENT_STACK_FRAME stack_pointer
  36. register char * stack_pointer __asm__ ("r1");
  37. /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
  38. struct _pthread_descr_struct;
  39. register struct _pthread_descr_struct *__thread_self __asm__("r2");
  40. /* Return the thread descriptor for the current thread. */
  41. #define THREAD_SELF __thread_self
  42. /* Initialize the thread-unique value. */
  43. #define INIT_THREAD_SELF(descr, nr) (__thread_self = (descr))
  44. /* Compare-and-swap for semaphores. */
  45. /* note that test-and-set(x) is the same as !compare-and-swap(x, 0, 1) */
  46. #define HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
  47. #define IMPLEMENT_TAS_WITH_CAS
  48. PT_EI int
  49. __compare_and_swap (long int *p, long int oldval, long int newval)
  50. {
  51. int ret;
  52. __asm__ __volatile__ (
  53. "0: lwarx %0,0,%1 ;"
  54. " xor. %0,%3,%0;"
  55. " bne 1f;"
  56. " stwcx. %2,0,%1;"
  57. " bne- 0b;"
  58. "1: "
  59. : "=&r"(ret)
  60. : "r"(p), "r"(newval), "r"(oldval)
  61. : "cr0", "memory");
  62. /* This version of __compare_and_swap is to be used when acquiring
  63. a lock, so we don't need to worry about whether other memory
  64. operations have completed, but we do need to be sure that any loads
  65. after this point really occur after we have acquired the lock. */
  66. __asm__ __volatile__ ("isync" : : : "memory");
  67. return ret == 0;
  68. }
  69. PT_EI int
  70. __compare_and_swap_with_release_semantics (long int *p,
  71. long int oldval, long int newval)
  72. {
  73. int ret;
  74. MEMORY_BARRIER ();
  75. __asm__ __volatile__ (
  76. "0: lwarx %0,0,%1 ;"
  77. " xor. %0,%3,%0;"
  78. " bne 1f;"
  79. " stwcx. %2,0,%1;"
  80. " bne- 0b;"
  81. "1: "
  82. : "=&r"(ret)
  83. : "r"(p), "r"(newval), "r"(oldval)
  84. : "cr0", "memory");
  85. return ret == 0;
  86. }
  87. #endif /* pt-machine.h */