Giter VIP home page Giter VIP logo

coroutine's Introduction

It's an asymmetric coroutine library (like lua).

You can use coroutine_open to open a schedule first, and then create coroutine in that schedule.

You should call coroutine_resume in the thread that you call coroutine_open, and you can't call it in a coroutine in the same schedule.

Coroutines in the same schedule share the stack , so you can create many coroutines without worry about memory.

But switching context will copy the stack the coroutine used.

Read source for detail.

Chinese blog : http://blog.codingnow.com/2012/07/c_coroutine.html

coroutine's People

Contributors

cloudwu avatar iceteacup avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

coroutine's Issues

coroutine_new bug

diff --git a/coroutine.c b/coroutine.c
index 7afdc1c..5470d0d 100644
--- a/coroutine.c
+++ b/coroutine.c
@@ -81,7 +81,7 @@ coroutine_new(struct schedule *S, coroutine_func func, void *ud) {
        struct coroutine *co = _co_new(S, func , ud);
        if (S->nco >= S->cap) {
                int id = S->cap;
-               S->cap *= 2;
+               //S->cap *= 2;
                S->co = realloc(S->co, S->cap * 2 * sizeof(struct coroutine *));
                memset(S->co + S->cap , 0 , sizeof(struct coroutine *) * S->cap);
                S->co[S->cap] = co;

On mac failed

co->status is wrong.
wx20161213-104157

OS: macOS sierra
gcc: Apple LLVM 8.0.0

公用栈支持分组?

比如一个 schedule 里面有 100个 stack,每个 2MB,一共 200MB,每个栈,管理一组 coroutine
假设有 5000个 coroutine ,平均分布在 100组栈上,每组栈 50个 coroutine。

coroutine 被切走的时候,并不会马上拷贝栈,而会保留在那一个栈上,直到同一组的 coroutine 需要resume,才会把之前 on-stack 的 coroutine 拷贝走。

这样每个 stack 上保留的 coroutine 都是最近最活跃的 coroutine,栈拷贝的次数下降100倍。

save [stack, sp]function _save_stack疑问

_save_stack函数中dummy变量定义在coroutine_yield是不是更好?

static void
_save_stack(struct coroutine *C, char *top, char *bottom) {
       //top:stack bottom:sp
	assert(top - bottom <= STACK_SIZE);
	if (C->cap < top - bottom) {
		free(C->stack);
		C->cap = top-bottom;
		C->stack = malloc(C->cap);
	}
	C->size = top - bottom;
	memcpy(C->stack, bottom, C->size);
}

void
coroutine_yield(struct schedule * S) {
	int id = S->running;
	assert(id >= 0);
	struct coroutine * C = S->co[id];
	assert((char *)&C > S->stack);
	char dummy = 0;
	_save_stack(C,S->stack + STACK_SIZE, &dummy);
	C->status = COROUTINE_SUSPEND;
	S->running = -1;
	swapcontext(&C->ctx , &S->main);
}

如何在安卓上使用本协程库

使用QtCreator编辑安卓项目
安卓平台支持ucontext_t,但是不支持setcontext, swapcontext等函数,有没有什么办法在安卓平台上使用本库?

请教个问题

大佬您好 :请教一下如果代码包含了 sleep 这种挂起的动作,怎么才能让其不阻塞呢?
假设某个函数里面有不确定会sleep多久,任何一个挂起就会影响下一个的调度。应该怎么来优化一下这种问题?

#include "coroutine.h"
//
void *loop1(schedule *s, void *args)
{
    printf("loop1\n");
    sleep(3);
}
void *loop2(schedule *s, void *args)
{
    printf("loop2\n");
    sleep(2);
}
void *loop3(schedule *s, void *args)
{
    printf("loop3\n");
    sleep(1);

}

run on macosx

Hi cloud,
I have a problem when compile with gcc and run on macosx10.8 using the example main.c.
Because of ucontext.h, I try to change the include(using sys/ucontext.h instand of ucontext.h):

ifdef ON_MAC

include <sys/ucontext.h>

else

include <ucontext.h>

endif

then complie succ, but execute with some exception. co[1]->status had changed when coroutine_resume() it. so coroutine_resume():default assert(0) running...

I try to gdb and watch the variable of co[1]->status, but nothing to find...
so ... I'm deep appreciate of yours help, or any suggest.

Assertion failed: (0), function coroutine_resume, file coroutine.c, line 155. 错误

Mac OS 10.14.7版本。
gcc info:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

在这个版本上编译,并运行代码生成的main 以后程序报告:

Assertion failed: (0), function coroutine_resume, file coroutine.c, line 155.
然后通过跟踪发现,main.c 里面

struct args arg1 = { 0 };
struct args arg2 = { 100 };

int co1 = coroutine_new(S, foo, &arg1);
int co2 = coroutine_new(S, foo, &arg2);

发现一个问题, struct coroutine 这个结构体的成员 ud 在程序resume coroutine的 co2的时候被修改了。

any idea for this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.