2 * Specialised local-global spinlock. Can only be declared as global variables
3 * to avoid overhead and keep things simple (and we don't want to start using
4 * these inside dynamically allocated structures).
6 * "local/global locks" (lglocks) can be used to:
8 * - Provide fast exclusive access to per-CPU data, with exclusive access to
9 * another CPU's data allowed but possibly subject to contention, and to
10 * provide very slow exclusive access to all per-CPU data.
11 * - Or to provide very fast and scalable read serialisation, and to provide
12 * very slow exclusive serialisation of data (not necessarily per-CPU data).
14 * Brlocks are also implemented as a short-hand notation for the latter use
17 * Copyright 2009, 2010, Nick Piggin, Novell Inc.
19 #ifndef __LINUX_LGLOCK_H
20 #define __LINUX_LGLOCK_H
22 #include <linux/spinlock.h>
23 #include <linux/lockdep.h>
24 #include <linux/percpu.h>
25 #include <linux/cpu.h>
26 #include <linux/notifier.h>
30 #ifdef CONFIG_DEBUG_LOCK_ALLOC
31 #define LOCKDEP_INIT_MAP lockdep_init_map
33 #define LOCKDEP_INIT_MAP(a, b, c, d)
37 #ifdef CONFIG_PREEMPT_RT_FULL
38 struct rt_mutex __percpu *lock;
40 arch_spinlock_t __percpu *lock;
42 #ifdef CONFIG_DEBUG_LOCK_ALLOC
43 struct lock_class_key lock_key;
44 struct lockdep_map lock_dep_map;
48 #ifdef CONFIG_PREEMPT_RT_FULL
49 # define DEFINE_LGLOCK(name) \
50 static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
51 = __RT_MUTEX_INITIALIZER( name ## _lock); \
52 struct lglock name = { .lock = &name ## _lock }
54 # define DEFINE_STATIC_LGLOCK(name) \
55 static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
56 = __RT_MUTEX_INITIALIZER( name ## _lock); \
57 static struct lglock name = { .lock = &name ## _lock }
61 #define DEFINE_LGLOCK(name) \
62 static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
63 = __ARCH_SPIN_LOCK_UNLOCKED; \
64 struct lglock name = { .lock = &name ## _lock }
66 #define DEFINE_STATIC_LGLOCK(name) \
67 static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
68 = __ARCH_SPIN_LOCK_UNLOCKED; \
69 static struct lglock name = { .lock = &name ## _lock }
72 void lg_lock_init(struct lglock *lg, char *name);
74 void lg_local_lock(struct lglock *lg);
75 void lg_local_unlock(struct lglock *lg);
76 void lg_local_lock_cpu(struct lglock *lg, int cpu);
77 void lg_local_unlock_cpu(struct lglock *lg, int cpu);
79 void lg_double_lock(struct lglock *lg, int cpu1, int cpu2);
80 void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2);
82 void lg_global_lock(struct lglock *lg);
83 void lg_global_unlock(struct lglock *lg);
85 #ifndef CONFIG_PREEMPT_RT_FULL
86 #define lg_global_trylock_relax(name) lg_global_lock(name)
88 void lg_global_trylock_relax(struct lglock *lg);
92 /* When !CONFIG_SMP, map lglock to spinlock */
93 #define lglock spinlock
94 #define DEFINE_LGLOCK(name) DEFINE_SPINLOCK(name)
95 #define DEFINE_STATIC_LGLOCK(name) static DEFINE_SPINLOCK(name)
96 #define lg_lock_init(lg, name) spin_lock_init(lg)
97 #define lg_local_lock spin_lock
98 #define lg_local_unlock spin_unlock
99 #define lg_local_lock_cpu(lg, cpu) spin_lock(lg)
100 #define lg_local_unlock_cpu(lg, cpu) spin_unlock(lg)
101 #define lg_global_lock spin_lock
102 #define lg_global_unlock spin_unlock