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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
// **********************************************************************
//
// Copyright (c) 2003 - 2004
// ZeroC, Inc.
// North Palm Beach, FL, USA
//
// All Rights Reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#ifndef ICE_UTIL_SHARED_H
#define ICE_UTIL_SHARED_H
#include <IceUtil/Config.h>
//
// Here are the sparc64+linux atomic functions, if you wish them.
//
#if defined(__linux) && defined(__sparc__) && defined(USE_SPARC_ASM) && !defined(ICE_USE_MUTEX_SHARED)
# define ICE_HAS_ATOMIC_FUNCTIONS
extern "C"
{
#include <asm-sparc64/atomic.h>
int __atomic_exchange_and_add(atomic_t *v, int i);
}
#define ice_atomic_t atomic_t
#define ice_atomic_set(v,i) atomic_set(v,i)
#define ice_atomic_inc(v) atomic_inc(v)
#define ice_atomic_dec_and_test(v) atomic_dec_and_test(v)
#define ice_atomic_exchange_add(i,v) (__atomic_exchange_and_add(v,i))
#elif defined(ICE_USE_MUTEX_SHARED)
# include <IceUtil/Mutex.h>
#elif (defined(__linux) || defined(__FreeBSD__)) && defined(__i386) && !defined(__ICC)
# define ICE_HAS_ATOMIC_FUNCTIONS
// __ICC: The inline assembler causes problems with shared libraries.
//
// Linux only. Unfortunately, asm/atomic.h builds non-SMP safe code
// with non-SMP kernels. This means that executables compiled with a
// non-SMP kernel would fail randomly due to concurrency errors with
// reference counting on SMP hosts. Therefore the relevent pieces of
// atomic.h are more-or-less duplicated.
//
/*
* Make sure gcc doesn't try to be clever and move things around
* on us. We need to use _exactly_ the address the user gave us,
* not some alias that contains the same information.
*/
struct ice_atomic_t
{
volatile int counter;
};
/*
* ice_atomic_set - set ice_atomic variable
* @v: pointer of type ice_atomic_t
* @i: required value
*
* Atomically sets the value of @v to @i. Note that the guaranteed
* useful range of an ice_atomic_t is only 24 bits.
*/
inline void ice_atomic_set(ice_atomic_t* v, int i)
{
v->counter = i;
}
/*
* ice_atomic_inc - increment ice_atomic variable
* @v: pointer of type ice_atomic_t
*
* Atomically increments @v by 1. Note that the guaranteed useful
* range of an ice_atomic_t is only 24 bits.
*
* Inlined because this operation is performance critical.
*/
inline void ice_atomic_inc(ice_atomic_t *v)
{
__asm__ __volatile__(
"lock ; incl %0"
:"=m" (v->counter)
:"m" (v->counter));
}
/**
* ice_atomic_dec_and_test - decrement and test
* @v: pointer of type ice_atomic_t
*
* Atomically decrements @v by 1 and returns true if the result is 0,
* or false for all other cases. Note that the guaranteed useful
* range of an ice_atomic_t is only 24 bits.
*
* Inlined because this operation is performance critical.
*/
inline int ice_atomic_dec_and_test(ice_atomic_t *v)
{
unsigned char c;
__asm__ __volatile__(
"lock ; decl %0; sete %1"
:"=m" (v->counter), "=qm" (c)
:"m" (v->counter) : "memory");
return c != 0;
}
/**
* ice_atomic_exchange_add - same as InterlockedExchangeAdd. This
* didn't come from atomic.h (the code was derived from similar code
* in /usr/include/asm/rwsem.h)
*
* Inlined because this operation is performance critical.
*/
inline int ice_atomic_exchange_add(int i, ice_atomic_t* v)
{
int tmp = i;
__asm__ __volatile__(
"lock ; xadd %0,(%2)"
:"+r"(tmp), "=m"(v->counter)
:"r"(v), "m"(v->counter)
: "memory");
return tmp + i;
}
#elif defined(_WIN32)
// Nothing to include
#else
// Use a simple mutex
# include <IceUtil/Mutex.h>
#endif
//
// Base classes for reference counted types. The IceUtil::Handle
// template can be used for smart pointers to types derived from these
// bases.
//
// IceUtil::SimpleShared
// =====================
//
// A non thread-safe base class for reference-counted types.
//
// IceUtil::Shared
// ===============
//
// A thread-safe base class for reference-counted types.
//
namespace IceUtil
{
//
// TODO: Not all operations in this class are performance critical,
// thus not all of them should be inlined.
//
class SimpleShared : public noncopyable
{
public:
SimpleShared() :
_ref(0),
_noDelete(false)
{
}
virtual ~SimpleShared()
{
}
void __incRef()
{
assert(_ref >= 0);
++_ref;
}
void __decRef()
{
assert(_ref > 0);
if(--_ref == 0)
{
if(!_noDelete)
{
_noDelete = true;
delete this;
}
}
}
int __getRef() const
{
return _ref;
}
void __setNoDelete(bool b)
{
_noDelete = b;
}
private:
int _ref;
bool _noDelete;
};
class Shared : public noncopyable
{
public:
Shared();
virtual ~Shared();
void __incRef();
void __decRef();
int __getRef() const;
void __setNoDelete(bool);
private:
#if defined(_WIN32)
LONG _ref;
#elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
ice_atomic_t _ref;
#else
int _ref;
Mutex _mutex;
#endif
bool _noDelete;
};
//
// TODO: Not all operations below are performance critical, thus not
// all of them should be inlined.
//
#if defined(_WIN32)
inline
Shared::Shared() :
_ref(0),
_noDelete(false)
{
}
inline
Shared::~Shared()
{
}
inline void
Shared::__incRef()
{
assert(InterlockedExchangeAdd(&_ref, 0) >= 0);
InterlockedIncrement(&_ref);
}
inline void
Shared::__decRef()
{
assert(InterlockedExchangeAdd(&_ref, 0) > 0);
if(InterlockedDecrement(&_ref) == 0 && !_noDelete)
{
_noDelete = true;
delete this;
}
}
inline int
Shared::__getRef() const
{
return InterlockedExchangeAdd(const_cast<LONG*>(&_ref), 0);
}
inline void
Shared::__setNoDelete(bool b)
{
_noDelete = b;
}
#elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
inline
Shared::Shared() :
_noDelete(false)
{
ice_atomic_set(&_ref, 0);
}
inline
Shared::~Shared()
{
}
inline void
Shared::__incRef()
{
assert(ice_atomic_exchange_add(0, &_ref) >= 0);
ice_atomic_inc(&_ref);
}
inline void
Shared::__decRef()
{
assert(ice_atomic_exchange_add(0, &_ref) > 0);
if(ice_atomic_dec_and_test(&_ref) && !_noDelete)
{
_noDelete = true;
delete this;
}
}
inline int
Shared::__getRef() const
{
return ice_atomic_exchange_add(0, const_cast<ice_atomic_t*>(&_ref));
}
inline void
Shared::__setNoDelete(bool b)
{
_noDelete = b;
}
#else
inline
Shared::Shared() :
_ref(0),
_noDelete(false)
{
}
inline
Shared::~Shared()
{
}
inline void
Shared::__incRef()
{
_mutex.lock();
assert(_ref >= 0);
++_ref;
_mutex.unlock();
}
inline void
Shared::__decRef()
{
_mutex.lock();
bool doDelete = false;
assert(_ref > 0);
if(--_ref == 0)
{
doDelete = !_noDelete;
_noDelete = true;
}
_mutex.unlock();
if(doDelete)
{
delete this;
}
}
inline int
Shared::__getRef() const
{
_mutex.lock();
int ref = _ref;
_mutex.unlock();
return ref;
}
inline void
Shared::__setNoDelete(bool b)
{
_mutex.lock();
_noDelete = b;
_mutex.unlock();
}
#endif
}
#endif
|