jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.4KB

  1. // Low-level functions for atomic operations: m68k version -*- C++ -*-
  2. // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 2, or (at your option)
  8. // any later version.
  9. // This 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
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License along
  14. // with this library; see the file COPYING. If not, write to the Free
  15. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. // USA.
  17. // As a special exception, you may use this file as part of a free software
  18. // library without restriction. Specifically, if other files instantiate
  19. // templates or use macros or inline functions from this file, or you compile
  20. // this file and link it with other files to produce an executable, this
  21. // file does not by itself cause the resulting executable to be covered by
  22. // the GNU General Public License. This exception does not however
  23. // invalidate any other reasons why the executable file might be covered by
  24. // the GNU General Public License.
  25. #ifndef _GLIBCXX_ATOMICITY_H
  26. #define _GLIBCXX_ATOMICITY_H 1
  27. typedef int _Atomic_word;
  28. #if ( defined(__mc68020__) || defined(__mc68030__) \
  29. || defined(__mc68040__) || defined(__mc68060__) ) \
  30. && !defined(__mcpu32__)
  31. // These variants support compare-and-swap.
  32. static inline _Atomic_word
  33. __attribute__ ((__unused__))
  34. __exchange_and_add (volatile _Atomic_word* __mem, int __val)
  35. {
  36. register _Atomic_word __result = *__mem;
  37. register _Atomic_word __temp;
  38. __asm__ __volatile__ ("1: move%.l %0,%1\n\t"
  39. "add%.l %3,%1\n\t"
  40. "cas%.l %0,%1,%2\n\t"
  41. "jne 1b"
  42. : "=d" (__result), "=&d" (__temp), "=m" (*__mem)
  43. : "d" (__val), "0" (__result), "m" (*__mem));
  44. return __result;
  45. }
  46. #elif defined(__rtems__)
  47. /*
  48. * TAS/JBNE is unsafe on systems with strict priority-based scheduling.
  49. * Disable interrupts, which we can do only from supervisor mode.
  50. */
  51. static inline _Atomic_word
  52. __attribute__ ((__unused__))
  53. __exchange_and_add (volatile _Atomic_word* __mem, int __val)
  54. {
  55. _Atomic_word __result;
  56. short __level, __tmpsr;
  57. __asm__ __volatile__ ("move%.w %%sr,%0\n\tor%.l %0,%1\n\tmove%.w %1,%%sr"
  58. : "=d" (__level), "=d" (__tmpsr) : "1" (0x700));
  59. __result = *__mem;
  60. *__mem = __result + __val;
  61. __asm__ __volatile__ ("move%.w %0,%%sr" : : "d" (__level));
  62. return __result;
  63. }
  64. #else
  65. template<int __inst>
  66. struct __Atomicity_lock {
  67. static volatile unsigned char _S_atomicity_lock;
  68. };
  69. template<int __inst>
  70. volatile unsigned char __Atomicity_lock<__inst>::_S_atomicity_lock = 0;
  71. template volatile unsigned char __Atomicity_lock<0>::_S_atomicity_lock;
  72. static inline _Atomic_word
  73. __attribute__ ((__unused__))
  74. __exchange_and_add (volatile _Atomic_word* __mem, int __val)
  75. {
  76. _Atomic_word __result;
  77. // bset with no immediate addressing (not SMP-safe)
  78. #if defined(__mcf5200__) || defined(__mcf5300__)
  79. __asm__ __volatile__ ("1: bset.b #7,%0@\n\tjbne 1b"
  80. : /* no outputs */
  81. : "a" (&__Atomicity_lock<0>::_S_atomicity_lock)
  82. : "cc", "memory");
  83. // CPU32 and MCF5400 support test-and-set (SMP-safe).
  84. #elif defined(__mcpu32__) || defined(__mcf5400__)
  85. __asm__ __volatile__ ("1: tas %0\n\tjbne 1b"
  86. : "+m" (__Atomicity_lock<0>::_S_atomicity_lock)
  87. : /* none */
  88. : "cc");
  89. // Use bset with immediate addressing for 68000/68010 (not SMP-safe)
  90. // NOTE: TAS is available on the 68000, but unsupported by some Amiga
  91. // memory controllers.
  92. #else
  93. __asm__ __volatile__ ("1: bset.b #7,%0\n\tjbne 1b"
  94. : "+m" (__Atomicity_lock<0>::_S_atomicity_lock)
  95. : /* none */
  96. : "cc");
  97. #endif
  98. __result = *__mem;
  99. *__mem = __result + __val;
  100. __Atomicity_lock<0>::_S_atomicity_lock = 0;
  101. return __result;
  102. }
  103. #endif /* TAS / BSET */
  104. static inline void
  105. __attribute__ ((__unused__))
  106. __atomic_add (volatile _Atomic_word* __mem, int __val)
  107. {
  108. // Careful: using add.l with a memory destination is not
  109. // architecturally guaranteed to be atomic.
  110. (void)__exchange_and_add (__mem, __val);
  111. }
  112. #endif /* !_GLIBCXX_ATOMICITY_H */