/[svn]/linuxsampler/trunk/src/common/lsatomic.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/common/lsatomic.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1870 - (show annotations) (download) (as text)
Sun Mar 22 16:53:01 2009 UTC (11 years, 8 months ago) by persson
File MIME type: text/x-c++hdr
File size: 4681 byte(s)
* bugfix: low-level lsatomic.h load function was broken on 64-bit
  PowerPC, which probably could cause crashes on that platform
* minor cleanups and optimization in lsatomic.h

1 /***************************************************************************
2 * *
3 * Copyright (C) 2008 Andreas Persson *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
18 * MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #ifndef LSATOMIC_H
22 #define LSATOMIC_H
23
24 /*
25 * Implementation of a small subset of the C++0x atomic operations
26 * (cstdatomic).
27 *
28 * The supported operations are:
29 *
30 * - fences (acquire, release and full)
31 *
32 * - load and store of atomic<int> with relaxed, acquire/release or
33 * seq_cst memory ordering
34 *
35 * The supported architectures are x86 and powerpc.
36 */
37 namespace LinuxSampler {
38 enum memory_order {
39 memory_order_relaxed, memory_order_acquire,
40 memory_order_release, memory_order_seq_cst
41 };
42
43 inline void atomic_thread_fence(memory_order order) {
44 switch (order) {
45 case memory_order_relaxed:
46 break;
47
48 case memory_order_acquire:
49 case memory_order_release:
50 #ifdef _ARCH_PPC64
51 asm volatile("lwsync" : : : "memory");
52 #elif defined(_ARCH_PPC)
53 asm volatile("sync" : : : "memory");
54 #else
55 asm volatile("" : : : "memory");
56 #endif
57 break;
58
59 case memory_order_seq_cst:
60 #ifdef _ARCH_PPC
61 asm volatile("sync" : : : "memory");
62 #elif defined(__i386__)
63 asm volatile("lock; addl $0,0(%%esp)" : : : "memory");
64 #elif defined(__x86_64__)
65 asm volatile("mfence" : : : "memory");
66 #else
67 asm volatile("" : : : "memory");
68 #endif
69 break;
70 }
71 }
72
73 template<typename T> class atomic;
74 template<> class atomic<int> { // int is the only implemented type
75 public:
76 atomic() { }
77 explicit atomic(int m) : f(m) { }
78 int load(memory_order order = memory_order_seq_cst) const volatile {
79 int m;
80 switch (order) {
81 case memory_order_relaxed:
82 m = f;
83 break;
84
85 case memory_order_seq_cst:
86 case memory_order_release: // (invalid)
87 atomic_thread_fence(memory_order_seq_cst);
88 // fall-through
89
90 case memory_order_acquire:
91 #ifdef _ARCH_PPC
92 // PPC load-acquire: artificial dependency + isync
93 asm volatile(
94 "lwz%U1%X1 %0,%1\n\t"
95 "cmpw %0,%0\n\t"
96 "bne- 1f\n\t"
97 "1: isync"
98 : "=r" (m)
99 : "m" (f)
100 : "memory", "cr0");
101 #else
102 m = f;
103 asm volatile("" : : : "memory");
104 #endif
105 break;
106 }
107 return m;
108 }
109
110 void store(int m, memory_order order = memory_order_seq_cst) volatile {
111 switch (order) {
112 case memory_order_relaxed:
113 f = m;
114 break;
115
116 case memory_order_release:
117 atomic_thread_fence(memory_order_release);
118 f = m;
119 break;
120
121 case memory_order_seq_cst:
122 case memory_order_acquire: // (invalid)
123 atomic_thread_fence(memory_order_release);
124 f = m;
125 atomic_thread_fence(memory_order_seq_cst);
126 break;
127 }
128 }
129 private:
130 int f;
131 atomic(const atomic&); // not allowed
132 atomic& operator=(const atomic&); // not allowed
133 };
134 }
135 #endif

  ViewVC Help
Powered by ViewVC