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>
35 #define ____cacheline_internodealigned_in_smp \
36 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
39 * api_pthreads.h: API mapping to pthreads environment.
41 * This program is free software; you can redistribute it and/or modify
42 * it under the terms of the GNU General Public License as published by
43 * the Free Software Foundation; either version 2 of the License, or
44 * (at your option) any later version. However, please note that much
45 * of the code in this file derives from the Linux kernel, and that such
46 * code may not be available except under GPLv2.
48 * This program is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
53 * You should have received a copy of the GNU General Public License
54 * along with this program; if not, write to the Free Software
55 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
57 * Copyright (c) 2006 Paul E. McKenney, IBM.
64 #include <sys/types.h>
66 #include <sys/param.h>
67 /* #include "atomic.h" */
70 * Exclusive locking primitives.
73 typedef pthread_mutex_t spinlock_t
;
75 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
76 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
78 static void spin_lock_init(spinlock_t
*sp
)
80 if (pthread_mutex_init(sp
, NULL
) != 0) {
81 perror("spin_lock_init:pthread_mutex_init");
86 static void spin_lock(spinlock_t
*sp
)
88 if (pthread_mutex_lock(sp
) != 0) {
89 perror("spin_lock:pthread_mutex_lock");
94 static void spin_unlock(spinlock_t
*sp
)
96 if (pthread_mutex_unlock(sp
) != 0) {
97 perror("spin_unlock:pthread_mutex_unlock");
102 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
103 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
106 * Thread creation/destruction primitives.
109 typedef pthread_t thread_id_t
;
111 #define NR_THREADS 128
113 #define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0)
114 #define __THREAD_ID_MAP_WAITING ((thread_id_t) 1)
115 thread_id_t __thread_id_map
[NR_THREADS
];
116 spinlock_t __thread_id_map_mutex
;
118 #define for_each_thread(t) \
119 for (t = 0; t < NR_THREADS; t++)
121 #define for_each_running_thread(t) \
122 for (t = 0; t < NR_THREADS; t++) \
123 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
124 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
126 #define for_each_tid(t, tid) \
127 for (t = 0; t < NR_THREADS; t++) \
128 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
129 ((tid) != __THREAD_ID_MAP_WAITING))
131 pthread_key_t thread_id_key
;
133 static int __smp_thread_id(void)
136 thread_id_t tid
= pthread_self();
138 for (i
= 0; i
< NR_THREADS
; i
++) {
139 if (__thread_id_map
[i
] == tid
) {
140 long v
= i
+ 1; /* must be non-NULL. */
142 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
143 perror("pthread_setspecific");
149 spin_lock(&__thread_id_map_mutex
);
150 for (i
= 0; i
< NR_THREADS
; i
++) {
151 if (__thread_id_map
[i
] == tid
) {
152 spin_unlock(&__thread_id_map_mutex
);
156 spin_unlock(&__thread_id_map_mutex
);
157 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %lu(%#lx)\n",
158 (unsigned long) tid
, (unsigned long) tid
);
162 static int smp_thread_id(void)
166 id
= pthread_getspecific(thread_id_key
);
168 return __smp_thread_id();
169 return (long)(id
- 1);
172 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
177 spin_lock(&__thread_id_map_mutex
);
178 for (i
= 0; i
< NR_THREADS
; i
++) {
179 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
182 if (i
>= NR_THREADS
) {
183 spin_unlock(&__thread_id_map_mutex
);
184 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
187 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
188 spin_unlock(&__thread_id_map_mutex
);
189 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
190 perror("create_thread:pthread_create");
193 __thread_id_map
[i
] = tid
;
197 static void *wait_thread(thread_id_t tid
)
202 for (i
= 0; i
< NR_THREADS
; i
++) {
203 if (__thread_id_map
[i
] == tid
)
206 if (i
>= NR_THREADS
){
207 fprintf(stderr
, "wait_thread: bad tid = %lu(%#lx)\n",
208 (unsigned long)tid
, (unsigned long)tid
);
211 if (pthread_join(tid
, &vp
) != 0) {
212 perror("wait_thread:pthread_join");
215 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
219 static void wait_all_threads(void)
224 for (i
= 1; i
< NR_THREADS
; i
++) {
225 tid
= __thread_id_map
[i
];
226 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
227 tid
!= __THREAD_ID_MAP_WAITING
)
228 (void)wait_thread(tid
);
232 static void run_on(int cpu
)
234 #if HAVE_SCHED_SETAFFINITY
239 #if SCHED_SETAFFINITY_ARGS == 2
240 sched_setaffinity(0, &mask
);
242 sched_setaffinity(0, sizeof(mask
), &mask
);
244 #endif /* HAVE_SCHED_SETAFFINITY */
248 * 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");