xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/config/cpu/i386/atomicity.h (revision 03a78d155d6fff5698289342b62759a75b20d130)
1*03a78d15Sespie // Low-level functions for atomic operations: x86, x >= 3 version  -*- C++ -*-
2*03a78d15Sespie 
3*03a78d15Sespie // Copyright (C) 2003 Free Software Foundation, Inc.
4*03a78d15Sespie //
5*03a78d15Sespie // This file is part of the GNU ISO C++ Library.  This library is free
6*03a78d15Sespie // software; you can redistribute it and/or modify it under the
7*03a78d15Sespie // terms of the GNU General Public License as published by the
8*03a78d15Sespie // Free Software Foundation; either version 2, or (at your option)
9*03a78d15Sespie // any later version.
10*03a78d15Sespie 
11*03a78d15Sespie // This library is distributed in the hope that it will be useful,
12*03a78d15Sespie // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*03a78d15Sespie // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*03a78d15Sespie // GNU General Public License for more details.
15*03a78d15Sespie 
16*03a78d15Sespie // You should have received a copy of the GNU General Public License along
17*03a78d15Sespie // with this library; see the file COPYING.  If not, write to the Free
18*03a78d15Sespie // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19*03a78d15Sespie // USA.
20*03a78d15Sespie 
21*03a78d15Sespie // As a special exception, you may use this file as part of a free software
22*03a78d15Sespie // library without restriction.  Specifically, if other files instantiate
23*03a78d15Sespie // templates or use macros or inline functions from this file, or you compile
24*03a78d15Sespie // this file and link it with other files to produce an executable, this
25*03a78d15Sespie // file does not by itself cause the resulting executable to be covered by
26*03a78d15Sespie // the GNU General Public License.  This exception does not however
27*03a78d15Sespie // invalidate any other reasons why the executable file might be covered by
28*03a78d15Sespie // the GNU General Public License.
29*03a78d15Sespie 
30*03a78d15Sespie #ifndef _BITS_ATOMICITY_H
31*03a78d15Sespie #define _BITS_ATOMICITY_H	1
32*03a78d15Sespie 
33*03a78d15Sespie typedef int _Atomic_word;
34*03a78d15Sespie 
35*03a78d15Sespie template <int __inst>
36*03a78d15Sespie struct __Atomicity_lock
37*03a78d15Sespie {
38*03a78d15Sespie   static volatile _Atomic_word _S_atomicity_lock;
39*03a78d15Sespie };
40*03a78d15Sespie 
41*03a78d15Sespie template <int __inst>
42*03a78d15Sespie volatile _Atomic_word __Atomicity_lock<__inst>::_S_atomicity_lock = 0;
43*03a78d15Sespie 
44*03a78d15Sespie template volatile _Atomic_word __Atomicity_lock<0>::_S_atomicity_lock;
45*03a78d15Sespie 
46*03a78d15Sespie static inline _Atomic_word
47*03a78d15Sespie __attribute__ ((__unused__))
__exchange_and_add(volatile _Atomic_word * __mem,int __val)48*03a78d15Sespie __exchange_and_add (volatile _Atomic_word *__mem, int __val)
49*03a78d15Sespie {
50*03a78d15Sespie   register _Atomic_word __result, __tmp = 1;
51*03a78d15Sespie 
52*03a78d15Sespie   /* obtain the atomic exchange/add spin lock */
53*03a78d15Sespie   do {
54*03a78d15Sespie     __asm__ __volatile__ ("xchg{l} {%0,%1|%1,%0}"
55*03a78d15Sespie 			  : "+m" (__Atomicity_lock<0>::_S_atomicity_lock),
56*03a78d15Sespie 			    "+r" (__tmp));
57*03a78d15Sespie   } while (__tmp);
58*03a78d15Sespie 
59*03a78d15Sespie   __result = *__mem;
60*03a78d15Sespie   *__mem += __val;
61*03a78d15Sespie 
62*03a78d15Sespie   /* release spin lock */
63*03a78d15Sespie   __Atomicity_lock<0>::_S_atomicity_lock = 0;
64*03a78d15Sespie 
65*03a78d15Sespie   return __result;
66*03a78d15Sespie }
67*03a78d15Sespie 
68*03a78d15Sespie static inline void
69*03a78d15Sespie __attribute__ ((__unused__))
__atomic_add(volatile _Atomic_word * __mem,int __val)70*03a78d15Sespie __atomic_add (volatile _Atomic_word* __mem, int __val)
71*03a78d15Sespie {
72*03a78d15Sespie   __exchange_and_add (__mem, __val);
73*03a78d15Sespie }
74*03a78d15Sespie 
75*03a78d15Sespie #endif /* atomicity.h */
76