Comments (3)
ok. 搞懂了。
首先,应该引入 #include <ucontext.h>,而不是 <sys/ucontext.h>。因为使用到的函数并没有被声明在 <sys/ucontext.h> 里。因为某些原因编译通过了,但是类型估计是不同的。编译时加 flag 跳过检查:-D_XOPEN_SOURCE=600
其次,swapcontext 在 macOS 系统中的实现依赖于在栈上的返回地址,在调用 swapcontext 之前保存栈的信息会导致死循环。
这里是我修改后可以跑通的代码(在同一个函数里保存和 swap 就行了):
https://gist.github.com/Jeswang/4ae2b612f0c2da20265d1d6407b6cd2b
from coroutine.
遇到同样的问题,coroutine_resume(S,co1) 调用 getcontext 这个函数时会修改 co2 里的内容,我猜测 macOS 里定义了不同的 ucontext_t,占用的内存更大,转换的过程中访问越界了。
from coroutine.
. @Jeswang 测试了一下,发现你的修改还是有问题,例子程序里有一句
printf("main end\n");
打印程序结束的信息,但是程序没有走到这里,说明中途出错了。整个程序的输出如下,供你参考。
coroutine % vi test.c
coroutine % gcc test.c -o test
test.c:74:8: warning: 'getcontext' is deprecated: first deprecated in macOS 10.6 - No longer supported [-Wdeprecated-declarations]
ret = getcontext(oucp);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/ucontext.h:38:6: note: 'getcontext' has been explicitly marked deprecated here
int getcontext(ucontext_t *);
^
test.c:80:9: warning: 'setcontext' is deprecated: first deprecated in macOS 10.6 - No longer supported [-Wdeprecated-declarations]
ret = setcontext(ucp);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/ucontext.h:44:6: note: 'setcontext' has been explicitly marked deprecated here
int setcontext(const ucontext_t *);
^
test.c:111:8: warning: 'getcontext' is deprecated: first deprecated in macOS 10.6 - No longer supported [-Wdeprecated-declarations]
ret = getcontext(oucp);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/ucontext.h:38:6: note: 'getcontext' has been explicitly marked deprecated here
int getcontext(ucontext_t *);
^
test.c:117:9: warning: 'setcontext' is deprecated: first deprecated in macOS 10.6 - No longer supported [-Wdeprecated-declarations]
ret = setcontext(ucp);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/ucontext.h:44:6: note: 'setcontext' has been explicitly marked deprecated here
int setcontext(const ucontext_t *);
^
test.c:216:3: warning: 'getcontext' is deprecated: first deprecated in macOS 10.6 - No longer supported [-Wdeprecated-declarations]
getcontext(&C->ctx);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/ucontext.h:38:6: note: 'getcontext' has been explicitly marked deprecated here
int getcontext(ucontext_t *);
^
test.c:223:3: warning: 'makecontext' is deprecated: first deprecated in macOS 10.6 - No longer supported [-Wdeprecated-declarations]
makecontext(&C->ctx, (void (*)(void)) mainfunc, 2, (uint32_t)ptr, (uint32_t)(ptr>>32));
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/ucontext.h:41:6: note: 'makecontext' has been explicitly marked deprecated here
void makecontext(ucontext_t *, void (*)(), int, ...);
^
6 warnings generated.
coroutine % ./test
main start
coroutine 0 : 0
coroutine 1 : 100
coroutine 0 : 1
coroutine 1 : 101
coroutine 0 : 2
coroutine 1 : 102
coroutine 0 : 3
coroutine 1 : 103
coroutine 0 : 4
coroutine 1 : 104
coroutine %
在苹果的源代码里没看出所以然来。
https://github.com/Apple-FOSS-Mirror/Libc/blob/master/x86_64/gen/swapcontext.c
from coroutine.
Related Issues (11)
- coroutine_new bug HOT 1
- 如何在安卓上使用本协程库
- Me
- 请教个问题 HOT 2
- run on macosx HOT 2
- On mac failed HOT 1
- mac ios 编译头文件作者导入错误
- ./main在MacOSX无法正常运行,可能与sys/ucontext.h已经被osX系统废止有关。 HOT 6
- 公用栈支持分组? HOT 4
- save [stack, sp]function _save_stack疑问 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coroutine.