-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref_i.h
383 lines (311 loc) · 10.3 KB
/
ref_i.h
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
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is COID/comm module.
*
* The Initial Developer of the Original Code is
* Ladislav Hrabcak
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef __COMM_REF_I_H__
#define __COMM_REF_I_H__
#include "commassert.h"
#include "ref_base.h"
#include "sync/mutex.h"
#include "hash/hashfunc.h"
#include "binstream/binstreambuf.h"
#include "metastream/metastream.h"
#include "atomic/pool_base.h"
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
template<class T> class irefw;
struct create_lock {};
template<class T>
class iref
{
friend class irefw<T>;
private:
T* _p;
typedef iref<T> iref_t;
typedef coid::pool< coid::policy_intrusive_pooled<T>* > pool_type_t;
public:
typedef T base_t;
iref() : _p(0) {}
iref(nullptr_t) : _p(0) {}
iref(const iref_t& r) : _p(r.add_refcount()) {}
iref(iref_t&& r) noexcept : _p(0) {
takeover(std::forward<iref_t>(r));
}
///Constructor from pointer, artificially removed priority to resolve ambiguity
template<class K>
iref(K* p, typename std::remove_const<K>::type* = 0) : _p(p) {
if (_p)
reinterpret_cast<policy_intrusive_base*>(_p)->add_refcount();
}
///Copy constructor from convertible type
#if SYSTYPE_MSVC && _MSC_VER >= 1800
template<class T2, class = typename std::enable_if<std::is_convertible<T2*, T*>::value>::type>
#else
template<class T2>
#endif
iref(const iref<T2>& r) {
_p = r.add_refcount();
}
///Move constructor from convertible type
#if SYSTYPE_MSVC && _MSC_VER >= 1800
template<class T2, class = typename std::enable_if<std::is_convertible<T2*, T*>::value>::type>
#else
template<class T2>
#endif
iref(iref<T2>&& r) : _p(0) {
takeover(std::forward<iref_t>(r));
}
///Increment refcount
T* add_refcount() const {
if (_p)
reinterpret_cast<policy_intrusive_base*>(_p)->add_refcount();
return _p;
}
///Assign refcounted object if its refcount is non-zero
/// @return assigned object or null
/// @note keeps previous object if assignment fails
T* add_refcount(T* p) {
if (p && p->can_add_refcount()) {
release();
return _p = p;
}
return 0;
}
//
explicit iref(const create_me&)
: _p(new T())
{
_p->add_refcount();
}
// special constructor from default policy
explicit iref(const create_pooled&)
: _p(coid::policy_intrusive_pooled<T>::create())
{}
~iref() { release(); }
void release() {
if (_p)
reinterpret_cast<policy_intrusive_base*>(_p)->release_refcount((void**)&_p);
_p = 0;
}
T* create(T* const p)
{
static_assert(std::is_base_of_v<policy_intrusive_base, T>, "type must be derived from policy_intrusive_base");
DASSERT_RET(p != _p, _p);
p->add_refcount();
release();
_p = p;
return _p;
}
bool create_lock(T* p) {
release();
if (p && p->add_refcount_lock()) {
_p = p;
return true;
}
else
return false;
}
T* create_pooled() {
T* p = coid::policy_intrusive_pooled<T>::create();
DASSERT_RET(p != _p, _p);
release();
_p = p;
return _p;
}
T* create_pooled(pool_type_t* po) {
T* p = coid::policy_intrusive_pooled<T>::create(po);
DASSERT_RET(p != _p, _p);
release();
_p = p;
return _p;
}
///Assign if empty
/*bool assign_safe(const iref_t& r) {
static coid::comm_mutex _mux(500, false);
_mux.lock();
//assign only if nobody assigned before us
bool succ = !_p;
if (succ)
_p = r.add_refcount();
_mux.unlock();
return succ;
}*/
const iref_t& operator = (const iref_t& r) {
T* p = r.add_refcount();
release();
_p = p;
return *this;
}
const iref_t& operator = (iref_t&& r) noexcept {
takeover(std::forward<iref_t>(r));
return *this;
}
T* get() const { return _p; }
T& operator*() const { DASSERT(_p != 0); return *_p; }
T* operator->() const { DASSERT(_p != 0); return _p; }
void swap(iref_t& r) {
std::swap(_p, r._p);
}
friend void swap(iref_t& a, iref_t& b) {
std::swap(a._p, b._p);
}
bool is_empty() const { return _p == 0; }
bool is_set() const { return _p != 0; }
///Discard without decrementing refcount
void forget() { _p = 0; }
template <class T2>
void takeover(iref<T2>&& p) {
if (_p == static_cast<T*>(p.get())) {
p.release();
return;
}
release();
_p = static_cast<T*>(p.get());
p.forget();
}
/// @brief Upcast to iref of derived type using static_cast
template <class T2>
T2* cast() const {
return static_cast<T2*>(_p);
}
coid::int32 refcount() const { return _p ? _p->refcount() : 0; }
///Automatic cast to unconvertible bool for checking via if
explicit operator bool () const { return _p != 0; }
friend bool operator == (const iref<T>& a, const iref<T>& b) {
return a._p == b._p;
}
friend bool operator != (const iref<T>& a, const iref<T>& b) {
return !operator==(a, b);
}
friend bool operator < (const iref<T>& a, const iref<T>& b) {
return a._p < b._p;
}
friend coid::binstream& operator << (coid::binstream& bin, const iref_t& s) {
return bin << *s.get();
}
friend coid::binstream& operator >> (coid::binstream& bin, iref_t& s) {
s.create(new T); return bin >> *s.get();
}
friend coid::metastream& operator || (coid::metastream& m, iref_t& s)
{
if (m.stream_writing())
m.write_optional(s.get());
else if (m.stream_reading())
s.create(m.read_optional<T>());
else {
if (m.meta_decl_raw_pointer(
typeid(s).name(),
false,
0,
[](const void* a) -> const void* { return static_cast<const iref_t*>(a)->_p; },
[](const void* a) -> uints { return static_cast<const iref_t*>(a)->is_empty() ? 0 : 1; },
[](void* a, uints& i) -> void* { return static_cast<iref*>(a)->_p; },
[](const void* a, uints& i) -> const void* { return static_cast<const iref_t*>(a)->_p; }
))
m || *s._p;
}
return m;
}
};
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
template<class T>
class irefw
{
protected:
T* _p;
volatile coid::uint32* _weaks;
public:
irefw() : _p(0), _weaks(0) {}
irefw(const iref<T>& o)
: _p(o._p)
, _weaks(o._p ? _p->add_weak_copy() : 0)
{}
irefw(const irefw<T>& o)
: _p(o._p)
, _weaks(o._p ? T::add_weak_copy(o._weaks) : 0)
{}
~irefw() { release(); }
void release() {
if (_p) {
const coid::uint32 weaks =
T::counter_t::dec(_weaks) & ~0x8000000;
if (weaks == 0)
delete _weaks;
_p = 0;
_weaks = 0;
}
}
bool lock(iref<T>& newref) {
if (_p && newref._p != _p)
for (;;) {
coid::int32 tmp = *_weaks;
if (tmp & 0x80000000) {
if (tmp == T::counter_t::add(_weaks, 0))
return false;
}
else {
if (!newref.create_lock(_p) || tmp != T::counter_t::add(_weaks, 0))
newref.forget();
else
return true;
}
}
else
return false;
}
};
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
namespace coid {
template<typename T> struct hasher<iref<T>> {
typedef iref<T> key_type;
uint operator()(const key_type& x) const { return (uint)x.get(); }
};
template <class T>
struct threadcached<iref<T>>
{
typedef iref<T> storage_type;
operator iref<T>& () { return _val; }
iref<T>& operator* () { return _val; }
iref<T>* operator& () { return &_val; }
threadcached& operator = (iref<T>&& val) {
_val = std::move(val);
return *this;
}
threadcached& operator = (const iref<T>& val) {
_val = val;
return *this;
}
T* get() { return _val.get(); }
private:
iref<T> _val;
};
} //namespace coid
#endif // __COMM_REF_I_H__