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