summaryrefslogtreecommitdiffstats
path: root/3rdparty/openpgm-svn-r1085/pgm/include/pgm/atomic.h
blob: 15a52721f147a2e828e442ebe24e0844219cb75f (plain) (blame)
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
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 * 
 * 32-bit atomic operations.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __PGM_ATOMIC_H__
#define __PGM_ATOMIC_H__

#ifdef sun
#	include <atomic.h>
#endif
#include <pgm/types.h>

static inline
uint32_t
pgm_atomic_exchange_and_add32 (
	volatile uint32_t*	atomic,
	const uint32_t		val
	)
{
#if defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
	uint32_t result;
	asm volatile (	"lock\n\t"
			"xaddl %0, %1"
		      : "=r" (result), "=m" (*atomic)
		      : "0" (val), "m" (*atomic)
		      : "memory", "cc"  );
	return result;
#elif defined( __SUNPRO_C ) && (defined( __i386 ) || defined( __amd64 ))
	uint32_t result = val;
	asm volatile (	"lock\n\t"
			"xaddl %0, %1"
		      :: "r" (result), "m" (*atomic)  );
	return result;
#elif defined( sun )
	const uint32_t nv = atomic_add_32_nv (atomic, (int32_t)val);
	return nv - val;
#elif defined( __GNUC__ ) && ( __GNUC__ * 100 + __GNUC_MINOR__ >= 401 )
	return __sync_fetch_and_add (atomic, val);
#elif defined( _WIN32 )
	return InterlockedExchangeAdd ((volatile LONG*)atomic, val);
#else
#	error "No supported atomic operations for this platform."
#endif
}

static inline
void
pgm_atomic_add32 (
	volatile uint32_t*	atomic,
	const uint32_t		val
	)
{
#if defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
	asm volatile (	"lock\n\t"
			"addl %1, %0"
		      : "=m" (*atomic)
		      : "ir" (val), "m" (*atomic)
		      : "memory", "cc"  );
#elif defined( __SUNPRO_C ) && (defined( __i386 ) || defined( __amd64 ))
	asm volatile (	"lock\n\t"
			"addl %1, %0"
		      :: "r" (val), "m" (*atomic)  );
#elif defined( sun )
	atomic_add_32 (atomic, (int32_t)val);
#elif defined( __GNUC__ ) && ( __GNUC__ * 100 + __GNUC_MINOR__ >= 401 )
	__sync_fetch_and_add (atomic, val);
#elif defined( _WIN32 )
	InterlockedExchangeAdd ((volatile LONG*)atomic, val);
#endif
}

static inline
void
pgm_atomic_inc32 (
	volatile uint32_t*	atomic
	)
{
#if (defined( __GNUC__ ) && (defined( __i386__ ) || defined( __x86_64__ ))) || (defined( __SUNPRO_C ) && (defined( __i386 ) || defined( __amd64 )))
	pgm_atomic_add32 (atomic, 1);
#elif defined( sun )
	atomic_inc_32 (atomic);
#elif defined( __GNUC__ ) && ( __GNUC__ * 100 + __GNUC_MINOR__ >= 401 )
	pgm_atomic_add32 (atomic, 1);
#elif defined( _WIN32 )
	InterlockedIncrement ((volatile LONG*)atomic);
#endif
}

static inline
void
pgm_atomic_dec32 (
	volatile uint32_t*	atomic
	)
{
#if (defined( __GNUC__ ) && (defined( __i386__ ) || defined( __x86_64__ ))) || (defined( __SUNPRO_C ) && (defined( __i386 ) || defined( __amd64 )))
	pgm_atomic_add32 (atomic, (uint32_t)-1);
#elif defined( sun )
	atomic_dec_32 (atomic);
#elif defined( __GNUC__ ) && ( __GNUC__ * 100 + __GNUC_MINOR__ >= 401 )
	pgm_atomic_add32 (atomic, (uint32_t)-1);
#elif defined( _WIN32 )
	InterlockedDecrement ((volatile LONG*)atomic);
#endif
}

static inline
uint32_t
pgm_atomic_read32 (
	const volatile uint32_t* atomic
	)
{
	return *atomic;
}

static inline
void
pgm_atomic_write32 (
	volatile uint32_t*	atomic,
	const uint32_t		val
	)
{
	*atomic = val;
}

#endif /* __PGM_ATOMIC_H__ */