1117397Skan// Low-level functions for atomic operations: m68k version -*- C++ -*-
2117397Skan
3169691Skan// Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4117397Skan//
5117397Skan// This file is part of the GNU ISO C++ Library.  This library is free
6117397Skan// software; you can redistribute it and/or modify it under the
7117397Skan// terms of the GNU General Public License as published by the
8117397Skan// Free Software Foundation; either version 2, or (at your option)
9117397Skan// any later version.
10117397Skan
11117397Skan// This library is distributed in the hope that it will be useful,
12117397Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
13117397Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14117397Skan// GNU General Public License for more details.
15117397Skan
16117397Skan// You should have received a copy of the GNU General Public License along
17117397Skan// with this library; see the file COPYING.  If not, write to the Free
18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19117397Skan// USA.
20117397Skan
21117397Skan// As a special exception, you may use this file as part of a free software
22117397Skan// library without restriction.  Specifically, if other files instantiate
23117397Skan// templates or use macros or inline functions from this file, or you compile
24117397Skan// this file and link it with other files to produce an executable, this
25117397Skan// file does not by itself cause the resulting executable to be covered by
26117397Skan// the GNU General Public License.  This exception does not however
27117397Skan// invalidate any other reasons why the executable file might be covered by
28117397Skan// the GNU General Public License.
29117397Skan
30169691Skan#include <ext/atomicity.h>
31117397Skan
32169691Skan_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
33169691Skan
34122182Skan#if ( defined(__mc68020__) || defined(__mc68030__) \
35122182Skan      || defined(__mc68040__) || defined(__mc68060__) ) \
36122182Skan    && !defined(__mcpu32__)
37132720Skan  // These variants support compare-and-swap.
38132720Skan  _Atomic_word
39132720Skan  __attribute__ ((__unused__))
40132720Skan  __exchange_and_add(volatile _Atomic_word* __mem, int __val)
41132720Skan  {
42132720Skan    register _Atomic_word __result = *__mem;
43132720Skan    register _Atomic_word __temp;
44132720Skan    __asm__ __volatile__ ("1: move%.l %0,%1\n\t"
45132720Skan			  "add%.l %3,%1\n\t"
46132720Skan			  "cas%.l %0,%1,%2\n\t"
47132720Skan			  "jne 1b"
48132720Skan			  : "=d" (__result), "=&d" (__temp), "=m" (*__mem)
49132720Skan			  : "d" (__val), "0" (__result), "m" (*__mem));
50132720Skan    return __result;
51132720Skan  }
52117397Skan
53117397Skan#elif defined(__rtems__)
54132720Skan  // TAS/JBNE is unsafe on systems with strict priority-based scheduling.
55132720Skan  // Disable interrupts, which we can do only from supervisor mode.
56132720Skan  _Atomic_word
57132720Skan  __attribute__ ((__unused__))
58132720Skan  __exchange_and_add(volatile _Atomic_word* __mem, int __val)
59132720Skan  {
60132720Skan    _Atomic_word __result;
61132720Skan    short __level, __tmpsr;
62132720Skan    __asm__ __volatile__ ("move%.w %%sr,%0\n\tor%.l %0,%1\n\tmove%.w %1,%%sr"
63132720Skan			  : "=d"(__level), "=d"(__tmpsr) : "1"(0x700));
64132720Skan
65132720Skan    __result = *__mem;
66132720Skan    *__mem = __result + __val;
67132720Skan    __asm__ __volatile__ ("move%.w %0,%%sr" : : "d"(__level));
68132720Skan
69132720Skan    return __result;
70132720Skan  }
71117397Skan
72132720Skan#else
73132720Skan
74132720Skan  template<int __inst>
75132720Skan    struct _Atomicity_lock
76132720Skan    {
77132720Skan      static volatile unsigned char _S_atomicity_lock;
78132720Skan    };
79117397Skan
80132720Skan  template<int __inst>
81132720Skan  volatile unsigned char _Atomicity_lock<__inst>::_S_atomicity_lock = 0;
82132720Skan
83132720Skan  template volatile unsigned char _Atomicity_lock<0>::_S_atomicity_lock;
84132720Skan
85132720Skan  _Atomic_word
86132720Skan  __attribute__ ((__unused__))
87132720Skan  __exchange_and_add(volatile _Atomic_word* __mem, int __val)
88132720Skan  {
89132720Skan    _Atomic_word __result;
90132720Skan
91132720Skan    // bset with no immediate addressing (not SMP-safe)
92132720Skan#if defined(__mcf5200__) || defined(__mcf5300__)
93132720Skan    __asm__ __volatile__("1: bset.b #7,%0@\n\tjbne 1b"
94132720Skan			 : /* no outputs */
95132720Skan			 : "a"(&_Atomicity_lock<0>::_S_atomicity_lock)
96132720Skan			 : "cc", "memory");
97132720Skan
98132720Skan    // CPU32 and MCF5400 support test-and-set (SMP-safe).
99132720Skan#elif defined(__mcpu32__) || defined(__mcf5400__)
100132720Skan    __asm__ __volatile__("1: tas %0\n\tjbne 1b"
101132720Skan			 : "+m"(_Atomicity_lock<0>::_S_atomicity_lock)
102132720Skan			 : /* none */
103132720Skan			 : "cc");
104132720Skan
105132720Skan    // Use bset with immediate addressing for 68000/68010 (not SMP-safe)
106132720Skan    // NOTE: TAS is available on the 68000, but unsupported by some Amiga
107132720Skan    // memory controllers.
108122182Skan#else
109132720Skan    __asm__ __volatile__("1: bset.b #7,%0\n\tjbne 1b"
110132720Skan			 : "+m"(_Atomicity_lock<0>::_S_atomicity_lock)
111132720Skan			 : /* none */
112132720Skan			 : "cc");
113122182Skan#endif
114132720Skan
115132720Skan    __result = *__mem;
116132720Skan    *__mem = __result + __val;
117132720Skan
118132720Skan    _Atomicity_lock<0>::_S_atomicity_lock = 0;
119132720Skan
120132720Skan    return __result;
121132720Skan  }
122132720Skan
123122182Skan#endif /* TAS / BSET */
124117397Skan
125132720Skan  void
126132720Skan  __attribute__ ((__unused__))
127132720Skan  __atomic_add(volatile _Atomic_word* __mem, int __val)
128132720Skan  {
129132720Skan    // Careful: using add.l with a memory destination is not
130132720Skan    // architecturally guaranteed to be atomic.
131132720Skan    __exchange_and_add(__mem, __val);
132132720Skan  }
133169691Skan
134169691Skan_GLIBCXX_END_NAMESPACE
135