atomicity.h revision 169691
1117397Skan// Low-level functions for atomic operations: x86, x >= 3 version -*- C++ -*- 2117397Skan 3169691Skan// Copyright (C) 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 34132720Skan template<int __inst> 35132720Skan struct _Atomicity_lock 36132720Skan { 37132720Skan static volatile _Atomic_word _S_atomicity_lock; 38132720Skan }; 39117397Skan 40132720Skan template<int __inst> 41132720Skan volatile _Atomic_word _Atomicity_lock<__inst>::_S_atomicity_lock = 0; 42117397Skan 43132720Skan template volatile _Atomic_word _Atomicity_lock<0>::_S_atomicity_lock; 44132720Skan 45132720Skan _Atomic_word 46132720Skan __attribute__ ((__unused__)) 47132720Skan __exchange_and_add(volatile _Atomic_word* __mem, int __val) 48132720Skan { 49132720Skan register _Atomic_word __result, __tmp = 1; 50132720Skan 51132720Skan // Obtain the atomic exchange/add spin lock. 52132720Skan do 53132720Skan { 54132720Skan __asm__ __volatile__ ("xchg{l} {%0,%1|%1,%0}" 55132720Skan : "=m" (_Atomicity_lock<0>::_S_atomicity_lock), 56132720Skan "+r" (__tmp) 57132720Skan : "m" (_Atomicity_lock<0>::_S_atomicity_lock)); 58132720Skan } 59132720Skan while (__tmp); 60132720Skan 61132720Skan __result = *__mem; 62132720Skan *__mem += __val; 63132720Skan 64132720Skan // Release spin lock. 65132720Skan _Atomicity_lock<0>::_S_atomicity_lock = 0; 66132720Skan 67132720Skan return __result; 68132720Skan } 69132720Skan 70132720Skan void 71132720Skan __attribute__ ((__unused__)) 72132720Skan __atomic_add(volatile _Atomic_word* __mem, int __val) 73132720Skan { __exchange_and_add(__mem, __val); } 74169691Skan 75169691Skan_GLIBCXX_END_NAMESPACE 76