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

12 篇文章 1 订阅

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

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

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

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



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

/**
 * mutex_init - initialize the mutex
 * @mutex: the mutex to be initialized
 *
 * Initialize the mutex to unlocked state.
 *
 * It is not allowed to initialize an already locked mutex.
 */
# define mutex_init(mutex) \
do {							\
	static struct lock_class_key __key;		\
							\
	__mutex_init((mutex), #mutex, &__key);		\
} while (0)

void
__mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
{
	atomic_set(&lock->count, 1);
	spin_lock_init(&lock->wait_lock);
	INIT_LIST_HEAD(&lock->wait_list);
	mutex_clear_owner(lock);
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
	lock->spin_mlock = NULL;
#endif

	debug_mutex_init(lock, name, key);
}


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

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

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

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

/**
 * mutex_lock - acquire the mutex
 * @lock: the mutex to be acquired
 *
 * Lock the mutex exclusively for this task. If the mutex is not
 * available right now, it will sleep until it can get it.
 *
 * The mutex must later on be released by the same task that
 * acquired it. Recursive locking is not allowed. The task
 * may not exit without first unlocking the mutex. Also, kernel
 * memory where the mutex resides mutex must not be freed with
 * the mutex still locked. The mutex must first be initialized
 * (or statically defined) before it can be locked. memset()-ing
 * the mutex to 0 is not allowed.
 *
 * ( The CONFIG_DEBUG_MUTEXES .config option turns on debugging
 *   checks that will enforce the restrictions and will also do
 *   deadlock debugging. )
 *
 * This function is similar to (but not equivalent to) down().
 */
void __sched mutex_lock(struct mutex *lock)
{
	might_sleep();
	/*
	 * The locking fastpath is the 1->0 transition from
	 * 'unlocked' into 'locked' state.
	 */
	__mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);
	mutex_set_owner(lock);
}


3. mutex_unlock(), 释放互斥体

a. 释放之前获得的mutex;

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

/**
 * mutex_unlock - release the mutex
 * @lock: the mutex to be released
 *
 * Unlock a mutex that has been locked by this task previously.
 *
 * This function must not be used in interrupt context. Unlocking
 * of a not locked mutex is not allowed.
 *
 * This function is similar to (but not equivalent to) up().
 */
void __sched mutex_unlock(struct mutex *lock)
{
	/*
	 * The unlocking fastpath is the 0->1 transition from 'locked'
	 * into 'unlocked' state:
	 */
#ifndef CONFIG_DEBUG_MUTEXES
	/*
	 * When debugging is enabled we must not clear the owner before time,
	 * the slow path will always be taken, and that clears the owner field
	 * after verifying that it was indeed current.
	 */
	mutex_clear_owner(lock);
#endif
	__mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
}



  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值