summaryrefslogtreecommitdiff
path: root/core/safe_refcount.h
blob: e5dc854c230d08e586a1e906d894ffd2aff5ec28 (plain)
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
/*************************************************************************/
/*  safe_refcount.h                                                      */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#ifndef SAFE_REFCOUNT_H
#define SAFE_REFCOUNT_H

#include "os/mutex.h"
/* x86/x86_64 GCC */

#include "platform_config.h"


#ifdef NO_THREADS

struct SafeRefCount {

	int count;

public:
	
	// destroy() is called when weak_count_ drops to zero.
	
	bool ref() {  //true on success
	
		if (count==0)
			return false;
		count++;
		
		return true;
	}

	int refval() {  //true on success

		if (count==0)
			return 0;
		count++;
		return count;
	}

	bool unref() { // true if must be disposed of

		if (count>0)
			count--;
				
		return count==0;
	}
	
	long get() const { // nothrow
		
		return static_cast<int const volatile &>( count );
	}
	
	void init(int p_value=1) {
	
		count=p_value;
	};

};








#else

#if defined( PLATFORM_REFCOUNT )

#include "platform_refcount.h"


#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )

#define REFCOUNT_T volatile int
#define REFCOUNT_GET_T int const volatile&

static inline int atomic_conditional_increment( volatile int * pw ) {
	// int rv = *pw;
	// if( rv != 0 ) ++*pw;
	// return rv;
	
	int rv, tmp;
	
	__asm__
	(
		"movl %0, %%eax\n\t"
		"0:\n\t"
		"test %%eax, %%eax\n\t"
		"je 1f\n\t"
		"movl %%eax, %2\n\t"
		"incl %2\n\t"
		"lock\n\t"
		"cmpxchgl %2, %0\n\t"
		"jne 0b\n\t"
		"1:":
		"=m"( *pw ), "=&a"( rv ), "=&r"( tmp ): // outputs (%0, %1, %2)
		"m"( *pw ): // input (%3)
		"cc" // clobbers
	);
	
	return rv;
}

static inline int atomic_decrement( volatile int *pw) {
	
	// return --(*pw);
		
	unsigned char rv;
	
	__asm__
	(
		"lock\n\t"
		"decl %0\n\t"
		"setne %1": 
		"=m" (*pw), "=qm" (rv): 
		"m" (*pw):
		"memory"
	);
	return static_cast<int>(rv);
}

/* PowerPC32/64 GCC */

#elif ( defined( __GNUC__ ) ) && ( defined( __powerpc__ ) || defined( __ppc__ ) )

#define REFCOUNT_T int
#define REFCOUNT_GET_T int const volatile&

inline int atomic_conditional_increment( int * pw )
{
    // if( *pw != 0 ) ++*pw;
    // return *pw;

    int rv;

    __asm__
    (
        "0:\n\t"
        "lwarx %1, 0, %2\n\t"
        "cmpwi %1, 0\n\t"
        "beq 1f\n\t"
        "addi %1, %1, 1\n\t"
        "1:\n\t"
        "stwcx. %1, 0, %2\n\t"
        "bne- 0b":

        "=m"( *pw ), "=&b"( rv ):
        "r"( pw ), "m"( *pw ):
        "cc"
    );

    return rv;
}


inline int atomic_decrement( int * pw )
{
    // return --*pw;

    int rv;

    __asm__ __volatile__
    (
        "sync\n\t"
        "0:\n\t"
        "lwarx %1, 0, %2\n\t"
        "addi %1, %1, -1\n\t"
        "stwcx. %1, 0, %2\n\t"
        "bne- 0b\n\t"
        "isync":

        "=m"( *pw ), "=&b"( rv ):
        "r"( pw ), "m"( *pw ):
        "memory", "cc"
    );

    return rv;
}

/* CW ARM */

#elif defined( __GNUC__ ) && ( defined( __arm__ )  )

#define REFCOUNT_T int
#define REFCOUNT_GET_T int const volatile&

inline int atomic_conditional_increment(volatile int* v)
{
   int t;
   int tmp;

   __asm__ __volatile__(
			 "1:  ldrex   %0, [%2]        \n"
			 "    cmp     %0, #0      \n"
			 "    beq     2f          \n"
			 "    add     %0, %0, #1      \n"
			 "2: \n"
			 "    strex   %1, %0, [%2]    \n"
			 "    cmp     %1, #0          \n"
			 "    bne     1b              \n"

			 : "=&r" (t), "=&r" (tmp)
			 : "r" (v)
			 : "cc", "memory");

   return t;
}


inline int atomic_decrement(volatile int* v)
{
   int t;
   int tmp;

   __asm__ __volatile__(
			 "1:  ldrex   %0, [%2]        \n"
			 "    add     %0, %0, #-1      \n"
			 "    strex   %1, %0, [%2]    \n"
			 "    cmp     %1, #0          \n"
			 "    bne     1b              \n"

			 : "=&r" (t), "=&r" (tmp)
			 : "r" (v)
			 : "cc", "memory");

   return t;
}



/* CW PPC */

#elif ( defined( __MWERKS__ ) ) && defined( __POWERPC__ )

inline long atomic_conditional_increment( register long * pw )
{
    register int a;

	asm
	{
	loop:
	
	lwarx   a, 0, pw
	cmpwi   a, 0
	beq     store
	
	addi    a, a, 1
	
	store:
	
	stwcx.  a, 0, pw
	bne-    loop
    }

    return a;
}


inline long atomic_decrement( register long * pw )
{
    register int a;

    asm {
    
	sync
	
	loop:
	
	lwarx   a, 0, pw
	addi    a, a, -1
	stwcx.  a, 0, pw
	bne-    loop
	
	isync
    }

    return a;
}

/* Any Windows (MSVC) */

#elif defined( _MSC_VER )

// made functions to not pollute namespace..

#define REFCOUNT_T long
#define REFCOUNT_GET_T long const volatile&

long atomic_conditional_increment( register long * pw );
long atomic_decrement( register long * pw );

#if 0
#elif defined( __GNUC__ ) && defined( ARMV6_ENABLED)


#endif




#else

#error This platform cannot use safe refcount, compile with NO_THREADS or implement it.

#endif



struct SafeRefCount {

  REFCOUNT_T count;

public:
	
	// destroy() is called when weak_count_ drops to zero.
	
	bool ref() {  //true on success
	
		return atomic_conditional_increment( &count ) != 0;
	}

	int refval() {  //true on success

		return atomic_conditional_increment( &count );
	}

	bool unref() { // true if must be disposed of

		if( atomic_decrement ( &count ) == 0 ) {
			return true;
		}
		
		return false;
	}
	
	long get() const { // nothrow
		
		return static_cast<REFCOUNT_GET_T>( count );
	}
	
	void init(int p_value=1) {
	
		count=p_value;
	};

};



#endif // no thread safe

#endif