cloudwu / coroutine Goto Github PK
View Code? Open in Web Editor NEWA asymmetric coroutine library for C.
License: MIT License
A asymmetric coroutine library for C.
License: MIT License
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;
#define _XOPEN_SOURCE
#if APPLE && MACH
#include <ucontext.h>
#else
#include <sys/ucontext.h>
#endif
在mac上 加上这个才能编译 运行成功
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):
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.
大佬您好 :请教一下如果代码包含了 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);
}
比如一个 schedule 里面有 100个 stack,每个 2MB,一共 200MB,每个栈,管理一组 coroutine
假设有 5000个 coroutine ,平均分布在 100组栈上,每组栈 50个 coroutine。
coroutine 被切走的时候,并不会马上拷贝栈,而会保留在那一个栈上,直到同一组的 coroutine 需要resume,才会把之前 on-stack 的 coroutine 拷贝走。
这样每个 stack 上保留的 coroutine 都是最近最活跃的 coroutine,栈拷贝的次数下降100倍。
_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);
}
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?
在osX下,程序在coroutine.c 150行,运行到assert(0)退出。在linux下正常运行。
使用QtCreator编辑安卓项目
安卓平台支持ucontext_t,但是不支持setcontext, swapcontext等函数,有没有什么办法在安卓平台上使用本库?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.