1 // SPDX-FileCopyrightText: 2006 Paul E. McKenney, IBM.
3 // SPDX-License-Identifier: GPL-2.0-only
9 * common.h: Common Linux kernel-isms.
11 * Much code taken from the Linux kernel. For such code, the option
12 * to redistribute under later versions of GPL might not be available.
15 #include <urcu/compiler.h>
16 #include <urcu/arch.h>
17 #include <urcu/uatomic.h>
23 #define ____cacheline_internodealigned_in_smp \
24 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
31 #include <sys/types.h>
33 #include <sys/param.h>
34 /* #include "atomic.h" */
37 * Exclusive locking primitives.
40 typedef pthread_mutex_t spinlock_t
;
42 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
43 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
45 static void spin_lock_init(spinlock_t
*sp
)
47 if (pthread_mutex_init(sp
, NULL
) != 0) {
48 perror("spin_lock_init:pthread_mutex_init");
53 static void spin_lock(spinlock_t
*sp
)
55 if (pthread_mutex_lock(sp
) != 0) {
56 perror("spin_lock:pthread_mutex_lock");
61 static void spin_unlock(spinlock_t
*sp
)
63 if (pthread_mutex_unlock(sp
) != 0) {
64 perror("spin_unlock:pthread_mutex_unlock");
69 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
70 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
73 * Thread creation/destruction primitives.
76 typedef pthread_t thread_id_t
;
78 #define NR_THREADS 4096
80 #define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0)
81 #define __THREAD_ID_MAP_WAITING ((thread_id_t) 1)
82 thread_id_t __thread_id_map
[NR_THREADS
];
83 spinlock_t __thread_id_map_mutex
;
85 #define for_each_thread(t) \
86 for (t = 0; t < NR_THREADS; t++)
88 #define for_each_running_thread(t) \
89 for (t = 0; t < NR_THREADS; t++) \
90 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
91 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
93 #define for_each_tid(t, tid) \
94 for (t = 0; t < NR_THREADS; t++) \
95 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
96 ((tid) != __THREAD_ID_MAP_WAITING))
98 pthread_key_t thread_id_key
;
100 static int __smp_thread_id(void)
103 thread_id_t tid
= pthread_self();
105 for (i
= 0; i
< NR_THREADS
; i
++) {
106 if (uatomic_read(&__thread_id_map
[i
]) == tid
) {
107 long v
= i
+ 1; /* must be non-NULL. */
109 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
110 perror("pthread_setspecific");
116 spin_lock(&__thread_id_map_mutex
);
117 for (i
= 0; i
< NR_THREADS
; i
++) {
118 if (__thread_id_map
[i
] == tid
) {
119 spin_unlock(&__thread_id_map_mutex
);
123 spin_unlock(&__thread_id_map_mutex
);
124 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %lu(%#lx)\n",
125 (unsigned long) tid
, (unsigned long) tid
);
129 static int smp_thread_id(void)
133 id
= pthread_getspecific(thread_id_key
);
135 return __smp_thread_id();
136 return ((long) id
- 1);
139 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
144 spin_lock(&__thread_id_map_mutex
);
145 for (i
= 0; i
< NR_THREADS
; i
++) {
146 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
149 if (i
>= NR_THREADS
) {
150 spin_unlock(&__thread_id_map_mutex
);
151 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
154 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
156 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
157 perror("create_thread:pthread_create");
160 uatomic_set(&__thread_id_map
[i
], tid
);
161 spin_unlock(&__thread_id_map_mutex
);
165 static void *wait_thread(thread_id_t tid
)
170 for (i
= 0; i
< NR_THREADS
; i
++) {
171 if (uatomic_read(&__thread_id_map
[i
]) == tid
)
174 if (i
>= NR_THREADS
){
175 fprintf(stderr
, "wait_thread: bad tid = %lu(%#lx)\n",
176 (unsigned long)tid
, (unsigned long)tid
);
179 if (pthread_join(tid
, &vp
) != 0) {
180 perror("wait_thread:pthread_join");
183 uatomic_set(&__thread_id_map
[i
], __THREAD_ID_MAP_EMPTY
);
187 static void wait_all_threads(void)
192 for (i
= 1; i
< NR_THREADS
; i
++) {
193 tid
= __thread_id_map
[i
];
194 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
195 tid
!= __THREAD_ID_MAP_WAITING
)
196 (void)wait_thread(tid
);
200 #ifdef HAVE_SCHED_SETAFFINITY
201 static void run_on(int cpu
)
207 sched_setaffinity(0, sizeof(mask
), &mask
);
211 static void run_on(int cpu
__attribute__((unused
)))
213 #endif /* HAVE_SCHED_SETAFFINITY */
216 * timekeeping -- very crude -- should use MONOTONIC...
220 long long get_microseconds(void)
224 if (gettimeofday(&tv
, NULL
) != 0)
226 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
230 * Per-thread variables.
233 #define DEFINE_PER_THREAD(type, name) \
236 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
237 } __per_thread_##name[NR_THREADS]
238 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
240 #define per_thread(name, thread) __per_thread_##name[thread].v
241 #define __get_thread_var(name) per_thread(name, smp_thread_id())
243 #define init_per_thread(name, v) \
246 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
247 per_thread(name, __i_p_t_i) = v; \
250 DEFINE_PER_THREAD(int, smp_processor_id
);
256 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
259 * Initialization -- Must be called before calling any primitives.
262 static void smp_init(void)
266 spin_lock_init(&__thread_id_map_mutex
);
267 __thread_id_map
[0] = pthread_self();
268 for (i
= 1; i
< NR_THREADS
; i
++)
269 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
270 init_per_thread(smp_processor_id
, 0);
271 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
272 perror("pthread_key_create");