請問有推薦實用高效的 C coroutine 實現嗎?

32 views
Skip to first unread message

DaboD

unread,
Jul 2, 2015, 1:47:56 AM7/2/15
to sh...@googlegroups.com
如題, google 了一些, 似乎有各自的缺點, 想配合像是 lib[ev|event|uv] 使用.

Chaos Eternal

unread,
Jul 2, 2015, 2:22:40 AM7/2/15
to sh...@googlegroups.com
自己撸一套吧?

On Thu, Jul 2, 2015 at 1:47 PM, DaboD <daid...@gmail.com> wrote:
> 如題, google 了一些, 似乎有各自的缺點, 想配合像是 lib[ev|event|uv] 使用.
>
> --
> -- You received this message because you are subscribed to the Google Groups
> Shanghai Linux User Group group. To post to this group, send email to
> sh...@googlegroups.com. To unsubscribe from this group, send email to
> shlug+un...@googlegroups.com. For more options, visit this group at
> https://groups.google.com/d/forum/shlug?hl=zh-CN
> ---
> 您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
> 要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
> 要查看更多选项,请访问https://groups.google.com/d/optout

Shell Xu

unread,
Jul 2, 2015, 8:25:59 AM7/2/15
to shlug
没有。
底层你要么用ucontext,要么用setjmp/longjmp。哪个都有点问题的。

On Thu, Jul 2, 2015 at 1:47 PM, DaboD <daid...@gmail.com> wrote:
如題, google 了一些, 似乎有各自的缺點, 想配合像是 lib[ev|event|uv] 使用.

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout



--
彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
blog: http://shell909090.org/blog/

Yang Fan

unread,
Jul 2, 2015, 8:48:39 PM7/2/15
to sh...@googlegroups.com
OT一下,换用coroutine真的会有很大的各种好处么?

之前看到boost都有coroutine的库了,一直比较怀疑其实用价值。
Regards,
Fan Yang

loong0

unread,
Jul 2, 2015, 9:45:22 PM7/2/15
to sh...@googlegroups.com
Portable Coroutine Library (PCL)  http://xmailserver.org/libpcl.html
Follow your dream!

DaboD

unread,
Jul 2, 2015, 11:03:54 PM7/2/15
to sh...@googlegroups.com
多謝, 來研究一下.


On Friday, July 3, 2015 at 9:45:22 AM UTC+8, loong0 wrote:
Portable Coroutine Library (PCL)  http://xmailserver.org/libpcl.html
在 2015年7月3日 上午8:48,Yang Fan <miss...@gmail.com>写道:
OT一下,换用coroutine真的会有很大的各种好处么?

之前看到boost都有coroutine的库了,一直比较怀疑其实用价值。

2015-07-02 20:25 GMT+08:00 Shell Xu <shell...@gmail.com>:
没有。
底层你要么用ucontext,要么用setjmp/longjmp。哪个都有点问题的。

On Thu, Jul 2, 2015 at 1:47 PM, DaboD <daid...@gmail.com> wrote:
如題, google 了一些, 似乎有各自的缺點, 想配合像是 lib[ev|event|uv] 使用.

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+unsubscribe@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout



--
彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
blog: http://shell909090.org/blog/

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+unsubscribe@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout



--
Regards,
Fan Yang

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+unsubscribe@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout



--
Follow your dream!

Shell Xu

unread,
Jul 2, 2015, 11:25:06 PM7/2/15
to shlug
引用一下我去年说过的话。

进入内核态:50ns
上下文切换:随着活跃上下文增加而开销增加,120ns-1000ns不等。
coroutine切换:20ns

Derek Dai

unread,
Jul 2, 2015, 11:29:28 PM7/2/15
to sh...@googlegroups.com
還有代碼中一堆散亂的 callback 及流程也會清楚得多.

想看看有沒有可能做到像是 python 的 generator, 或是 vala 的 async/yield.

Derek Dai

您收到此邮件是因为您订阅了Google网上论坛上“Shanghai Linux User Group”群组中的主题。
要退订此主题,请访问https://groups.google.com/d/topic/shlug/CgDILJ5plfA/unsubscribe
要退订此群组及其所有主题,请发送电子邮件到shlug+un...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

ggarlic

unread,
Jul 6, 2015, 10:43:50 AM7/6/15
to sh...@googlegroups.com
http://state-threads.sourceforge.net/
NSPR的继承者
这个还真不依赖ucontext或者setjmp/longjmp

2015-07-02 20:25 GMT+08:00 Shell Xu <shell...@gmail.com>:
Welcome to World of Ggarlic
http://ggarlic.org

Shell Xu

unread,
Jul 6, 2015, 12:00:38 PM7/6/15
to shlug
唔,这么说不大严谨。
这货的底层自己实现了一套builtin的setjmp/longjmp,叫做_st_md_cxt_save/_st_md_cxt_restore。但是可以根据编译条件使用_setjmp/_longjmp。
而他的实现是怎么做的呢?
jmp

