免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 777 | 回复: 0
打印 上一主题 下一主题

Concurrency and Race Condition in Linux 2.6 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-14 11:14 |只看该作者 |倒序浏览
Concurrency and Race Condition in Linux 2.6
   
   
   
   
  
  
    September 2, 2006 - 4:31am
  Submitted by
dynamo2
on September 2, 2006 - 4:31am.
  
  
   

  
  
Linux Device Driver Study Notes
Concurrency and Race Condition
Summary:
This document is a brief summary of reading Concurrent and Race Condition chapter of Linux Device Driver.
1        Mutual exclusion Operations
1.1        Semaphores
Semaphores in Linux system is used for mutual exclusion operations.
It needs to get semaphore firstly that thread want to come into the
critical section protected by semaphore. If semaphore has held by
another thread, thread want to come into critical section will to go to
sleep and this thread will be woke up by kernel later.
To use semaphores, code need include .
1.1.1        Create a Semaphore
struct semaphore sem;
void sema_init(struct semaphore *sem, int val)
void init_MUTEX(struct semaphore *sem)
void init_MUTEX_LOCKED(struct semaphore *sem)
DECLARE_MUTEX(name)
DECLARE_MUTEX_LOCKED(name)
1.1.2        Hold a Semaphore
void down(struct semaphore *sem)
It can be broke by interruption, when it is blocking for hold the “Semaphore”.
void down_interruptible(struct semaphore *sem)
It can not be broke by interruption, when it is blocking for hold the “Semaphore”.
void down_trylock(struct semaphore *sem)
It is not be blocked when request to hold a semaphore.
1.1.3        Release a Semaphore
void up(struct semaphore *sem)
1.2        Spin Lock
“Spinlock” is a lock that is used to mutual exclusion. It is different
from “Semaphore” that using “Spinlock” will not bring thread into
sleeping. It will run like a loop to check the lock status.
To use “Spinlock”, we need to include .
1.2.1        Create Spinlock
spinlock_t my_lock = SPIN_LOCK_UNLOCKED;
void spin_lock_init(spinlock_t *lock);
1.2.2        Hold a Spinlock
void spin_lock(spinlock_t *lock);
It cannot be broke by interruption during blocking for hold a lock. The
procedure in critical section that protected by Spinlock should not be
broke by interruption or be forced to go to sleep for any blocking
operations.
spin_lock_irqsave
Before into the critical section, it disables the IRQ and save the status of current IRQ.
spin_lock_irq
Before into the critical section, it disables the IRQ but not save the status of current IRQ.
spin_lock_bh
Before into the critical section, it disables the software IRQ but still enable the hardware IRQ.
spin_trylock
spin_trylock_bh
These two APIs are not block during the requesting of hold Spinlock.
1.2.3        Release a Spinlock
Void spin_unlock(spinlock_t *lock);
spin_unlock_irqrestore
spin_unlock_irq
spin_unlock_bh
2        Synchronization Operations
2.1        Completions
“Completions” in Linux is used to synchronize tasks between threads.
To use “Completions”, we need to include .
2.1.1        Create a “Completions”
DECLARE_COMPLETION(my_completion)
Struct completion my_completion;
Init_completion(&my_completion);
2.1.2        Wait for Completed
void wait_for_completion(struct completion *c)
It cannot be broke by interruption during the waiting for “Completion”.
Flowing APIs defined in Linux kernel version more than 2.6.11.
unsigned long wait_for_completion_timeout(struct completion *x, unsigned long timeout)
int wait_for_completion_interruptible(struct completion *x)
unsigned long wait_for_completion_interruptible_timeout(struct completion *x, unsigned long timeout)
2.1.3        Wake up waited threads
void complete(struct completion *c)
Wake up one thread that is waiting for.
void complete_all(struct completion *c)
Wake up all threads that are waiting for.
After call complete_all API, we need to reinitialize the “Completion” using INIT_COMPLETION.
2.2        Wait Queue
If want a process or thread go to sleep for waiting to get a special
condition, we can use “Wait Queue”. What difference between “Wait
Queue” and “Completion” is “Wait Queue” can check the condition before
or after sleep.
2.2.1        Create a “Wait Queue”
DECLARE_WAIT_QUEUE_HEAD(name)
wait_queue_head_t my_queue;
init_waitqueue_head(&my_queue);
2.2.2        Wait for expected condition
wait_event(queue, condition)
“queue” is the head of “Wait Queue”.  “condition” is an expression used to evaluate the condition.
wait_event_interruptable(queue, condition)
It can be broke during sleeping.
wait_event_timeout(queue, condition, timeout)
wait_event_interruptable_timeout(queue, condition, timeout)
2.2.3        Wake up waiting thread of process
wake_up(wait_queue_head_t *queue)
wake_up_interruptable(wait_queue_head_t *queue)
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/12880/showart_247149.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP