lglock.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  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).
  5. *
  6. * "local/global locks" (lglocks) can be used to:
  7. *
  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).
  13. *
  14. * Brlocks are also implemented as a short-hand notation for the latter use
  15. * case.
  16. *
  17. * Copyright 2009, 2010, Nick Piggin, Novell Inc.
  18. */
  19. #ifndef __LINUX_LGLOCK_H
  20. #define __LINUX_LGLOCK_H
  21. #include <linux/spinlock.h>
  22. #include <linux/lockdep.h>
  23. #include <linux/percpu.h>
  24. #include <linux/cpu.h>
  25. #include <linux/notifier.h>
  26. #ifdef CONFIG_SMP
  27. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  28. #define LOCKDEP_INIT_MAP lockdep_init_map
  29. #else
  30. #define LOCKDEP_INIT_MAP(a, b, c, d)
  31. #endif
  32. struct lglock {
  33. arch_spinlock_t __percpu *lock;
  34. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  35. struct lock_class_key lock_key;
  36. struct lockdep_map lock_dep_map;
  37. #endif
  38. };
  39. #define DEFINE_LGLOCK(name) \
  40. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  41. = __ARCH_SPIN_LOCK_UNLOCKED; \
  42. struct lglock name = { .lock = &name ## _lock }
  43. #define DEFINE_STATIC_LGLOCK(name) \
  44. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  45. = __ARCH_SPIN_LOCK_UNLOCKED; \
  46. static struct lglock name = { .lock = &name ## _lock }
  47. void lg_lock_init(struct lglock *lg, char *name);
  48. void lg_local_lock(struct lglock *lg);
  49. void lg_local_unlock(struct lglock *lg);
  50. void lg_local_lock_cpu(struct lglock *lg, int cpu);
  51. void lg_local_unlock_cpu(struct lglock *lg, int cpu);
  52. void lg_double_lock(struct lglock *lg, int cpu1, int cpu2);
  53. void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2);
  54. void lg_global_lock(struct lglock *lg);
  55. void lg_global_unlock(struct lglock *lg);
  56. #else
  57. /* When !CONFIG_SMP, map lglock to spinlock */
  58. #define lglock spinlock
  59. #define DEFINE_LGLOCK(name) DEFINE_SPINLOCK(name)
  60. #define DEFINE_STATIC_LGLOCK(name) static DEFINE_SPINLOCK(name)
  61. #define lg_lock_init(lg, name) spin_lock_init(lg)
  62. #define lg_local_lock spin_lock
  63. #define lg_local_unlock spin_unlock
  64. #define lg_local_lock_cpu(lg, cpu) spin_lock(lg)
  65. #define lg_local_unlock_cpu(lg, cpu) spin_unlock(lg)
  66. #define lg_global_lock spin_lock
  67. #define lg_global_unlock spin_unlock
  68. #endif
  69. #endif