| 1 | #ifndef _INCLUDE_API_H |
| 2 | #define _INCLUDE_API_H |
| 3 | |
| 4 | #define _GNU_SOURCE |
| 5 | #include "config.h" |
| 6 | |
| 7 | /* |
| 8 | * common.h: Common Linux kernel-isms. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; but version 2 of the License only due |
| 13 | * to code included from the Linux kernel. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | * Copyright (c) 2006 Paul E. McKenney, IBM. |
| 25 | * |
| 26 | * Much code taken from the Linux kernel. For such code, the option |
| 27 | * to redistribute under later versions of GPL might not be available. |
| 28 | */ |
| 29 | |
| 30 | #include <urcu/compiler.h> |
| 31 | #include <urcu/arch.h> |
| 32 | #include "cpuset.h" |
| 33 | |
| 34 | /* |
| 35 | * Machine parameters. |
| 36 | */ |
| 37 | |
| 38 | #define ____cacheline_internodealigned_in_smp \ |
| 39 | __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))) |
| 40 | |
| 41 | /* |
| 42 | * api_pthreads.h: API mapping to pthreads environment. |
| 43 | * |
| 44 | * This program is free software; you can redistribute it and/or modify |
| 45 | * it under the terms of the GNU General Public License as published by |
| 46 | * the Free Software Foundation; either version 2 of the License, or |
| 47 | * (at your option) any later version. However, please note that much |
| 48 | * of the code in this file derives from the Linux kernel, and that such |
| 49 | * code may not be available except under GPLv2. |
| 50 | * |
| 51 | * This program is distributed in the hope that it will be useful, |
| 52 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 53 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 54 | * GNU General Public License for more details. |
| 55 | * |
| 56 | * You should have received a copy of the GNU General Public License |
| 57 | * along with this program; if not, write to the Free Software |
| 58 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 59 | * |
| 60 | * Copyright (c) 2006 Paul E. McKenney, IBM. |
| 61 | */ |
| 62 | |
| 63 | #include <stdio.h> |
| 64 | #include <stdlib.h> |
| 65 | #include <errno.h> |
| 66 | #include <limits.h> |
| 67 | #include <sys/types.h> |
| 68 | #include <pthread.h> |
| 69 | #include <sys/param.h> |
| 70 | /* #include "atomic.h" */ |
| 71 | |
| 72 | /* |
| 73 | * Exclusive locking primitives. |
| 74 | */ |
| 75 | |
| 76 | typedef pthread_mutex_t spinlock_t; |
| 77 | |
| 78 | #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER; |
| 79 | #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER |
| 80 | |
| 81 | static void spin_lock_init(spinlock_t *sp) |
| 82 | { |
| 83 | if (pthread_mutex_init(sp, NULL) != 0) { |
| 84 | perror("spin_lock_init:pthread_mutex_init"); |
| 85 | exit(-1); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static void spin_lock(spinlock_t *sp) |
| 90 | { |
| 91 | if (pthread_mutex_lock(sp) != 0) { |
| 92 | perror("spin_lock:pthread_mutex_lock"); |
| 93 | exit(-1); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static void spin_unlock(spinlock_t *sp) |
| 98 | { |
| 99 | if (pthread_mutex_unlock(sp) != 0) { |
| 100 | perror("spin_unlock:pthread_mutex_unlock"); |
| 101 | exit(-1); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0) |
| 106 | #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0) |
| 107 | |
| 108 | /* |
| 109 | * Thread creation/destruction primitives. |
| 110 | */ |
| 111 | |
| 112 | typedef pthread_t thread_id_t; |
| 113 | |
| 114 | #define NR_THREADS 128 |
| 115 | |
| 116 | #define __THREAD_ID_MAP_EMPTY ((thread_id_t) 0) |
| 117 | #define __THREAD_ID_MAP_WAITING ((thread_id_t) 1) |
| 118 | thread_id_t __thread_id_map[NR_THREADS]; |
| 119 | spinlock_t __thread_id_map_mutex; |
| 120 | |
| 121 | #define for_each_thread(t) \ |
| 122 | for (t = 0; t < NR_THREADS; t++) |
| 123 | |
| 124 | #define for_each_running_thread(t) \ |
| 125 | for (t = 0; t < NR_THREADS; t++) \ |
| 126 | if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \ |
| 127 | (__thread_id_map[t] != __THREAD_ID_MAP_WAITING)) |
| 128 | |
| 129 | #define for_each_tid(t, tid) \ |
| 130 | for (t = 0; t < NR_THREADS; t++) \ |
| 131 | if ((((tid) = __thread_id_map[t]) != __THREAD_ID_MAP_EMPTY) && \ |
| 132 | ((tid) != __THREAD_ID_MAP_WAITING)) |
| 133 | |
| 134 | pthread_key_t thread_id_key; |
| 135 | |
| 136 | static int __smp_thread_id(void) |
| 137 | { |
| 138 | int i; |
| 139 | thread_id_t tid = pthread_self(); |
| 140 | |
| 141 | for (i = 0; i < NR_THREADS; i++) { |
| 142 | if (__thread_id_map[i] == tid) { |
| 143 | long v = i + 1; /* must be non-NULL. */ |
| 144 | |
| 145 | if (pthread_setspecific(thread_id_key, (void *)v) != 0) { |
| 146 | perror("pthread_setspecific"); |
| 147 | exit(-1); |
| 148 | } |
| 149 | return i; |
| 150 | } |
| 151 | } |
| 152 | spin_lock(&__thread_id_map_mutex); |
| 153 | for (i = 0; i < NR_THREADS; i++) { |
| 154 | if (__thread_id_map[i] == tid) { |
| 155 | spin_unlock(&__thread_id_map_mutex); |
| 156 | return i; |
| 157 | } |
| 158 | } |
| 159 | spin_unlock(&__thread_id_map_mutex); |
| 160 | fprintf(stderr, "smp_thread_id: Rogue thread, id: %lu(%#lx)\n", |
| 161 | (unsigned long) tid, (unsigned long) tid); |
| 162 | exit(-1); |
| 163 | } |
| 164 | |
| 165 | static int smp_thread_id(void) |
| 166 | { |
| 167 | void *id; |
| 168 | |
| 169 | id = pthread_getspecific(thread_id_key); |
| 170 | if (id == NULL) |
| 171 | return __smp_thread_id(); |
| 172 | return (long)(id - 1); |
| 173 | } |
| 174 | |
| 175 | static thread_id_t create_thread(void *(*func)(void *), void *arg) |
| 176 | { |
| 177 | thread_id_t tid; |
| 178 | int i; |
| 179 | |
| 180 | spin_lock(&__thread_id_map_mutex); |
| 181 | for (i = 0; i < NR_THREADS; i++) { |
| 182 | if (__thread_id_map[i] == __THREAD_ID_MAP_EMPTY) |
| 183 | break; |
| 184 | } |
| 185 | if (i >= NR_THREADS) { |
| 186 | spin_unlock(&__thread_id_map_mutex); |
| 187 | fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS); |
| 188 | exit(-1); |
| 189 | } |
| 190 | __thread_id_map[i] = __THREAD_ID_MAP_WAITING; |
| 191 | spin_unlock(&__thread_id_map_mutex); |
| 192 | if (pthread_create(&tid, NULL, func, arg) != 0) { |
| 193 | perror("create_thread:pthread_create"); |
| 194 | exit(-1); |
| 195 | } |
| 196 | __thread_id_map[i] = tid; |
| 197 | return tid; |
| 198 | } |
| 199 | |
| 200 | static void *wait_thread(thread_id_t tid) |
| 201 | { |
| 202 | int i; |
| 203 | void *vp; |
| 204 | |
| 205 | for (i = 0; i < NR_THREADS; i++) { |
| 206 | if (__thread_id_map[i] == tid) |
| 207 | break; |
| 208 | } |
| 209 | if (i >= NR_THREADS){ |
| 210 | fprintf(stderr, "wait_thread: bad tid = %lu(%#lx)\n", |
| 211 | (unsigned long)tid, (unsigned long)tid); |
| 212 | exit(-1); |
| 213 | } |
| 214 | if (pthread_join(tid, &vp) != 0) { |
| 215 | perror("wait_thread:pthread_join"); |
| 216 | exit(-1); |
| 217 | } |
| 218 | __thread_id_map[i] = __THREAD_ID_MAP_EMPTY; |
| 219 | return vp; |
| 220 | } |
| 221 | |
| 222 | static void wait_all_threads(void) |
| 223 | { |
| 224 | int i; |
| 225 | thread_id_t tid; |
| 226 | |
| 227 | for (i = 1; i < NR_THREADS; i++) { |
| 228 | tid = __thread_id_map[i]; |
| 229 | if (tid != __THREAD_ID_MAP_EMPTY && |
| 230 | tid != __THREAD_ID_MAP_WAITING) |
| 231 | (void)wait_thread(tid); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static void run_on(int cpu) |
| 236 | { |
| 237 | #if HAVE_SCHED_SETAFFINITY |
| 238 | cpu_set_t mask; |
| 239 | |
| 240 | CPU_ZERO(&mask); |
| 241 | CPU_SET(cpu, &mask); |
| 242 | #if SCHED_SETAFFINITY_ARGS == 2 |
| 243 | sched_setaffinity(0, &mask); |
| 244 | #else |
| 245 | sched_setaffinity(0, sizeof(mask), &mask); |
| 246 | #endif |
| 247 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * timekeeping -- very crude -- should use MONOTONIC... |
| 252 | */ |
| 253 | |
| 254 | long long get_microseconds(void) |
| 255 | { |
| 256 | struct timeval tv; |
| 257 | |
| 258 | if (gettimeofday(&tv, NULL) != 0) |
| 259 | abort(); |
| 260 | return ((long long)tv.tv_sec) * 1000000LL + (long long)tv.tv_usec; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Per-thread variables. |
| 265 | */ |
| 266 | |
| 267 | #define DEFINE_PER_THREAD(type, name) \ |
| 268 | struct { \ |
| 269 | __typeof__(type) v \ |
| 270 | __attribute__((__aligned__(CAA_CACHE_LINE_SIZE))); \ |
| 271 | } __per_thread_##name[NR_THREADS] |
| 272 | #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name) |
| 273 | |
| 274 | #define per_thread(name, thread) __per_thread_##name[thread].v |
| 275 | #define __get_thread_var(name) per_thread(name, smp_thread_id()) |
| 276 | |
| 277 | #define init_per_thread(name, v) \ |
| 278 | do { \ |
| 279 | int __i_p_t_i; \ |
| 280 | for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \ |
| 281 | per_thread(name, __i_p_t_i) = v; \ |
| 282 | } while (0) |
| 283 | |
| 284 | DEFINE_PER_THREAD(int, smp_processor_id); |
| 285 | |
| 286 | /* |
| 287 | * Bug checks. |
| 288 | */ |
| 289 | |
| 290 | #define BUG_ON(c) do { if (!(c)) abort(); } while (0) |
| 291 | |
| 292 | /* |
| 293 | * Initialization -- Must be called before calling any primitives. |
| 294 | */ |
| 295 | |
| 296 | static void smp_init(void) |
| 297 | { |
| 298 | int i; |
| 299 | |
| 300 | spin_lock_init(&__thread_id_map_mutex); |
| 301 | __thread_id_map[0] = pthread_self(); |
| 302 | for (i = 1; i < NR_THREADS; i++) |
| 303 | __thread_id_map[i] = __THREAD_ID_MAP_EMPTY; |
| 304 | init_per_thread(smp_processor_id, 0); |
| 305 | if (pthread_key_create(&thread_id_key, NULL) != 0) { |
| 306 | perror("pthread_key_create"); |
| 307 | exit(-1); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | #endif |