Giter Club home page Giter Club logo

Comments (3)

Jeswang avatar Jeswang commented on July 19, 2024 1

ok. 搞懂了。

首先,应该引入 #include <ucontext.h>,而不是 <sys/ucontext.h>。因为使用到的函数并没有被声明在 <sys/ucontext.h> 里。因为某些原因编译通过了,但是类型估计是不同的。编译时加 flag 跳过检查:-D_XOPEN_SOURCE=600

其次,swapcontext 在 macOS 系统中的实现依赖于在栈上的返回地址,在调用 swapcontext 之前保存栈的信息会导致死循环。

苹果的实现见:
https://github.com/Apple-FOSS-Mirror/Libc/blob/2ca2ae74647714acfc18674c3114b1a5d3325d7d/i386/gen/swapcontext.c

这里是我修改后可以跑通的代码(在同一个函数里保存和 swap 就行了):
https://gist.github.com/Jeswang/4ae2b612f0c2da20265d1d6407b6cd2b

from coroutine.

Jeswang avatar Jeswang commented on July 19, 2024

遇到同样的问题,coroutine_resume(S,co1) 调用 getcontext 这个函数时会修改 co2 里的内容,我猜测 macOS 里定义了不同的 ucontext_t,占用的内存更大,转换的过程中访问越界了。

from coroutine.

ssrlive avatar ssrlive commented on July 19, 2024

. @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)

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.