免费注册 查看新帖 |

Chinaunix

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

[内核同步] completion 与 semaphore 相比究竟有什么优点? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-10-05 10:37 |只看该作者 |倒序浏览
看了好多关于semphore completion的描述中,似乎都是要A线程 要等待 B线程工作完成后 才能继续执行。 不太理解书上为何讲使用completion 比 semaphore 更好?  求指点。

论坛徽章:
1
15-16赛季CBA联赛之辽宁
日期:2016-07-06 16:53:09
2 [报告]
发表于 2014-10-05 12:30 |只看该作者
我猜semaphore和mutex一样都是同步用的,开销应该要比completion机制大一些。

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
3 [报告]
发表于 2014-10-07 09:48 |只看该作者
http://stackoverflow.com/questio ... bles-and-semaphores
有非常详细的说明。连引入的历史都给你了。

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
4 [报告]
发表于 2014-10-07 09:49 |只看该作者
There are two reasons you might want to use a completion instead of a semaphore. First, multiple threads can wait for a completion, and they can all be released with one call to complete_all(). It's more complex to have a semaphore wake up an unknown number of threads.

Second, if the waiting thread is going to deallocate the synchronization object, there is a race condition if you're using semaphores. That is, the waiter might get woken up and deallocate the object before the waking thread is done with up(). This race doesn't exist for completions. (See Lasse's post.)

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
5 [报告]
发表于 2014-10-07 09:49 |只看该作者
Explanation of why completions were originally implemented: http://lkml.indiana.edu/hypermail/linux/kernel/0107.3/0674.html

The basic summary is that we had this (fairly common) way of waiting for certain events by having a locked semaphore on the stack of the waiter, and then having the waiter do a "down()" which caused it to block until the thing it was waiting for did an "up()".

This works fairly well, but it has a really small (and quite unlikely) race on SMP, that is not so much a race of the idea itself, as of the implementation of the semaphores. We could have fixed the semaphores, but there were a few reasons not to:

the semaphores are optimized (on purpose) for the non-contention case. The "wait for completion" usage has the opposite default case
the semaphores are quite involved and architecture-specific, exactly
due to this optimization. Trying to change them is painful as hell.
So instead, I introduced the notion of "wait for completion":

More recent thread about completions vs semaphores http://lkml.org/lkml/2008/4/11/323

论坛徽章:
7
丑牛
日期:2013-10-18 14:43:21技术图书徽章
日期:2013-11-03 09:58:03辰龙
日期:2014-01-15 22:57:50午马
日期:2014-09-15 07:04:39丑牛
日期:2014-10-16 14:25:222015年亚洲杯之伊朗
日期:2015-03-16 10:24:352015亚冠之城南
日期:2015-05-31 09:52:32
6 [报告]
发表于 2014-10-08 21:34 |只看该作者
回复 4# Tinnal
That is, the waiter might get woken up and deallocate the object before the waking thread is done with up().
这句好难理解。


   

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
7 [报告]
发表于 2014-10-08 22:04 |只看该作者
被woken up的线程可能立即把对应的锁的空间释放,但waking 它的线程的up函数需要访问这个空间,这样就会崩溃。内核的代码里有大量的completions都是定义成局部变量的,如果改为信号量,就会出现这种情况。有个CSDN的网友也进行了说明,见:
http://blog.csdn.net/dreamxu/article/details/5866593

同时,completion比semaphore要轻量运行得快,他两个有一个本质的差别。
一个是有信号量,有数量的概论,数量不够了才睡。
一个是完成量,大家都等同一条件,没有数量的差异。

论坛徽章:
7
丑牛
日期:2013-10-18 14:43:21技术图书徽章
日期:2013-11-03 09:58:03辰龙
日期:2014-01-15 22:57:50午马
日期:2014-09-15 07:04:39丑牛
日期:2014-10-16 14:25:222015年亚洲杯之伊朗
日期:2015-03-16 10:24:352015亚冠之城南
日期:2015-05-31 09:52:32
8 [报告]
发表于 2014-10-10 01:35 |只看该作者
本帖最后由 smalloc 于 2014-10-10 01:38 编辑

回复 7# Tinnal


    CSDN上这个解释非常怪异,
谈的大致流程可认为:
B down 获得semaphore
A down sleep
B up start
A down
A up
B up end

也就是认为B up end 有段代码在A down up后还会去访问semaphore.

但从代码看显然不是这样。所有访问semaphore的操作都是用 spinlock_irq保护的原子操作。
  1. void up(struct semaphore *sem)
  2. {
  3.         unsigned long flags;

  4.         spin_lock_irqsave(&sem->lock, flags);
  5.         if (likely(list_empty(&sem->wait_list)))
  6.                 sem->count++;
  7.         else
  8.                 __up(sem);
  9.         spin_unlock_irqrestore(&sem->lock, flags);
  10. }
复制代码
这意味着从全局域角度看时序,A的down 完成也只能在B up之后

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
9 [报告]
发表于 2014-10-10 08:49 |只看该作者
回复 8# smalloc


技术是不断在往前发展的。要从历史的角度去看代问题,所有事情事出必有因。

引入completion时(应该是2.4.7版本)的semaphore确实存在问题,问题模型为:
http://lkml.iu.edu//hypermail/linux/kernel/0107.3/0674.html,google上应该还有其它的邮件。
那时的semaphore是没有锁保护的。到少我手上2.6.24的源码里就没有。

现在semaphore已经没有问题了。甚至有人还提议用semaphore从新实现completion:
http://lwn.net/Articles/277621/
从git来看,最终社区并没有接纳。


现在高版本的semaphore 已经不存在问题了。只是从概念层面,completion 和semaphore 应该被用来不同的场合,completion 是用来等待一个条件成力,而semaphore应该是等待一个资源。条件是没有数量的,而资源是有数量的。
从实现来说,completion更轻(我没有验正过)
请在http://www.makelinux.net/ldd3/chp-5-sect-4里搜lightweight。

论坛徽章:
0
10 [报告]
发表于 2016-04-14 17:34 |只看该作者
这样的讨论真的很有价值 么么哒
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP