函数 mutex_init() / mutex_lock() / mutex_unlock()

1. 初始化互斥体 -- mutex_init();

2. 获得互斥体 -- mutex_lock();

3. 释放互斥体 -- mutex_unlock();

mutex不能使用在 中断的上下文 中。



1. mutex_init(), 注意mutex使用之前都需要先init

[html]  view plain  copy
  1. /**  
  2.  * mutex_init - initialize the mutex  
  3.  * @mutex: the mutex to be initialized  
  4.  *  
  5.  * Initialize the mutex to unlocked state.  
  6.  *  
  7.  * It is not allowed to initialize an already locked mutex.  
  8.  */  
  9. # define mutex_init(mutex) \  
  10. do {                            \  
  11.     static struct lock_class_key __key;     \  
  12.                             \  
  13.     __mutex_init((mutex), #mutex, &__key);      \  
  14. } while (0)  

[html]  view plain  copy
  1. void  
  2. __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)  
  3. {  
  4.     atomic_set(&lock->count, 1);  
  5.     spin_lock_init(&lock->wait_lock);  
  6.     INIT_LIST_HEAD(&lock->wait_list);  
  7.     mutex_clear_owner(lock);  
  8. #ifdef CONFIG_MUTEX_SPIN_ON_OWNER  
  9.     lock->spin_mlock = NULL;  
  10. #endif  
  11.   
  12.     debug_mutex_init(lock, name, key);  
  13. }  


2. mutex_lock(), 注意看注释说明,

a. 如果mutex已经被其他task获取,那么目前的task先sleep直到获取;

b. mutex不能被嵌套获取;上一个task释放mutex之后,才能被其他的task获取;

c. mutex要先被初始化才能使用;mutex正在使用过程中,其内存不能被释放;

[html]  view plain  copy
  1. /**  
  2.  * mutex_lock - acquire the mutex  
  3.  * @lock: the mutex to be acquired  
  4.  *  
  5.  * Lock the mutex exclusively for this task. If the mutex is not  
  6.  * available right now, it will sleep until it can get it.  
  7.  *  
  8.  * The mutex must later on be released by the same task that  
  9.  * acquired it. Recursive locking is not allowed. The task  
  10.  * may not exit without first unlocking the mutex. Also, kernel  
  11.  * memory where the mutex resides mutex must not be freed with  
  12.  * the mutex still locked. The mutex must first be initialized  
  13.  * (or statically defined) before it can be locked. memset()-ing  
  14.  * the mutex to 0 is not allowed.  
  15.  *  
  16.  * ( The CONFIG_DEBUG_MUTEXES .config option turns on debugging  
  17.  *   checks that will enforce the restrictions and will also do  
  18.  *   deadlock debugging. )  
  19.  *  
  20.  * This function is similar to (but not equivalent to) down().  
  21.  */  
  22. void __sched mutex_lock(struct mutex *lock)  
  23. {  
  24.     might_sleep();  
  25.     /*  
  26.      * The locking fastpath is the 1->0 transition from  
  27.      * 'unlocked' into 'locked' state.  
  28.      */  
  29.     __mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);  
  30.     mutex_set_owner(lock);  
  31. }  


3. mutex_unlock(), 释放互斥体

a. 释放之前获得的mutex;

b. mutex只有被获得,才能调用这个函数来释放;换言之,如果没有获得,就没有必要做释放的动作;

[html]  view plain  copy
  1. /**  
  2.  * mutex_unlock - release the mutex  
  3.  * @lock: the mutex to be released  
  4.  *  
  5.  * Unlock a mutex that has been locked by this task previously.  
  6.  *  
  7.  * This function must not be used in interrupt context. Unlocking  
  8.  * of a not locked mutex is not allowed.  
  9.  *  
  10.  * This function is similar to (but not equivalent to) up().  
  11.  */  
  12. void __sched mutex_unlock(struct mutex *lock)  
  13. {  
  14.     /*  
  15.      * The unlocking fastpath is the 0->1 transition from 'locked'  
  16.      * into 'unlocked' state:  
  17.      */  
  18. #ifndef CONFIG_DEBUG_MUTEXES  
  19.     /*  
  20.      * When debugging is enabled we must not clear the owner before time,  
  21.      * the slow path will always be taken, and that clears the owner field  
  22.      * after verifying that it was indeed current.  
  23.      */  
  24.     mutex_clear_owner(lock);  
  25. #endif  
  26.     __mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);  
  27. }  
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pthread_mutex_init: 用于初始化互斥锁(mutex),将互斥锁设置为默认属性。 ```c #include <pthread.h> int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); ``` pthread_cond_init: 用于初始化条件变量(condition variable),将条件变量设置为默认属性。 ```c #include <pthread.h> int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); ``` pthread_create: 用于创建一个新的线程,并执行指定的函数。 ```c #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg); ``` pthread_cond_signal: 用于唤醒一个等待在条件变量上的线程。 ```c #include <pthread.h> int pthread_cond_signal(pthread_cond_t *cond); ``` pthread_mutex_unlock: 用于解锁互斥锁,允许其他线程获取该互斥锁。 ```c #include <pthread.h> int pthread_mutex_unlock(pthread_mutex_t *mutex); ``` pthread_mutex_lock: 用于加锁互斥锁,如果互斥锁已经被锁定,则调用线程会阻塞直到互斥锁被解锁。 ```c #include <pthread.h> int pthread_mutex_lock(pthread_mutex_t *mutex); ``` 下面是一个使用这些函数的简单示例: ```c #include <stdio.h> #include <pthread.h> pthread_mutex_t mutex; pthread_cond_t cond; int count = 0; void* thread_func(void* arg) { pthread_mutex_lock(&mutex); // 检查条件是否满足 while (count < 10) { // 等待条件变量被唤醒 pthread_cond_wait(&cond, &mutex); } // 条件满足,执行任务 printf("Thread: Count is now %d\n", count); pthread_mutex_unlock(&mutex); return NULL; } int main() { pthread_t thread; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); // 创建线程 pthread_create(&thread, NULL, thread_func, NULL); // 模拟更新计数 for (int i = 0; i < 15; ++i) { pthread_mutex_lock(&mutex); count++; // 每次更新计数后,唤醒等待的线程 if (count >= 10) { pthread_cond_signal(&cond); } pthread_mutex_unlock(&mutex); } // 等待线程结束 pthread_join(thread, NULL); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); return 0; } ``` 在上面的示例中,主线程更新一个计数器,当计数器达到10时,会通过条件变量唤醒等待的线程。等待的线程在条件满足时执行任务,并输出计数器的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值