atomicity.h revision 117397
1117397Skan// Low-level functions for atomic operations: x86, x >= 3 version -*- C++ -*- 2117397Skan 3117397Skan// Copyright (C) 2003 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 18117397Skan// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 30117397Skan#ifndef _BITS_ATOMICITY_H 31117397Skan#define _BITS_ATOMICITY_H 1 32117397Skan 33117397Skantypedef int _Atomic_word; 34117397Skan 35117397Skantemplate <int __inst> 36117397Skanstruct __Atomicity_lock 37117397Skan{ 38117397Skan static volatile _Atomic_word _S_atomicity_lock; 39117397Skan}; 40117397Skan 41117397Skantemplate <int __inst> 42117397Skanvolatile _Atomic_word __Atomicity_lock<__inst>::_S_atomicity_lock = 0; 43117397Skan 44117397Skantemplate volatile _Atomic_word __Atomicity_lock<0>::_S_atomicity_lock; 45117397Skan 46117397Skanstatic inline _Atomic_word 47117397Skan__attribute__ ((__unused__)) 48117397Skan__exchange_and_add (volatile _Atomic_word *__mem, int __val) 49117397Skan{ 50117397Skan register _Atomic_word __result, __tmp = 1; 51117397Skan 52117397Skan /* obtain the atomic exchange/add spin lock */ 53117397Skan do { 54117397Skan __asm__ __volatile__ ("xchg{l} {%0,%1|%1,%0}" 55117397Skan : "+m" (__Atomicity_lock<0>::_S_atomicity_lock), 56117397Skan "+r" (__tmp)); 57117397Skan } while (__tmp); 58117397Skan 59117397Skan __result = *__mem; 60117397Skan *__mem += __val; 61117397Skan 62117397Skan /* release spin lock */ 63117397Skan __Atomicity_lock<0>::_S_atomicity_lock = 0; 64117397Skan 65117397Skan return __result; 66117397Skan} 67117397Skan 68117397Skanstatic inline void 69117397Skan__attribute__ ((__unused__)) 70117397Skan__atomic_add (volatile _Atomic_word* __mem, int __val) 71117397Skan{ 72117397Skan __exchange_and_add (__mem, __val); 73117397Skan} 74117397Skan 75117397Skan#endif /* atomicity.h */ 76