博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(ARM v7)信号量、互斥体代码追踪
阅读量:6642 次
发布时间:2019-06-25

本文共 3068 字,大约阅读时间需要 10 分钟。

void down(struct semaphore *sem){    unsigned long flags;    raw_spin_lock_irqsave(&sem->lock, flags);    if (likely(sem->count > 0))        sem->count--;    else        __down(sem);    raw_spin_unlock_irqrestore(&sem->lock, flags);}static noinline void __sched __down(struct semaphore *sem){    __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);}static inline int __sched __down_common(struct semaphore *sem, long state,                                long timeout){    struct task_struct *task = current;    struct semaphore_waiter waiter;    list_add_tail(&waiter.list, &sem->wait_list);    waiter.task = task;    waiter.up = false;    for (;;) {        if (signal_pending_state(state, task))            goto interrupted;        if (unlikely(timeout <= 0))            goto timed_out;        __set_task_state(task, state);        raw_spin_unlock_irq(&sem->lock);        timeout = schedule_timeout(timeout);        raw_spin_lock_irq(&sem->lock);        if (waiter.up)            return 0;    } timed_out:    list_del(&waiter.list);    return -ETIME; interrupted:    list_del(&waiter.list);    return -EINTR;}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);}# define might_sleep() do { might_resched(); } while (0)# define might_resched() do { } while (0)static inline void__mutex_fastpath_lock(atomic_t *count, void (*fail_fn)(atomic_t *)){    if (unlikely(atomic_dec_return(count) < 0))        fail_fn(count);}#define atomic_dec_return(v)        atomic_sub_return(1, (v))ATOMIC_OP_RETURN(sub, -)#define ATOMIC_OP_RETURN(op, c_op, asm_op)                \static inline int atomic_##op##_return(int i, atomic_t *v)        \{                                    \    unsigned long tmp;                        \    int result;                            \                                    \    smp_mb();                            \    prefetchw(&v->counter);                        \                                    \    __asm__ __volatile__("@ atomic_" #op "_return\n"        \"1:    ldrex    %0, [%3]\n"                        \"    " #asm_op "    %0, %0, %4\n"                    \"    strex    %1, %0, [%3]\n"                        \"    teq    %1, #0\n"                        \"    bne    1b"                            \    : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)        \    : "r" (&v->counter), "Ir" (i)                    \    : "cc");                            \                                    \    smp_mb();                            \                                    \    return result;                            \}__visible void __sched__mutex_lock_slowpath(atomic_t *lock_count){    struct mutex *lock = container_of(lock_count, struct mutex, count);    __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0,                NULL, _RET_IP_, NULL, 0);}
View Code

 

转载于:https://www.cnblogs.com/zero-jh/p/5079130.html

你可能感兴趣的文章
【295】暗黑表格模板及相关
查看>>
mysql group replication 安装&配置详解
查看>>
深拷贝和浅拷贝
查看>>
java版sqlhelper(转)
查看>>
android搭建环境错误 daemon not running. starting it now on port 5037 ADB server didn't ACK
查看>>
我的第一本著作:Spark技术内幕上市!
查看>>
现实世界的Windows Azure:采访Gridsum的Sr.业务发展总监Yun Xu
查看>>
公开发布版的Windows Azure 基础结构服务中的 SQL Server – 文档和最佳实践(已更新),还有即将发布的博客...
查看>>
UVa 494 - Kindergarten Counting Game
查看>>
java中IO操作
查看>>
Python 值传递和引用传递
查看>>
hdu4405Aeroplane chess 概率dp水题
查看>>
jq查找父类元素三个函数的区别
查看>>
1.27eia原油
查看>>
vue loading 插件编写与实战
查看>>
Linux I/O多路转接之select函数
查看>>
Android深度探索第二章总结
查看>>
matlab练习程序(单源最短路径Bellman-Ford)
查看>>
深入理解Java的接口和抽象类
查看>>
JavaScript 简介
查看>>