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>
22 #define ____cacheline_internodealigned_in_smp \
23 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
30 #include <sys/types.h>
32 #include <sys/param.h>
33 /* #include "atomic.h" */
36 * Exclusive locking primitives.
39 typedef pthread_mutex_t spinlock_t
;
41 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
42 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
44 static void spin_lock_init(spinlock_t
*sp
)
46 if (pthread_mutex_init(sp
, NULL
) != 0) {
47 perror("spin_lock_init:pthread_mutex_init");
52 static void spin_lock(spinlock_t
*sp
)
54 if (pthread_mutex_lock(sp
) != 0) {
55 perror("spin_lock:pthread_mutex_lock");
60 static void spin_unlock(spinlock_t
*sp
)
62 if (pthread_mutex_unlock(sp
) != 0) {
63 perror("spin_unlock:pthread_mutex_unlock");
68 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
69 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
72 * Thread creation/destruction primitives.
75 typedef pthread_t thread_id_t
;
77 #define NR_THREADS 4096
79 #define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0)
80 #define __THREAD_ID_MAP_WAITING ((thread_id_t) 1)
81 thread_id_t __thread_id_map
[NR_THREADS
];
82 spinlock_t __thread_id_map_mutex
;
84 #define for_each_thread(t) \
85 for (t = 0; t < NR_THREADS; t++)
87 #define for_each_running_thread(t) \
88 for (t = 0; t < NR_THREADS; t++) \
89 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
90 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
92 #define for_each_tid(t, tid) \
93 for (t = 0; t < NR_THREADS; t++) \
94 if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \
95 ((tid) != __THREAD_ID_MAP_WAITING))
97 pthread_key_t thread_id_key
;
99 static int __smp_thread_id(void)
102 thread_id_t tid
= pthread_self();
104 for (i
= 0; i
< NR_THREADS
; i
++) {
105 if (__thread_id_map
[i
] == tid
) {
106 long v
= i
+ 1; /* must be non-NULL. */
108 if (pthread_setspecific(thread_id_key
, (void *)v
) != 0) {
109 perror("pthread_setspecific");
115 spin_lock(&__thread_id_map_mutex
);
116 for (i
= 0; i
< NR_THREADS
; i
++) {
117 if (__thread_id_map
[i
] == tid
) {
118 spin_unlock(&__thread_id_map_mutex
);
122 spin_unlock(&__thread_id_map_mutex
);
123 fprintf(stderr
, "smp_thread_id: Rogue thread, id: %lu(%#lx)\n",
124 (unsigned long) tid
, (unsigned long) tid
);
128 static int smp_thread_id(void)
132 id
= pthread_getspecific(thread_id_key
);
134 return __smp_thread_id();
135 return ((long) id
- 1);
138 static thread_id_t
create_thread(void *(*func
)(void *), void *arg
)
143 spin_lock(&__thread_id_map_mutex
);
144 for (i
= 0; i
< NR_THREADS
; i
++) {
145 if (__thread_id_map
[i
] == __THREAD_ID_MAP_EMPTY
)
148 if (i
>= NR_THREADS
) {
149 spin_unlock(&__thread_id_map_mutex
);
150 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
153 __thread_id_map
[i
] = __THREAD_ID_MAP_WAITING
;
154 spin_unlock(&__thread_id_map_mutex
);
155 if (pthread_create(&tid
, NULL
, func
, arg
) != 0) {
156 perror("create_thread:pthread_create");
159 __thread_id_map
[i
] = tid
;
163 static void *wait_thread(thread_id_t tid
)
168 for (i
= 0; i
< NR_THREADS
; i
++) {
169 if (__thread_id_map
[i
] == tid
)
172 if (i
>= NR_THREADS
){
173 fprintf(stderr
, "wait_thread: bad tid = %lu(%#lx)\n",
174 (unsigned long)tid
, (unsigned long)tid
);
177 if (pthread_join(tid
, &vp
) != 0) {
178 perror("wait_thread:pthread_join");
181 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
185 static void wait_all_threads(void)
190 for (i
= 1; i
< NR_THREADS
; i
++) {
191 tid
= __thread_id_map
[i
];
192 if (tid
!= __THREAD_ID_MAP_EMPTY
&&
193 tid
!= __THREAD_ID_MAP_WAITING
)
194 (void)wait_thread(tid
);
198 #ifdef HAVE_SCHED_SETAFFINITY
199 static void run_on(int cpu
)
205 sched_setaffinity(0, sizeof(mask
), &mask
);
209 static void run_on(int cpu
__attribute__((unused
)))
211 #endif /* HAVE_SCHED_SETAFFINITY */
214 * timekeeping -- very crude -- should use MONOTONIC...
218 long long get_microseconds(void)
222 if (gettimeofday(&tv
, NULL
) != 0)
224 return ((long long)tv
.tv_sec
) * 1000000LL + (long long)tv
.tv_usec
;
228 * Per-thread variables.
231 #define DEFINE_PER_THREAD(type, name) \
234 __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \
235 } __per_thread_##name[NR_THREADS]
236 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
238 #define per_thread(name, thread) __per_thread_##name[thread].v
239 #define __get_thread_var(name) per_thread(name, smp_thread_id())
241 #define init_per_thread(name, v) \
244 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
245 per_thread(name, __i_p_t_i) = v; \
248 DEFINE_PER_THREAD(int, smp_processor_id
);
254 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
257 * Initialization -- Must be called before calling any primitives.
260 static void smp_init(void)
264 spin_lock_init(&__thread_id_map_mutex
);
265 __thread_id_map
[0] = pthread_self();
266 for (i
= 1; i
< NR_THREADS
; i
++)
267 __thread_id_map
[i
] = __THREAD_ID_MAP_EMPTY
;
268 init_per_thread(smp_processor_id
, 0);
269 if (pthread_key_create(&thread_id_key
, NULL
) != 0) {
270 perror("pthread_key_create");