Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1587420
  • 博文数量: 77
  • 博客积分: 1205
  • 博客等级: 少尉
  • 技术积分: 4476
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-22 21:48
文章分类
文章存档

2018年(1)

2017年(1)

2015年(1)

2014年(18)

2013年(12)

2012年(44)

分类: AIX

2014-02-20 17:04:24

实验性的代码,保留了Linux kernel下的接口,实现用的是AIX的锁机制(暂未提供基于Complex lock的rw_lock的实现):


点击(此处)折叠或打开

  1. typedef struct spinlock{
  2.     Simple_lock lock;
  3.     int int_pri;
  4. }spinlock_t;

  5. struct mutex{
  6.     Simple_lock lock;
  7.     /* 1: unlocked, 0: locked, negative: locked, possible waiters */
  8.     atomic_t count;
  9. };

  10. static inline void __spin_lock_irq(spinlock_t *lockp)
  11. {
  12.     lockp->int_pri = disable_lock(INTMAX, &lockp->lock);
  13. }

  14. static inline void __spin_unlock_irq(spinlock_t *lockp)
  15. {
  16.     unlock_enable(lockp->int_pri, &lockp->lock);
  17. }

  18. #ifdef LOCK_DATA_INSTRUMENTATION

  19. #define __SIMPLE_LOCK_COMMON_INIT(_lockp) lock_alloc(_lockp, LOCK_ALLOC_PIN, OEM_CLASS_DEF, INSTANCE)

  20. #define mutex_destroy(_lockp)         lock_free(_lockp)
  21. #define spin_lock_destroy(_lockp)    mutex_destroy(_lockp)

  22. #else //LOCK_DATA_INSTRUMENTATION
  23. #define __SIMPLE_LOCK_COMMON_INIT(_lockp)
  24. #define mutex_destroy(_lockp)
  25. #define spin_lock_destroy(_lockp)
  26. #endif

  27. #define spin_lock_init(_lockp) \
  28. do { \
  29.     __SIMPLE_LOCK_COMMON_INIT(&(_lockp)->lock); \
  30.     simple_lock_init(&(_lockp)->lock); \
  31. } while (0)

  32. #define mutex_init(mutex) \
  33. do { \
  34.     __SIMPLE_LOCK_COMMON_INIT(&(mutex)->lock); \
  35.     simple_lock_init(&(mutex)->lock); \
  36.     atomic_set(&(mutex)->count, 1); \
  37. } while (0)

  38. /* define and initialize a spinlock statically, don't call spin_lock_destroy upon this lock var */
  39. #define DEFINE_SPINLOCK(x) spinlock_t x = {.lock = {SIMPLE_LOCK_AVAIL}}

  40. /* define and initialize a mutex statically, don't call mutex_destroy upon this lock var */
  41. #define DEFINE_MUTEX(mutexname) \
  42.         struct mutex mutexname = \
  43.         {.count = ATOMIC_INIT(1), \
  44.          .lock = {SIMPLE_LOCK_AVAIL}}

  45. static inline void mutex_lock(struct mutex *lock)
  46. {
  47.         MUTEX_LOCK_TRACE(lock);
  48.     simple_lock(&lock->lock);
  49.     atomic_dec(&lock->count);
  50. }

  51. static inline void mutex_unlock(struct mutex *lock)
  52. {
  53.         MUTEX_UNLOCK_TRACE(lock);
  54.     atomic_inc(&lock->count);
  55.     simple_unlock(&lock->lock);
  56. }

  57. static inline bool mutex_trylock(struct mutex *lock)
  58. {
  59.     bool bret = simple_lock_try(&lock->lock);
  60.     if (bret)
  61.         atomic_dec(&lock->count);

  62.     return bret;
  63. }

  64. #define spin_lock(lockp)        simple_lock(&(lockp)->lock)
  65. #define spin_unlock(lockp)        simple_unlock(&(lockp)->lock)

  66. #define spin_lock_irq(lockp)         __spin_lock_irq(lockp)
  67. #define spin_unlock_irq(lockp)         __spin_unlock_irq(lockp)

  68. /**
  69.  * mutex_is_locked - is the mutex locked
  70.  * @lock: the mutex to be queried
  71.  *
  72.  * Returns 1 if the mutex is locked, 0 if unlocked.
  73.  */
  74. static inline int mutex_is_locked(struct mutex *lock)
  75. {
  76.     return atomic_read(&lock->count) != 1;
  77. }


阅读(3485) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~