lockhelp.h
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:4k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. #ifndef _LOCKHELP_H
  2. #define _LOCKHELP_H
  3. #include <linux/config.h>
  4. #include <linux/spinlock.h>
  5. #include <asm/atomic.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/smp.h>
  8. /* Header to do help in lock debugging. */
  9. #ifdef CONFIG_NETFILTER_DEBUG
  10. struct spinlock_debug
  11. {
  12. spinlock_t l;
  13. atomic_t locked_by;
  14. };
  15. struct rwlock_debug
  16. {
  17. rwlock_t l;
  18. long read_locked_map;
  19. long write_locked_map;
  20. };
  21. #define DECLARE_LOCK(l) 
  22. struct spinlock_debug l = { SPIN_LOCK_UNLOCKED, ATOMIC_INIT(-1) }
  23. #define DECLARE_LOCK_EXTERN(l) 
  24. extern struct spinlock_debug l
  25. #define DECLARE_RWLOCK(l)
  26. struct rwlock_debug l = { RW_LOCK_UNLOCKED, 0, 0 }
  27. #define DECLARE_RWLOCK_EXTERN(l)
  28. extern struct rwlock_debug l
  29. #define MUST_BE_LOCKED(l)
  30. do { if (atomic_read(&(l)->locked_by) != smp_processor_id())
  31. printk("ASSERT %s:%u %s unlockedn", __FILE__, __LINE__, #l);
  32. } while(0)
  33. #define MUST_BE_UNLOCKED(l)
  34. do { if (atomic_read(&(l)->locked_by) == smp_processor_id())
  35. printk("ASSERT %s:%u %s lockedn", __FILE__, __LINE__, #l);
  36. } while(0)
  37. /* Write locked OK as well. */     
  38. #define MUST_BE_READ_LOCKED(l)     
  39. do { if (!((l)->read_locked_map & (1 << smp_processor_id()))     
  40.  && !((l)->write_locked_map & (1 << smp_processor_id())))     
  41. printk("ASSERT %s:%u %s not readlockedn", __FILE__, __LINE__, #l); 
  42. } while(0)
  43. #define MUST_BE_WRITE_LOCKED(l)      
  44. do { if (!((l)->write_locked_map & (1 << smp_processor_id())))      
  45. printk("ASSERT %s:%u %s not writelockedn", __FILE__, __LINE__, #l); 
  46. } while(0)
  47. #define MUST_BE_READ_WRITE_UNLOCKED(l)   
  48. do { if ((l)->read_locked_map & (1 << smp_processor_id()))   
  49. printk("ASSERT %s:%u %s readlockedn", __FILE__, __LINE__, #l);   
  50.  else if ((l)->write_locked_map & (1 << smp_processor_id()))   
  51.  printk("ASSERT %s:%u %s writelockedn", __FILE__, __LINE__, #l); 
  52. } while(0)
  53. #define LOCK_BH(lk)
  54. do {
  55. MUST_BE_UNLOCKED(lk);
  56. spin_lock_bh(&(lk)->l);
  57. atomic_set(&(lk)->locked_by, smp_processor_id());
  58. } while(0)
  59. #define UNLOCK_BH(lk)
  60. do {
  61. MUST_BE_LOCKED(lk);
  62. atomic_set(&(lk)->locked_by, -1);
  63. spin_unlock_bh(&(lk)->l);
  64. } while(0)
  65. #define READ_LOCK(lk) 
  66. do {
  67. MUST_BE_READ_WRITE_UNLOCKED(lk);
  68. read_lock_bh(&(lk)->l);
  69. set_bit(smp_processor_id(), &(lk)->read_locked_map);
  70. } while(0)
  71. #define WRITE_LOCK(lk)   
  72. do {   
  73. MUST_BE_READ_WRITE_UNLOCKED(lk);   
  74. write_lock_bh(&(lk)->l);   
  75. set_bit(smp_processor_id(), &(lk)->write_locked_map);   
  76. } while(0)
  77. #define READ_UNLOCK(lk)
  78. do {
  79. if (!((lk)->read_locked_map & (1 << smp_processor_id())))
  80. printk("ASSERT: %s:%u %s not readlockedn", 
  81.        __FILE__, __LINE__, #lk);
  82. clear_bit(smp_processor_id(), &(lk)->read_locked_map);
  83. read_unlock_bh(&(lk)->l);
  84. } while(0)
  85. #define WRITE_UNLOCK(lk)
  86. do {
  87. MUST_BE_WRITE_LOCKED(lk);
  88. clear_bit(smp_processor_id(), &(lk)->write_locked_map);
  89. write_unlock_bh(&(lk)->l);
  90. } while(0)
  91. #else
  92. #define DECLARE_LOCK(l) spinlock_t l = SPIN_LOCK_UNLOCKED
  93. #define DECLARE_LOCK_EXTERN(l) extern spinlock_t l
  94. #define DECLARE_RWLOCK(l) rwlock_t l = RW_LOCK_UNLOCKED
  95. #define DECLARE_RWLOCK_EXTERN(l) extern rwlock_t l
  96. #define MUST_BE_LOCKED(l)
  97. #define MUST_BE_UNLOCKED(l)
  98. #define MUST_BE_READ_LOCKED(l)
  99. #define MUST_BE_WRITE_LOCKED(l)
  100. #define MUST_BE_READ_WRITE_UNLOCKED(l)
  101. #define LOCK_BH(l) spin_lock_bh(l)
  102. #define UNLOCK_BH(l) spin_unlock_bh(l)
  103. #define READ_LOCK(l) read_lock_bh(l)
  104. #define WRITE_LOCK(l) write_lock_bh(l)
  105. #define READ_UNLOCK(l) read_unlock_bh(l)
  106. #define WRITE_UNLOCK(l) write_unlock_bh(l)
  107. #endif /*CONFIG_NETFILTER_DEBUG*/
  108. #endif /* _LOCKHELP_H */