同步_一个执行单元等待另一个执行单元执行完某事
2014-02-12 16:51
162 查看
2008年07月29日 星期二 21:46
Linux系统提供了一种比信号量更好的同步机制,即completion,它用于一个执行单元等待另一个执行单元执行完某事。Linux系统中与completion相关的操作主要有以下4种:
(1) 定义completion
struct completion my_completion;
(2) 初始化completion
init_completion(&my_completion);
对my_completion的定义和初始化可以通过如下快捷方式实现
DECLEARE_COMPLETION(my_completion);
(3) 等待completion
void wait_for_completion(struct completion *c);
(4) 唤醒completion
void complete(struct completion *c);
void complete_all(struct completion *c);
前者只唤醒一个等待的执行单元,后者唤醒所有等待同一completion的执行单元。
执行单元A 执行单元B
struct completion com;
init_completion(&com);
wake up
wait_for_completion(&com);
kernel_thread.c
---------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include // for DECLARE_COMPLETION()
#include // for daemonize() and set_current_state()
#include // mdelay()
static pid_t thread_id;
static DECLARE_COMPLETION(my_completion);
int my_fuction(void *arg)
{
printk(" in %s()\n", __FUNCTION__);
daemonize("demo-thread");
allow_signal(SIGKILL);
mdelay(2000);
printk(" my_function complete()\n");
complete(&my_completion); // wake up wait_for_completion
while (!signal_pending(current)) { // no signal
printk(" jiffies is %lu\n", jiffies);
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(HZ * 5);
}
return 0;
}
static int __init init(void)
{
thread_id = kernel_thread(my_fuction, NULL, CLONE_FS | CLONE_FILES);
printk(" init wait_for_completion()\n");
wait_for_completion(&my_completion);
return 0;
}
static void __exit finish(void)
{
kill_proc(thread_id, SIGKILL, 1);
printk(" Goodbye\n");
}
module_init(init);
module_exit(finish);
MODULE_LICENSE("GPL");
Makefile
---------------------------------------
KDIR=/usr/src/kernels/2.6.23.1-42.fc8-i686
obj-m += kernel_thread.o
all:
make -C $(KDIR) M=`pwd` modules
clean:
make -C $(KDIR) M=`pwd` clean
注意:spin_lock_irq(&x->wait.lock)和spin_unlock_irq(&x->wait.lock)并非真正对应的一对自旋锁,因为在自旋锁保护中是不允许休眠和调度的。与他们相对应的解锁和上锁操作在do_wait_for_common(x, timeout, state)函数内部。
函数完成的操作:声明等待队列,在队列末尾添加信号量,设置当前任务为TASK_UNINTERRUPTIBLE,释放自旋锁,进行任务调度,(任务被激活)获取自旋锁,移除等待队列。最后返回上一层函数,在上一层函数中释放自旋锁。
函数直接进去MAX_SCHEDULE_TIMEOUT的情况,调用schedule()。
Linux系统提供了一种比信号量更好的同步机制,即completion,它用于一个执行单元等待另一个执行单元执行完某事。Linux系统中与completion相关的操作主要有以下4种:
(1) 定义completion
struct completion my_completion;
(2) 初始化completion
init_completion(&my_completion);
对my_completion的定义和初始化可以通过如下快捷方式实现
DECLEARE_COMPLETION(my_completion);
(3) 等待completion
void wait_for_completion(struct completion *c);
(4) 唤醒completion
void complete(struct completion *c);
void complete_all(struct completion *c);
前者只唤醒一个等待的执行单元,后者唤醒所有等待同一completion的执行单元。
执行单元A 执行单元B
struct completion com;
init_completion(&com);
wake up
wait_for_completion(&com);
kernel_thread.c
---------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include // for DECLARE_COMPLETION()
#include // for daemonize() and set_current_state()
#include // mdelay()
static pid_t thread_id;
static DECLARE_COMPLETION(my_completion);
int my_fuction(void *arg)
{
printk(" in %s()\n", __FUNCTION__);
daemonize("demo-thread");
allow_signal(SIGKILL);
mdelay(2000);
printk(" my_function complete()\n");
complete(&my_completion); // wake up wait_for_completion
while (!signal_pending(current)) { // no signal
printk(" jiffies is %lu\n", jiffies);
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(HZ * 5);
}
return 0;
}
static int __init init(void)
{
thread_id = kernel_thread(my_fuction, NULL, CLONE_FS | CLONE_FILES);
printk(" init wait_for_completion()\n");
wait_for_completion(&my_completion);
return 0;
}
static void __exit finish(void)
{
kill_proc(thread_id, SIGKILL, 1);
printk(" Goodbye\n");
}
module_init(init);
module_exit(finish);
MODULE_LICENSE("GPL");
Makefile
---------------------------------------
KDIR=/usr/src/kernels/2.6.23.1-42.fc8-i686
obj-m += kernel_thread.o
all:
make -C $(KDIR) M=`pwd` modules
clean:
make -C $(KDIR) M=`pwd` clean
/** * wait_for_completion: - waits for completion of a task * @x: holds the state of this particular completion * * This waits to be signaled for completion of a specific task. It is NOT * interruptible and there is no timeout. * * See also similar routines (i.e. wait_for_completion_timeout()) with timeout * and interrupt capability. Also see complete(). */ void __sched wait_for_completion(struct completion *x) { wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); }
static long __sched wait_for_common(struct completion *x, long timeout, int state) { might_sleep(); spin_lock_irq(&x->wait.lock); timeout = do_wait_for_common(x, timeout, state); spin_unlock_irq(&x->wait.lock); return timeout; }
注意:spin_lock_irq(&x->wait.lock)和spin_unlock_irq(&x->wait.lock)并非真正对应的一对自旋锁,因为在自旋锁保护中是不允许休眠和调度的。与他们相对应的解锁和上锁操作在do_wait_for_common(x, timeout, state)函数内部。
static inline long __sched do_wait_for_common(struct completion *x, long timeout, int state) { if (!x->done) { DECLARE_WAITQUEUE(wait, current); wait.flags |= WQ_FLAG_EXCLUSIVE; __add_wait_queue_tail(&x->wait, &wait); do { if (signal_pending_state(state, current)) { timeout = -ERESTARTSYS; break; } __set_current_state(state); spin_unlock_irq(&x->wait.lock); timeout = schedule_timeout(timeout); spin_lock_irq(&x->wait.lock); } while (!x->done && timeout); __remove_wait_queue(&x->wait, &wait); if (!x->done) return timeout; } x->done--; return timeout ?: 1; }
函数完成的操作:声明等待队列,在队列末尾添加信号量,设置当前任务为TASK_UNINTERRUPTIBLE,释放自旋锁,进行任务调度,(任务被激活)获取自旋锁,移除等待队列。最后返回上一层函数,在上一层函数中释放自旋锁。
/** * schedule_timeout - sleep until timeout * @timeout: timeout value in jiffies * * Make the current task sleep until @timeout jiffies have * elapsed. The routine will return immediately unless * the current task state has been set (see set_current_state()). * * You can set the task state as follows - * * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to * pass before the routine returns. The routine will return 0 * * %TASK_INTERRUPTIBLE - the routine may return early if a signal is * delivered to the current task. In this case the remaining time * in jiffies will be returned, or 0 if the timer expired in time * * The current task state is guaranteed to be TASK_RUNNING when this * routine returns. * * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule * the CPU away without a bound on the timeout. In this case the return * value will be %MAX_SCHEDULE_TIMEOUT. * * In all cases the return value is guaranteed to be non-negative. */ signed long __sched schedule_timeout(signed long timeout) { struct timer_list timer; unsigned long expire; switch (timeout) { case MAX_SCHEDULE_TIMEOUT: /* * These two special cases are useful to be comfortable * in the caller. Nothing more. We could take * MAX_SCHEDULE_TIMEOUT from one of the negative value * but I' d like to return a valid offset (>=0) to allow * the caller to do everything it want with the retval. */ schedule(); goto out; default: /* * Another bit of PARANOID. Note that the retval will be * 0 since no piece of kernel is supposed to do a check * for a negative retval of schedule_timeout() (since it * should never happens anyway). You just have the printk() * that will tell you if something is gone wrong and where. */ if (timeout < 0) { printk(KERN_ERR "schedule_timeout: wrong timeout " "value %lx\n", timeout); dump_stack(); current->state = TASK_RUNNING; goto out; } } expire = timeout + jiffies; setup_timer_on_stack(&timer, process_timeout, (unsigned long)current); __mod_timer( 4000 &timer, expire, false, TIMER_NOT_PINNED); schedule(); del_singleshot_timer_sync(&timer); /* Remove the timer from the object tracker */ destroy_timer_on_stack(&timer); timeout = expire - jiffies; out: return timeout < 0 ? 0 : timeout; }
函数直接进去MAX_SCHEDULE_TIMEOUT的情况,调用schedule()。
/* * schedule() is the main scheduler function. */ asmlinkage void __sched schedule(void) { struct task_struct *prev, *next; unsigned long *switch_count; struct rq *rq; int cpu; need_resched: preempt_disable(); cpu = smp_processor_id(); rq = cpu_rq(cpu); rcu_sched_qs(cpu); prev = rq->curr; switch_count = &prev->nivcsw; release_kernel_lock(prev); need_resched_nonpreemptible: schedule_debug(prev); if (sched_feat(HRTICK)) hrtick_clear(rq); spin_lock_irq(&rq->lock); update_rq_clock(rq); clear_tsk_need_resched(prev); if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { if (unlikely(signal_pending_state(prev->state, prev))) prev->state = TASK_RUNNING; else deactivate_task(rq, prev, 1); switch_count = &prev->nvcsw; } pre_schedule(rq, prev); if (unlikely(!rq->nr_running)) idle_balance(cpu, rq); put_prev_task(rq, prev); next = pick_next_task(rq); if (likely(prev != next)) { sched_info_switch(prev, next); perf_event_task_sched_out(prev, next, cpu); rq->nr_switches++; rq->curr = next; ++*switch_count; context_switch(rq, prev, next); /* unlocks the rq */ /* * the context switch might have flipped the stack from under * us, hence refresh the local variables. */ cpu = smp_processor_id(); rq = cpu_rq(cpu); } else spin_unlock_irq(&rq->lock); post_schedule(rq); if (unlikely(reacquire_kernel_lock(current) < 0)) goto need_resched_nonpreemptible; preempt_enable_no_resched(); if (need_resched()) goto need_resched; }
相关文章推荐
- java中一个线程等待另一个线程执行完后再执行
- Delphi技巧集六 (等待执行完一个外部程序再执行另一个程序)
- CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待
- CLR 无法从 COM 上下文 0x1a2740 转换为 COM 上下文 0x1a28b0,这种状态已持续 60 秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作
- Delphi技巧集六 (等待执行完一个外部程序再执行另一个程序)
- 异常信息:CLR无法从COM 上下文0x645e18 转换为COM上下文0x645f88,这种状态已持续60秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作.这种情况通常会影响到性能,甚至可能导致应用程序不响应或者使用的内存随时间不断累积
- CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
- 异常信息:CLR无法从COM 上下文0x645e18 转换为COM上下文0x645f88,这种状态已持续60秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作.这种情况通常会影响到
- 异常信息:CLR无法从COM 上下文0x645e18 转换为COM上下文0x645f88,这种状态已持续60秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作.这种情况通常会影响到
- JAVA多线程—CountDownLatch-一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
- dos下怎样实现一个程序执行完后自动执行另一个程序,不是批处理
- 一个等待Winexec执行完成后再继续执行后面代码的函数
- [Java 09 多线程] 线程是指一个进程在执行过程中可以产生更小的程序单元
- Monitor线程操作(当一个线程中处于等待状态时,另外一个线程来解锁它的等待状态继续执行下去)
- 多线程之join()方法---(Thread提供的让一个线程等待另一个线程完成的方法)
- java线程同步,等待,2线程切换轮流执行 笔记
- 一个动作执行完后在执行另一个精灵动作代码
- 多线程的一个问题(同步事件和等待句柄)转
- 前端踩坑小结:多个异步请求在同一个函数里面执行时的同步问题之promise的用法。
- 1.写多进程,一个父进程,两个子进程,一个运行ls –l, 另一个暂停5s,父进程先阻塞等待第一个子进程的结束,然后用非阻塞等另一个进程退出,收集到第二个子进程结束的信息,父进程就返回。