前一篇文章讲的是completion信号量,该机制不能用于中断上下文中,因为该锁可睡眠。
接下来,介绍的锁机制也是一个可以睡眠的锁——互斥锁,所以该锁也不能用于中断上下文中;
1、头文件:include/linux/mutex.h
结构体:
48 struct mutex { 49 /* 1: unlocked, 0: locked, negative: locked, possible waiters */ 50 atomic_t count; 51 spinlock_t wait_lock; 52 struct list_head wait_list; 53 #if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP) 54 struct task_struct *owner; 55 #endif 56 #ifdef CONFIG_DEBUG_MUTEXES 57 const char *name; 58 void *magic; 59 #endif 60 #ifdef CONFIG_DEBUG_LOCK_ALLOC 61 struct lockdep_map dep_map; 62 #endif 63 }; 64
2、定义及其初始化:
89 # define mutex_init(mutex) \ 90 do { \ 91 static struct lock_class_key __key; \ 92 \ 93 __mutex_init((mutex), #mutex, &__key); \ 94 } while (0)
3、加互斥锁:
152 extern void mutex_lock(struct mutex *lock);
4、释放互斥锁
169 extern void mutex_unlock(struct mutex *lock);
前面所说的,该所可用于可睡眠的线程中,所以该锁机制不能用于中断上下文,切记!!
浙公网安备 33010602011771号