1169691Skan// Support for atomic operations -*- C++ -*-
2169691Skan
3169691Skan// Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
4169691Skan//
5169691Skan// This file is part of the GNU ISO C++ Library.  This library is free
6169691Skan// software; you can redistribute it and/or modify it under the
7169691Skan// terms of the GNU General Public License as published by the
8169691Skan// Free Software Foundation; either version 2, or (at your option)
9169691Skan// any later version.
10169691Skan
11169691Skan// This library is distributed in the hope that it will be useful,
12169691Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
13169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14169691Skan// GNU General Public License for more details.
15169691Skan
16169691Skan// You should have received a copy of the GNU General Public License along
17169691Skan// 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,
19169691Skan// USA.
20169691Skan
21169691Skan// As a special exception, you may use this file as part of a free software
22169691Skan// library without restriction.  Specifically, if other files instantiate
23169691Skan// templates or use macros or inline functions from this file, or you compile
24169691Skan// this file and link it with other files to produce an executable, this
25169691Skan// file does not by itself cause the resulting executable to be covered by
26169691Skan// the GNU General Public License.  This exception does not however
27169691Skan// invalidate any other reasons why the executable file might be covered by
28169691Skan// the GNU General Public License.
29169691Skan
30169691Skan/** @file atomicity.h
31169691Skan *  This is an internal header file, included by other library headers.
32169691Skan *  You should not attempt to use it directly.
33169691Skan */
34169691Skan
35169691Skan#ifndef _GLIBCXX_ATOMICITY_H
36169691Skan#define _GLIBCXX_ATOMICITY_H	1
37169691Skan
38169691Skan#include <bits/c++config.h>
39169691Skan#include <bits/gthr.h>
40169691Skan#include <bits/atomic_word.h>
41169691Skan
42169691Skan_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
43169691Skan
44169691Skan  // Functions for portable atomic access.
45228780Spfg  // To abstract locking primitives across all thread policies, use:
46169691Skan  // __exchange_and_add_dispatch
47169691Skan  // __atomic_add_dispatch
48169691Skan#ifdef _GLIBCXX_ATOMIC_BUILTINS
49169691Skan  static inline _Atomic_word
50169691Skan  __exchange_and_add(volatile _Atomic_word* __mem, int __val)
51169691Skan  { return __sync_fetch_and_add(__mem, __val); }
52169691Skan
53169691Skan  static inline void
54169691Skan  __atomic_add(volatile _Atomic_word* __mem, int __val)
55169691Skan  { __sync_fetch_and_add(__mem, __val); }
56169691Skan#else
57169691Skan  _Atomic_word
58169691Skan  __attribute__ ((__unused__))
59169691Skan  __exchange_and_add(volatile _Atomic_word*, int);
60169691Skan
61169691Skan  void
62169691Skan  __attribute__ ((__unused__))
63169691Skan  __atomic_add(volatile _Atomic_word*, int);
64169691Skan#endif
65169691Skan
66169691Skan  static inline _Atomic_word
67169691Skan  __exchange_and_add_single(_Atomic_word* __mem, int __val)
68169691Skan  {
69169691Skan    _Atomic_word __result = *__mem;
70169691Skan    *__mem += __val;
71169691Skan    return __result;
72169691Skan  }
73169691Skan
74169691Skan  static inline void
75169691Skan  __atomic_add_single(_Atomic_word* __mem, int __val)
76169691Skan  { *__mem += __val; }
77169691Skan
78169691Skan  static inline _Atomic_word
79169691Skan  __attribute__ ((__unused__))
80169691Skan  __exchange_and_add_dispatch(_Atomic_word* __mem, int __val)
81169691Skan  {
82169691Skan#ifdef __GTHREADS
83169691Skan    if (__gthread_active_p())
84169691Skan      return __exchange_and_add(__mem, __val);
85169691Skan    else
86169691Skan      return __exchange_and_add_single(__mem, __val);
87169691Skan#else
88169691Skan    return __exchange_and_add_single(__mem, __val);
89169691Skan#endif
90169691Skan  }
91169691Skan
92169691Skan  static inline void
93169691Skan  __attribute__ ((__unused__))
94169691Skan  __atomic_add_dispatch(_Atomic_word* __mem, int __val)
95169691Skan  {
96169691Skan#ifdef __GTHREADS
97169691Skan    if (__gthread_active_p())
98169691Skan      __atomic_add(__mem, __val);
99169691Skan    else
100169691Skan      __atomic_add_single(__mem, __val);
101169691Skan#else
102169691Skan    __atomic_add_single(__mem, __val);
103169691Skan#endif
104169691Skan  }
105169691Skan
106169691Skan_GLIBCXX_END_NAMESPACE
107169691Skan
108169691Skan// Even if the CPU doesn't need a memory barrier, we need to ensure
109169691Skan// that the compiler doesn't reorder memory accesses across the
110169691Skan// barriers.
111169691Skan#ifndef _GLIBCXX_READ_MEM_BARRIER
112169691Skan#define _GLIBCXX_READ_MEM_BARRIER __asm __volatile ("":::"memory")
113169691Skan#endif
114169691Skan#ifndef _GLIBCXX_WRITE_MEM_BARRIER
115169691Skan#define _GLIBCXX_WRITE_MEM_BARRIER __asm __volatile ("":::"memory")
116169691Skan#endif
117169691Skan
118169691Skan#endif
119