mutex_init() / mutex_lock() / mutex_unlock()

请求

1). 初始化互斥体 -- mutex_init();
2). 获得互斥体 -- mutex_lock();
3). 释放互斥体 -- mutex_unlock();

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

void
__mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
{
        atomic_long_set(&lock->owner, 0);
        spin_lock_init(&lock->wait_lock);
        INIT_LIST_HEAD(&lock->wait_list);
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
        osq_lock_init(&lock->osq);
#endif

        debug_mutex_init(lock, name, key);
}
EXPORT_SYMBOL(__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)

2.mutex_lock(), 注意看注释说明,
a). 如果mutex已经被其他task获取,那么目前的task先sleep直到获取;
b). mutex不能被嵌套获取;上一个task释放mutex之后,才能被其他的task获取;
c). mutex要先被初始化才能使用;mutex正在使用过程中,其内存不能被释放;

//------kernel4.4------
/**
 * 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 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);
}

//======kernel4.14======
/**
 * 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 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();
                
        if (!__mutex_trylock_fast(lock))
                __mutex_lock_slowpath(lock);
}
EXPORT_SYMBOL(mutex_lock);

3. mutex_unlock(), 释放互斥体
a). 释放之前获得的mutex;
b). mutex只有被获得,才能调用这个函数来释放;换言之,如果没有获得,就没有必要做释放的动作;

//======kernel 4.4======
/**
 * 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);
}

EXPORT_SYMBOL(mutex_unlock);

//=======kernel 4.14======
/**     
 * 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)
{
#ifndef CONFIG_DEBUG_LOCK_ALLOC
        if (__mutex_unlock_fast(lock))
                return;
#endif
        __mutex_unlock_slowpath(lock, _RET_IP_);
}       
EXPORT_SYMBOL(mutex_unlock);

 

如果lock只在interrupt handler里使用,则没有必要加。中断handler本身就是不可重入的,不存在竞争。 

static irqreturn_t xxxx_typec_interrupt(int irq, void *data)
{
        struct xxxx_typec *sc = data;
        u32 event;
        int ret;

        //mutex_lock(&sc->lock);
        ret = regmap_read(sc->regmap, XXXX_INT_MASK, &event);
        if (ret)
                goto unlock;

        event &= XXXX_EVENT_MASK;

        ret = regmap_read(sc->regmap, XXXX_STATUS, &sc->state);
        if (ret)
                goto clear_ints;

        sc->state &= XXXX_STATE_MASK;

        if (event & XXXX_ATTACH_INT) {
                ret = XXxx_typec_connect(sc, sc->state);
                if (ret)
                        dev_warn(sc->dev, "failed to register partner\n");
        } else if (event & XXXX_DETACH_INT) {
                xxxx_typec_disconnect(sc, sc->state);
        }

clear_ints:
        regmap_write(sc->regmap, XXXX_INT_CLR, event);
unlock:
        //mutex_unlock(&sc->lock);

        dev_info(sc->dev, "now works as DRP and is in %d state, event %d\n",
                sc->state, event);
        return IRQ_HANDLED;
}


static int xxxx_typec_probe(struct platform_device *pdev)
{
        struct xxxx_typec *sc;
        int ret;

        struct device *dev = &pdev->dev;
        struct device_node *node = pdev->dev.of_node;
        u32 value = 0;

        sc = devm_kzalloc(&pdev->dev, sizeof(*sc), GFP_KERNEL);
        if (!sc)
                return -ENOMEM;

        sc->edev = devm_extcon_dev_allocate(&pdev->dev, xxxx_typec_cable);
        if (IS_ERR(sc->edev)) {
                dev_err(&pdev->dev, "failed to allocate extcon device\n");
                return PTR_ERR();
        }

        ret = devm_extcon_dev_register(&pdev->dev, sc->edev);
        if (ret < 0) {
                dev_err(&pdev->dev, "can't register extcon device: %d\n", ret);
                return ret;
        }

        //mutex_init(&sc->lock);
        sc->dev = &pdev->dev;
        sc->irq = platform_get_irq(pdev, 0);
        ...
        ret = devm_request_threaded_irq(sc->dev, sc->irq, NULL,
                                        xxxx_typec_interrupt,
                                        IRQF_EARLY_RESUME | IRQF_ONESHOT,
                                        dev_name(sc->dev), sc);
        ...
}

 

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

余额充值