summaryrefslogtreecommitdiffstats
path: root/runtime/atomic.h
blob: ea3be37a6c04cad652fef6937413353331a48ab4 (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
/* This header supplies atomic operations. So far, we rely on GCC's
 * atomic builtins. During configure, we check if atomic operatons are
 * available. If they are not, I am making the necessary provisioning to live without them if
 * they are not available. Please note that you should only use the macros
 * here if you think you can actually live WITHOUT an explicit atomic operation,
 * because in the non-presence of them, we simply do it without atomicitiy.
 * Which, for word-aligned data types, usually (but only usually!) should work.
 *
 * We are using the functions described in
 * http:/gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html
 *
 * THESE MACROS MUST ONLY BE USED WITH WORD-SIZED DATA TYPES!
 *
 * Copyright 2008 Rainer Gerhards and Adiscon GmbH.
 *
 * This file is part of the rsyslog runtime library.
 *
 * The rsyslog runtime 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 3 of the License, or
 * (at your option) any later version.
 *
 * The rsyslog runtime 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 the rsyslog runtime library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * A copy of the GPL can be found in the file "COPYING" in this distribution.
 * A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution.
 */
#include "config.h" /* autotools! */

#ifndef INCLUDED_ATOMIC_H
#define INCLUDED_ATOMIC_H

/* for this release, we disable atomic calls because there seem to be some
 * portability problems and we can not fix that without destabilizing the build.
 * They simply came in too late. -- rgerhards, 2008-04-02
 */
#ifdef HAVE_ATOMIC_BUILTINS
#	define ATOMIC_INC(data) ((void) __sync_fetch_and_add(&(data), 1))
#	define ATOMIC_INC_AND_FETCH(data) __sync_fetch_and_add(&(data), 1)
#	define ATOMIC_DEC(data) ((void) __sync_sub_and_fetch(&(data), 1))
#	define ATOMIC_DEC_AND_FETCH(data) __sync_sub_and_fetch(&(data), 1)
#	define ATOMIC_FETCH_32BIT(data) ((unsigned) __sync_fetch_and_and(&(data), 0xffffffff))
#	define ATOMIC_STORE_1_TO_32BIT(data) __sync_lock_test_and_set(&(data), 1)
#	define ATOMIC_STORE_0_TO_INT(data) __sync_fetch_and_and(&(data), 0)
#	define ATOMIC_STORE_1_TO_INT(data) __sync_fetch_and_or(&(data), 1)
#	define ATOMIC_STORE_INT_TO_INT(data, val) __sync_fetch_and_or(&(data), (val))
#	define ATOMIC_CAS(data, oldVal, newVal) __sync_bool_compare_and_swap(&(data), (oldVal), (newVal));
#	define ATOMIC_CAS_VAL(data, oldVal, newVal) __sync_val_compare_and_swap(&(data), (oldVal), (newVal));
#else
#ifdef HAVE_SEMAPHORE_H
	/* we use POSIX semaphores instead */

#include "rsyslog.h"
#include <semaphore.h>

extern sem_t atomicSem;
rsRetVal atomicSemInit(void);
void atomicSemExit(void);

#if HAVE_TYPEOF
#define my_typeof(x) typeof(x)
#else /* sorry, can't determine types, using 'int' */
#define my_typeof(x) int
#endif

#	define ATOMIC_SUB(data, val) \
({ \
	my_typeof(data) tmp; \
	sem_wait(&atomicSem); \
	tmp = data; \
	data -= val; \
	sem_post(&atomicSem); \
	tmp; \
})

#	define ATOMIC_ADD(data, val) \
({ \
	my_typeof(data) tmp; \
	sem_wait(&atomicSem); \
	tmp = data; \
	data += val; \
	sem_post(&atomicSem); \
	tmp; \
})

#	define ATOMIC_INC_AND_FETCH(data) \
({ \
	my_typeof(data) tmp; \
	sem_wait(&atomicSem); \
	tmp = data; \
	data += 1; \
	sem_post(&atomicSem); \
	tmp; \
})

#	define ATOMIC_INC(data) ((void) ATOMIC_INC_AND_FETCH(data))

#	define ATOMIC_DEC_AND_FETCH(data) \
({ \
	sem_wait(&atomicSem); \
	data -= 1; \
	sem_post(&atomicSem); \
	data; \
})

#	define ATOMIC_DEC(data) ((void) ATOMIC_DEC_AND_FETCH(data))

#	define ATOMIC_FETCH_32BIT(data) ((unsigned) ATOMIC_ADD((data), 0xffffffff))

#	define ATOMIC_STORE_1_TO_32BIT(data) \
({ \
	my_typeof(data) tmp; \
	sem_wait(&atomicSem); \
	tmp = data; \
	data = 1; \
	sem_post(&atomicSem); \
	tmp; \
})

#	define ATOMIC_STORE_0_TO_INT(data) \
({ \
	my_typeof(data) tmp; \
	sem_wait(&atomicSem); \
	tmp = data; \
	data = 0; \
	sem_post(&atomicSem); \
	tmp; \
})

#	define ATOMIC_STORE_1_TO_INT(data) \
({ \
	my_typeof(data) tmp; \
	sem_wait(&atomicSem); \
	tmp = data; \
	data = 1; \
	sem_post(&atomicSem); \
	tmp; \
})

#	define ATOMIC_CAS(data, oldVal, newVal) \
({ \
	int ret; \
	sem_wait(&atomicSem); \
	if(data != oldVal) ret = 0; \
	else \
	{ \
		data = newVal; \
		ret = 1; \
	} \
	sem_post(&atomicSem); \
	ret; \
})

#	define ATOMIC_CAS_VAL(data, oldVal, newVal) \
({ \
	sem_wait(&atomicSem); \
	if(data == oldVal) \
	{ \
		data = newVal; \
	} \
	sem_post(&atomicSem); \
	data; \
})

#else /* not HAVE_SEMAPHORE_H */
	/* note that we gained parctical proof that theoretical problems DO occur
	 * if we do not properly address them. See this blog post for details:
	 * http://blog.gerhards.net/2009/01/rsyslog-data-race-analysis.html
	 * The bottom line is that if there are no atomics available, we should NOT
	 * simply go ahead and do without them - use mutexes or other things. The
	 * code needs to be checked against all those cases. -- rgerhards, 2009-01-30
	 */
#	warning "atomic builtins not available, using nul operations - rsyslogd will probably be racy!"
#	define ATOMIC_INC(data) (++(data))
#	define ATOMIC_DEC(data) (--(data))
#	define ATOMIC_DEC_AND_FETCH(data) (--(data))
#	define ATOMIC_FETCH_32BIT(data) (data)
#	define ATOMIC_STORE_1_TO_32BIT(data) (data) = 1
#	define ATOMIC_STORE_1_TO_INT(data) (data) = 1
#	define ATOMIC_STORE_0_TO_INT(data) (data) = 0
#	define ATOMIC_CAS_VAL(data, oldVal, newVal) (data) = (newVal)
#endif
#endif

#endif /* #ifndef INCLUDED_ATOMIC_H */