lglock.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /* can make br locks by using local lock for read side, global lock for write */
  27. #define br_lock_init(name) lg_lock_init(name, #name)
  28. #define br_read_lock(name) lg_local_lock(name)
  29. #define br_read_unlock(name) lg_local_unlock(name)
  30. #define br_write_lock(name) lg_global_lock(name)
  31. #define br_write_unlock(name) lg_global_unlock(name)
  32. #define DEFINE_BRLOCK(name) DEFINE_LGLOCK(name)
  33. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  34. #define LOCKDEP_INIT_MAP lockdep_init_map
  35. #define DEFINE_LGLOCK_LOCKDEP(name) \
  36. struct lock_class_key name##_lock_key; \
  37. struct lockdep_map name##_lock_dep_map; \
  38. EXPORT_SYMBOL(name##_lock_dep_map)
  39. #else
  40. #define LOCKDEP_INIT_MAP(a, b, c, d)
  41. #define DEFINE_LGLOCK_LOCKDEP(name)
  42. #endif
  43. struct lglock {
  44. arch_spinlock_t __percpu *lock;
  45. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  46. struct lock_class_key lock_key;
  47. struct lockdep_map lock_dep_map;
  48. #endif
  49. };
  50. #define DEFINE_LGLOCK(name) \
  51. DEFINE_LGLOCK_LOCKDEP(name); \
  52. DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  53. = __ARCH_SPIN_LOCK_UNLOCKED; \
  54. struct lglock name = { .lock = &name ## _lock }
  55. void lg_lock_init(struct lglock *lg, char *name);
  56. void lg_local_lock(struct lglock *lg);
  57. void lg_local_unlock(struct lglock *lg);
  58. void lg_local_lock_cpu(struct lglock *lg, int cpu);
  59. void lg_local_unlock_cpu(struct lglock *lg, int cpu);
  60. void lg_global_lock(struct lglock *lg);
  61. void lg_global_unlock(struct lglock *lg);
  62. #endif