实际上从操作系统来说,无非看到两种形态,一个是纯coroutine切换不进内核的,一个是虽然不需要内核上下文切换,但是依然需要进入内核的。
如果是前者,那么和setjmp/longjmp没有本质分别。差别在于可能实现的更好一些。而如果是后者,则和ucontext没什么太大差别。一次内核态切换就是100多时钟周期。
但是如果不进入内核,那么是不大可能做一些内核操作来切换一些只有内核才能处理的问题的。就我看到而言,貌似在ucontext里面唯一的内核调用就是sigprocmask。
有趣的是,有个API叫做sigsetjmp,是带着sig处理的setjmp。虽然没做过性能测试,但是我预计他和ucontext没什么太大差别。

因此总体来说,coroutine有三种设计可能。
1. 不进入内核态,不处理信号差异。因此速度快,但是可能带来问题(别问我是啥)。
2. 进入内核态,因此速度慢。(每次100多时钟周期)
3. 使用中间层机制来处理信号问题,因而可以让代码平滑的在不同上下文间切换。

C是没有第三个选项的。

我特别解释一下为什么有进入内核态但是不切换上下文这么个选项。因为上下文切换开销是按照活跃上下文数的增长而增长的,因此在巨量活跃上下文的情况下(例如大并发连接),每次切换开销高达数千个指令周期。而仅进入内核态的开销就小很多,比最低活跃上下文的情况下发生的切换还要小。

setjmp/longjmp的原生形态特别遭人鄙视的一个问题是,为了生成栈,所以在栈上开空间,形成栈上栈(微栈)。这样导致栈的深度和可用空间锐减。当然,你可以在不进入内核的前提下修正掉这个问题。这也是各种coroutine lib的要点。

您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/d/optout

DaboD

unread,
Jul 6, 2015, 12:11:15 PM7/6/15
to sh...@googlegroups.com
如果是將 signal 都 block 改用 signalfd (犧牲可攜性) 可行嗎?
>> 要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+unsubscribe@googlegroups.com

>> 要查看更多选项,请访问https://groups.google.com/d/optout
>
>
>
>
> --
> 彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
> blog: http://shell909090.org/blog/
> twitter: @shell909090
> about.me: http://about.me/shell909090
>
> --
> -- You received this message because you are subscribed to the Google Groups
> Shanghai Linux User Group group. To post to this group, send email to
> sh...@googlegroups.com. To unsubscribe from this group, send email to
> shlug+un...@googlegroups.com. For more options, visit this group at
> https://groups.google.com/d/forum/shlug?hl=zh-CN
> ---
> 您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
> 要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+unsubscribe@googlegroups.com
> 要查看更多选项,请访问https://groups.google.com/d/optout



--
Welcome to World of Ggarlic
http://ggarlic.org

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+unsubscribe@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/d/optout

Shell Xu

unread,
Jul 6, 2015, 10:48:18 PM7/6/15
to shlug
我不大理解。
signalfd不需要进入内核态么?

>> 要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com

>> 要查看更多选项,请访问https://groups.google.com/d/optout
>
>
>
>
> --
> 彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
> blog: http://shell909090.org/blog/
> twitter: @shell909090
> about.me: http://about.me/shell909090
>
> --
> -- You received this message because you are subscribed to the Google Groups
> Shanghai Linux User Group group. To post to this group, send email to
> sh...@googlegroups.com. To unsubscribe from this group, send email to
> shlug+un...@googlegroups.com. For more options, visit this group at

> https://groups.google.com/d/forum/shlug?hl=zh-CN
> ---
> 您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
> 要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
> 要查看更多选项,请访问https://groups.google.com/d/optout



--
Welcome to World of Ggarlic
http://ggarlic.org

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/d/optout



--
彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
blog: http://shell909090.org/blog/

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

Derek Dai

unread,
Jul 6, 2015, 11:00:17 PM7/6/15
to sh...@googlegroups.com
需要進入 kernel space, 但 signal 的 handling 就可以用一般的 context 處理, coroutine library 的設計如果不需要擔心 signal 的亂入的話, 是不是就可以不需要去考慮 signal 而免除調用相關的 syscall? 不過是不是有可能會影響部份 library 的使用?

Derek Dai

我不大理解。
signalfd不需要进入内核态么?

Shell Xu

unread,
Jul 6, 2015, 11:14:08 PM7/6/15
to shlug
切换时进入内核的话就需要对应的开销,这是协程极力避免的事情。至于handling,和上下文切换来说比例相对较小。
如果不关心signal乱入的话,应当确实如此。

Han Lei

unread,
Jul 10, 2015, 9:42:25 PM7/10/15
to sh...@googlegroups.com
看下Lua的实现?
                           //////////\\\\
                           ( ~~ ~~ )
                          (   @ @   )
                            (    o    )
 -------------oOOo-----------oOOo--------------
Mit freundlichen Grüßen
Man proposes, God disposes .

Greetings from Oliver Twist
                                      0ooo
 ---------------------oooO---(   )------------------
                        (   )           )  /
                         \  (          (_/
                          \_)
Reply all
Reply to author
Forward
0 new messages