5 * common.h: Common Linux kernel-isms.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; but version 2 of the License only due
10 * to code included from the Linux kernel.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Copyright (c) 2006 Paul E. McKenney, IBM.
23 * Much code taken from the Linux kernel. For such code, the option
24 * to redistribute under later versions of GPL might not be available.
27 #include <urcu/compiler.h>
28 #include <urcu/arch.h>
34 #define ____cacheline_internodealigned_in_smp \
35 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
38 * api_pthreads.h: API mapping to pthreads environment.
40 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version. However, please note that much
44 * of the code in this file derives from the Linux kernel, and that such
45 * code may not be available except under GPLv2.
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, write to the Free Software
54 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
56 * Copyright (c) 2006 Paul E. McKenney, IBM.
63 #include <sys/types.h>
65 #include <sys/param.h>
66 /* #include "atomic.h" */
69 * Exclusive locking primitives.
72 typedef pthread_mutex_t spinlock_t
;
74 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
75 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
77 static void spin_lock_init(spinlock_t
*sp
)
79 if (pthread_mutex_init(sp
, NULL
) != 0) {
80 perror("spin_lock_init:pthread_mutex_init");
85 static void spin_lock(spinlock_t
*sp
)
87 if (pthread_mutex_lock(sp
) != 0) {
88 perror("spin_lock:pthread_mutex_lock");
93 static void spin_unlock(spinlock_t
*sp
)
95 if (pthread_mutex_unlock(sp
) != 0) {
96 perror("spin_unlock:pthread_mutex_unlock");
101 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
102 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
105 * Thread creation/destruction primitives.
108 typedef pthread_t thread_id_t
;
110 #define NR_THREADS 4096
112 #define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0)
113 #define __THREAD_ID_MAP_WAITING ((thread_id_t) 1)
114 thread_id_t __thread_id_map
[NR_THREADS
];
115 spinlock_t __thread_id_map_mutex
;
117 #define for_each_thread(t) \
118 for (t = 0; t < NR_THREADS; t++)
120 #define for_each_running_thread(t) \
121 for (t = 0; t < NR_THREADS; t++) \
122 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
123 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
125 #define for_each_tid(t, tid) \
126 for (t = 0; t < NR_THREADS; t++) \
127 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
128 ((tid) != __THREAD_ID_MAP_WAITING))
130 pthread_key_t thread_id_key
;
132 static int __smp_thread_id(void)
135 thread_id_t tid
= pthread_self();
137 for (i
= 0; i
< NR_THREADS
; i
++) {
138 if (__thread_id_map
[i
] == tid
) {
139 long v
= i
+ 1; /* must be non-NULL. */
141 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
142 perror("pthread_setspecific");
148 spin_lock(&__thread_id_map_mutex
);
149 for (i
= 0; i
< NR_THREADS
; i
++) {
150 if (__thread_id_map
[i
] == tid
) {
151 spin_unlock(&__thread_id_map_mutex
);
155 spin_unlock(&__thread_id_map_mutex
);
156 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %lu(%#lx)\n",
157 (unsigned long) tid
, (unsigned long) tid
);
161 static int smp_thread_id(void)
165 id
= pthread_getspecific(thread_id_key
);
167 return __smp_thread_id();
168 return ((long) id
- 1);
171 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
176 spin_lock(&__thread_id_map_mutex
);
177 for (i
= 0; i
< NR_THREADS
; i
++) {
178 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
181 if (i
>= NR_THREADS
) {
182 spin_unlock(&__thread_id_map_mutex
);
183 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
186 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
187 spin_unlock(&__thread_id_map_mutex
);
188 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
189 perror("create_thread:pthread_create");
192 __thread_id_map
[i
] = tid
;
196 static void *wait_thread(thread_id_t tid
)
201 for (i
= 0; i
< NR_THREADS
; i
++) {
202 if (__thread_id_map
[i
] == tid
)
205 if (i
>= NR_THREADS
){
206 fprintf(stderr
, "wait_thread: bad tid = %lu(%#lx)\n",
207 (unsigned long)tid
, (unsigned long)tid
);
210 if (pthread_join(tid
, &vp
) != 0) {
211 perror("wait_thread:pthread_join");
214 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
218 static void wait_all_threads(void)
223 for (i
= 1; i
< NR_THREADS
; i
++) {
224 tid
= __thread_id_map
[i
];
225 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
226 tid
!= __THREAD_ID_MAP_WAITING
)
227 (void)wait_thread(tid
);
231 #ifdef HAVE_SCHED_SETAFFINITY
232 static void run_on(int cpu
)
238 sched_setaffinity(0, sizeof(mask
), &mask
);
242 static void run_on(int cpu
__attribute__((unused
)))
244 #endif /* HAVE_SCHED_SETAFFINITY */
247 * timekeeping -- very crude -- should use MONOTONIC...
251 long long get_microseconds(void)
255 if (gettimeofday(&tv
, NULL
) != 0)
257 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
261 * Per-thread variables.
264 #define DEFINE_PER_THREAD(type, name) \
267 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
268 } __per_thread_##name[NR_THREADS]
269 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
271 #define per_thread(name, thread) __per_thread_##name[thread].v
272 #define __get_thread_var(name) per_thread(name, smp_thread_id())
274 #define init_per_thread(name, v) \
277 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
278 per_thread(name, __i_p_t_i) = v; \
281 DEFINE_PER_THREAD(int, smp_processor_id
);
287 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
290 * Initialization -- Must be called before calling any primitives.
293 static void smp_init(void)
297 spin_lock_init(&__thread_id_map_mutex
);
298 __thread_id_map
[0] = pthread_self();
299 for (i
= 1; i
< NR_THREADS
; i
++)
300 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
301 init_per_thread(smp_processor_id
, 0);
302 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
303 perror("pthread_key_create");