1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
#include "ace/Atomic_Op.h"
#include "ace/OS_NS_unistd.h"
#if !defined (__ACE_INLINE__)
#include "ace/Atomic_Op.inl"
#endif /* __ACE_INLINE__ */
#if defined (ACE_HAS_BUILTIN_ATOMIC_OP)
#if defined (ACE_INCLUDE_ATOMIC_OP_SPARC)
# include "ace/Atomic_Op_Sparc.h"
#endif /* ACE_INCLUDE_ATOMIC_OP_SPARC */
namespace {
#if defined (_MSC_VER)
// Disable "no return value" warning, as we will be putting
// the return values directly into the EAX register.
#pragma warning (push)
#pragma warning (disable: 4035)
#endif /* _MSC_VER */
long
single_cpu_increment (volatile long *value)
{
#if defined (ACE_HAS_INTEL_ASSEMBLY)
long tmp = 1;
unsigned long addr = reinterpret_cast<unsigned long> (value);
asm( "xadd %0, (%1)" : "+r"(tmp) : "r"(addr) );
return tmp + 1;
#elif !defined (ACE_HAS_SOLARIS_ATOMIC_LIB) && (defined (sun) || \
(defined (__SUNPRO_CC) && (defined (__i386) || defined (__x86_64))))
return ace_atomic_add_long (
reinterpret_cast<volatile unsigned long*> (value), 1);
#elif defined(__GNUC__) && defined(PPC)
long tmp;
asm("lwz %0,%1" : "=r" (tmp) : "m" (*value) );
asm("addi %0,%0,1" : "+r" (tmp) );
asm("stw %0,%1" : "+r" (tmp), "=m" (*value) );
return tmp;
#else /* ACE_HAS_INTEL_ASSEMBLY*/
ACE_UNUSED_ARG (value);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_INTEL_ASSEMBLY*/
}
long
single_cpu_decrement (volatile long *value)
{
#if defined (ACE_HAS_INTEL_ASSEMBLY)
long tmp = -1;
unsigned long addr = reinterpret_cast<unsigned long> (value);
asm( "xadd %0, (%1)" : "+r"(tmp) : "r"(addr) );
return tmp - 1;
#elif !defined (ACE_HAS_SOLARIS_ATOMIC_LIB) && (defined (sun) || \
(defined (__SUNPRO_CC) && (defined (__i386) || defined (__x86_64))))
return ace_atomic_add_long (
reinterpret_cast<volatile unsigned long*> (value), -1);
#elif defined(__GNUC__) && defined(PPC)
long tmp;
asm("lwz %0,%1" : "=r" (tmp) : "m" (*value) );
asm("addi %0,%0,-1" : "+r" (tmp) );
asm("stw %0,%1" : "+r" (tmp), "=m" (*value) );
return tmp;
#else /* ACE_HAS_INTEL_ASSEMBLY*/
ACE_UNUSED_ARG (value);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_INTEL_ASSEMBLY*/
}
long
single_cpu_exchange (volatile long *value, long rhs)
{
#if defined (ACE_HAS_INTEL_ASSEMBLY)
unsigned long addr = reinterpret_cast<unsigned long> (value);
asm( "xchg %0, (%1)" : "+r"(rhs) : "r"(addr) );
return rhs;
#elif !defined (ACE_HAS_SOLARIS_ATOMIC_LIB) && (defined (sun) || \
(defined (__SUNPRO_CC) && (defined (__i386) || defined (__x86_64))))
return ace_atomic_swap_long (
reinterpret_cast<volatile unsigned long*> (value), rhs);
#elif defined(__GNUC__) && defined(PPC)
long tmp;
asm("lwz %0,%1" : "=r" (tmp) : "m" (rhs) );
asm("stw %0,%1" : "+r" (tmp), "=m" (*value) );
return tmp;
#else /* ACE_HAS_INTEL_ASSEMBLY*/
ACE_UNUSED_ARG (value);
ACE_UNUSED_ARG (rhs);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_INTEL_ASSEMBLY*/
}
long
single_cpu_exchange_add (volatile long *value, long rhs)
{
#if defined (ACE_HAS_INTEL_ASSEMBLY)
unsigned long addr = reinterpret_cast<unsigned long> (value);
asm( "xadd %0, (%1)" : "+r"(rhs) : "r"(addr) );
return rhs;
#elif !defined (ACE_HAS_SOLARIS_ATOMIC_LIB) && (defined (sun) || \
(defined (__SUNPRO_CC) && (defined (__i386) || defined (__x86_64))))
return ace_atomic_swap_add_long (
reinterpret_cast<volatile unsigned long*> (value), rhs);
#elif defined(__GNUC__) && defined(PPC)
long tmp;
asm("add %0,%1,%2" : "=r" (tmp) : "r" (*value), "r" (rhs) );
asm("stw %0,%1" : "+r" (tmp), "=m" (*value) );
return tmp;
#elif defined (WIN32) && !defined (ACE_HAS_INTERLOCKED_EXCHANGEADD)
# if defined (_MSC_VER)
__asm
{
mov eax, rhs
mov edx, value
xadd [edx], eax
}
// Return value is already in EAX register.
# elif defined (__BORLANDC__)
_EAX = rhs;
_EDX = reinterpret_cast<unsigned long> (value);
__emit__(0x0F, 0xC1, 0x02); // xadd [edx], eax
// Return value is already in EAX register.
# else /* _MSC_VER */
ACE_UNUSED_ARG (value);
ACE_UNUSED_ARG (rhs);
ACE_NOTSUP_RETURN (-1);
# endif /* _MSC_VER */
#else /* ACE_HAS_INTEL_ASSEMBLY*/
ACE_UNUSED_ARG (value);
ACE_UNUSED_ARG (rhs);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_INTEL_ASSEMBLY*/
}
long
multi_cpu_increment (volatile long *value)
{
#if defined (ACE_HAS_INTEL_ASSEMBLY)
long tmp = 1;
unsigned long addr = reinterpret_cast<unsigned long> (value);
asm( "lock ; xadd %0, (%1)" : "+r"(tmp) : "r"(addr) );
return tmp + 1;
#elif !defined (ACE_HAS_SOLARIS_ATOMIC_LIB) && (defined (sun) || \
(defined (__SUNPRO_CC) && (defined (__i386) || defined (__x86_64))))
return ace_atomic_add_long (
reinterpret_cast<volatile unsigned long*> (value), 1);
#else /* ACE_HAS_INTEL_ASSEMBLY*/
ACE_UNUSED_ARG (value);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_INTEL_ASSEMBLY*/
}
long
multi_cpu_decrement (volatile long *value)
{
#if defined (ACE_HAS_INTEL_ASSEMBLY)
long tmp = -1;
unsigned long addr = reinterpret_cast<unsigned long> (value);
asm( "lock ; xadd %0, (%1)" : "+r"(tmp) : "r"(addr) );
return tmp - 1;
#elif !defined (ACE_HAS_SOLARIS_ATOMIC_LIB) && (defined (sun) || \
(defined (__SUNPRO_CC) && (defined (__i386) || defined (__x86_64))))
return ace_atomic_add_long (
reinterpret_cast<volatile unsigned long*> (value), -1);
#else /* ACE_HAS_INTEL_ASSEMBLY*/
ACE_UNUSED_ARG (value);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_INTEL_ASSEMBLY*/
}
long
multi_cpu_exchange (volatile long *value, long rhs)
{
#if defined (ACE_HAS_INTEL_ASSEMBLY)
unsigned long addr = reinterpret_cast<unsigned long> (value);
// The XCHG instruction automatically follows LOCK semantics
asm( "xchg %0, (%1)" : "+r"(rhs) : "r"(addr) );
return rhs;
#elif !defined (ACE_HAS_SOLARIS_ATOMIC_LIB) && (defined (sun) || \
(defined (__SUNPRO_CC) && (defined (__i386) || defined (__x86_64))))
return ace_atomic_swap_long (
reinterpret_cast<volatile unsigned long*> (value), rhs);
#else /* ACE_HAS_INTEL_ASSEMBLY*/
ACE_UNUSED_ARG (value);
ACE_UNUSED_ARG (rhs);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_INTEL_ASSEMBLY*/
}
long
multi_cpu_exchange_add (volatile long *value, long rhs)
{
#if defined (ACE_HAS_INTEL_ASSEMBLY)
unsigned long addr = reinterpret_cast<unsigned long> (value);
asm( "lock ; xadd %0, (%1)" : "+r"(rhs) : "r"(addr) );
return rhs;
#elif !defined (ACE_HAS_SOLARIS_ATOMIC_LIB) && (defined (sun) || \
(defined (__SUNPRO_CC) && (defined (__i386) || defined (__x86_64))))
return ace_atomic_swap_add_long (
reinterpret_cast<volatile unsigned long*> (value), rhs);
#elif defined (WIN32) && !defined (ACE_HAS_INTERLOCKED_EXCHANGEADD)
# if defined (_MSC_VER)
__asm
{
mov eax, rhs
mov edx, value
lock xadd [edx], eax
}
// Return value is already in EAX register.
# elif defined (__BORLANDC__)
_EAX = rhs;
_EDX = reinterpret_cast<unsigned long> (value);
__emit__(0xF0, 0x0F, 0xC1, 0x02); // lock xadd [edx], eax
// Return value is already in EAX register.
# else /* _MSC_VER */
ACE_UNUSED_ARG (value);
ACE_UNUSED_ARG (rhs);
ACE_NOTSUP_RETURN (-1);
# endif /* _MSC_VER */
#else /* ACE_HAS_INTEL_ASSEMBLY*/
ACE_UNUSED_ARG (value);
ACE_UNUSED_ARG (rhs);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_INTEL_ASSEMBLY*/
}
#if defined (_MSC_VER)
#pragma warning (pop)
#endif /* _MSC_VER */
} // end namespace
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
long (*ACE_Atomic_Op<ACE_Thread_Mutex, long>::increment_fn_) (volatile long *) = multi_cpu_increment;
long (*ACE_Atomic_Op<ACE_Thread_Mutex, long>::decrement_fn_) (volatile long *) = multi_cpu_decrement;
long (*ACE_Atomic_Op<ACE_Thread_Mutex, long>::exchange_fn_) (volatile long *, long) = multi_cpu_exchange;
long (*ACE_Atomic_Op<ACE_Thread_Mutex, long>::exchange_add_fn_) (volatile long *, long) = multi_cpu_exchange_add;
void
ACE_Atomic_Op<ACE_Thread_Mutex, long>::init_functions (void)
{
if (ACE_OS::num_processors () == 1)
{
increment_fn_ = single_cpu_increment;
decrement_fn_ = single_cpu_decrement;
exchange_fn_ = single_cpu_exchange;
exchange_add_fn_ = single_cpu_exchange_add;
}
else
{
increment_fn_ = multi_cpu_increment;
decrement_fn_ = multi_cpu_decrement;
exchange_fn_ = multi_cpu_exchange;
exchange_add_fn_ = multi_cpu_exchange_add;
}
}
void
ACE_Atomic_Op<ACE_Thread_Mutex, long>::dump (void) const
{
#if defined (ACE_HAS_DUMP)
ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}
long (*ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long>::increment_fn_) (volatile long *) = multi_cpu_increment;
long (*ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long>::decrement_fn_) (volatile long *) = multi_cpu_decrement;
long (*ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long>::exchange_fn_) (volatile long *, long) = multi_cpu_exchange;
long (*ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long>::exchange_add_fn_) (volatile long *, long) = multi_cpu_exchange_add;
void
ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long>::init_functions (void)
{
if (ACE_OS::num_processors () == 1)
{
increment_fn_ = single_cpu_increment;
decrement_fn_ = single_cpu_decrement;
exchange_fn_ = single_cpu_exchange;
exchange_add_fn_ = single_cpu_exchange_add;
}
else
{
increment_fn_ = multi_cpu_increment;
decrement_fn_ = multi_cpu_decrement;
exchange_fn_ = multi_cpu_exchange;
exchange_add_fn_ = multi_cpu_exchange_add;
}
}
void
ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long>::dump (void) const
{
#if defined (ACE_HAS_DUMP)
ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}
ACE_END_VERSIONED_NAMESPACE_DECL
#endif /* ACE_HAS_BUILTIN_ATOMIC_OP */
|