forked from c-icap/c-icap-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic.c
169 lines (158 loc) · 8.47 KB
/
atomic.c
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
/*
* Copyright (C) 2004-2021 Christos Tsantilas
*
* This program 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 program 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*/
#include "common.h"
#include "atomic.h"
#include "ci_threads.h"
#include "debug.h"
#include "proc_mutex.h"
#if defined(__CI_INLINE_POSIX_ATOMICS)
__ci_implement_atomic_ops(,i32_non_inline, int32_t);
__ci_implement_atomic_ops(,u32_non_inline, uint32_t);
__ci_implement_atomic_ops(,u64_non_inline, uint64_t);
__ci_implement_atomic_ops(,i64_non_inline, int64_t);
#if defined(CI_ATOMICS_USE_128BIT)
#if defined(__clang__)
/* Suppress warning about "large atomic operation may incur significant
performance" */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Watomic-alignment"
#endif
__ci_implement_atomic_ops(,u128_non_inline, unsigned __int128);
__ci_implement_atomic_ops(,i128_non_inline, __int128);
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#endif
int ci_atomics_init()
{
return 1;
}
#else
#define MTX_SIZE 5
static ci_thread_mutex_t thr_mtx[MTX_SIZE];
static ci_proc_mutex_t proc_mtx[MTX_SIZE];
#define get_mtx(array, pointer) (&array[((size_t)((void *)pointer - (void *)0)) % MTX_SIZE])
#define _implement_atomic_ops(name, type) \
void ci_atomic_load_##name(const type *counter, type *store) { \
ci_thread_mutex_t *mtx = get_mtx(thr_mtx, counter); \
ci_thread_mutex_lock(mtx); \
*store = *counter; \
ci_thread_mutex_unlock(mtx); \
} \
void ci_atomic_store_##name(type *counter, type store) { \
ci_thread_mutex_t *mtx = get_mtx(thr_mtx, counter); \
ci_thread_mutex_lock(mtx); \
*counter = *store; \
ci_thread_mutex_unlock(mtx); \
} \
void ci_atomic_add_##name(type *counter, type add) { \
ci_thread_mutex_t *mtx = get_mtx(thr_mtx, counter); \
ci_thread_mutex_lock(mtx); \
*counter += add; \
ci_thread_mutex_unlock(mtx); \
} \
void ci_atomic_sub_##name(type *counter, type sub) { \
ci_thread_mutex_t *mtx = get_mtx(thr_mtx, counter); \
ci_thread_mutex_lock(mtx); \
*counter -= sub; \
ci_thread_mutex_unlock(mtx); \
} \
type ci_atomic_fetch_add_##name(type *counter, type add) { \
type old; \
ci_thread_mutex_t *mtx = get_mtx(thr_mtx, counter); \
ci_thread_mutex_lock(mtx); \
old = *counter; \
*counter += add; \
ci_thread_mutex_unlock(mtx); \
return old; \
} \
type ci_atomic_fetch_sub_##name(type *counter, type sub) { \
type old; \
ci_thread_mutex_t *mtx = get_mtx(thr_mtx, counter); \
ci_thread_mutex_lock(mtx); \
old = *counter; \
*counter -= sub; \
ci_thread_mutex_unlock(mtx); \
return old; \
} \
void ci_atomic_load_##name ## _gl(const type *counter, type *store) { \
ci_proc_mutex_t *mtx = get_mtx(proc_mtx, counter); \
ci_proc_mutex_lock(mtx); \
*store = *counter; \
ci_proc_mutex_unlock(mtx); \
} \
void ci_atomic_store_##name ## _gl(type *counter, type store) { \
ci_proc_mutex_t *mtx = get_mtx(proc_mtx, counter); \
ci_proc_mutex_lock(mtx); \
*counter = *store; \
ci_proc_mutex_unlock(mtx); \
} \
void ci_atomic_add_##name ## _gl(type *counter, type add) { \
ci_proc_mutex_t *mtx = get_mtx(proc_mtx, counter); \
ci_proc_mutex_lock(mtx); \
*counter += add; \
ci_proc_mutex_unlock(mtx); \
} \
void ci_atomic_sub_##name ## _gl(type *counter, type sub) { \
ci_proc_mutex_t *mtx = get_mtx(proc_mtx, counter); \
ci_proc_mutex_lock(mtx); \
*counter -= sub; \
ci_proc_mutex_unlock(mtx); \
} \
type ci_atomic_fetch_add_##name ## _gl(type *counter, type add) { \
type old; \
ci_proc_mutex_t *mtx = get_mtx(proc_mtx, counter); \
ci_proc_mutex_lock(mtx); \
old = *counter; \
*counter += add; \
ci_proc_mutex_unlock(mtx); \
} \
type ci_atomic_fetch_sub_##name ## _gl(type *counter, type sub) { \
type old; \
ci_proc_mutex_t *mtx = get_mtx(proc_mtx, counter); \
ci_proc_mutex_lock(mtx); \
old = *counter; \
*counter -= sub; \
ci_proc_mutex_unlock(mtx); \
}
_implement_atomic_ops(i32_non_inline, int32_t);
_implement_atomic_ops(u32_non_inline, uint32_t);
_implement_atomic_ops(u64_non_inline, uint64_t);
_implement_atomic_ops(i64_non_inline, int64_t);
#if defined(CI_ATOMICS_USE_128BIT)
_implement_atomic_ops(u128_non_inline, unsigned __int128);
_implement_atomic_ops(i128_non_inline, __int128);
#endif
int ci_atomics_init()
{
int i;
for (i = 0; i < MTX_SIZE; ++i ) {
ci_thread_mutex_init(&(thr_mtx[i]));
}
int ret = 0 , error = 0;
for (i = 0; i < MTX_SIZE; ++i) {
char name[128];
snprintf(name, sizeof(name), "ci_atomic_%d", i);
ret = ci_proc_mutex_init(&(proc_mtx[i]), name);
if (!ret)
error = 1;
}
return !error;
}
#endif