Compatibility of luajit coroutine

Source: Internet
Author: User
Compatibility of luajit coroutine

//
// This test shows that Lua and luajit have different processing capabilities in the coroutine,
// Luajit lacks compatibility in processing operations from Lua yield to Lua,
// However, the example included with Coco: cotet. Lua description, because luajit supports Coco,
// The support for the Lua pcall test is enhanced.
//

Bool test_c_lua_coroutine ()
{
//
// Not passed in luajit
//
Luastateincpp * State = new luastateincpp ();
State-> Init (null );
State-> do_string ("A = function () I = coroutine. Yield (1); print (I); coroutine. Yield (2); End ");
Luastateincpp * CO = state-> new_thread ("");
Printf ("start... n ");
Int yieldret = Co-> resume ();
Printf ("n first yield ret = % d", yieldret );
Yieldret = Co-> resume ();
Printf ("N 2nd yield ret = % d", yieldret );
Yieldret = Co-> resume ();
Printf ("n 3rd yield ret = % d", yieldret );

//
// Passed in luajit
//
{
Luastateincpp * State = new luastateincpp ();
State-> Init (null );
State-> do_string ("A = function () I = coroutine. Yield (1); print (I); coroutine. Yield (2); End ");
State-> do_string ("CO = coroutine. Wrap ()");
State-> do_string ("Print ('1: '. CO ())");
State-> do_string ("Print ('2: '. CO ())");
State-> do_string ("Print ('3: '.. (Co () or ''))");
}

Return true;
}




Lua 5.1.2 copyright (c) 1994-2007 lua.org, pUC-Rio
> Require "linker_mod"
Linker_mod Ver: 1.0.0.0 loaded!
> Test_c_lua_coroutine ()
Cannot open: Invalid argumentstart...

First yield ret = 1nil

2nd yield ret = 1
3rd yield ret = 0 cannot open: Invalid argument1: 1
Nil
2: 2
3:





Lua 5.1.3 copyright (c) 1994-2008 lua.org, pUC-Rio
Luajit 1.1.4 copyright (c) 2005-2008 Mike pall, http://luajit.org/
> Require "linker_mod"
Linker_mod Ver: 1.0.0.0 loaded!
> Test_c_lua_coroutine ()
Cannot open: Invalid argumentstart...
Attempt to yield processing SS metamethod/c-call boundary
First yield ret =-1 cannot resume non-susponded coroutine
2nd yield ret =-1 cannot resume non-susponded coroutine
3rd yield ret =-1 cannot open: Invalid argument1: 1
Nil
2: 2
3:
>


To solve this problem, someone wrote a patch on Lua wiki, but this patch has not been fully tested and verified.
Not maintained for a long time.
To put it simply, the root cause of the yield resume problem of luavm is the processing of the context correspondence between the context of the C function and the context of the Lua coroutine.
In fact, the complete solution is to avoid directly switching the coroutine between C and Lua, but to implement all coroutine switching on the Lua side.
Limited to simple function calls.
This avoids many problems.













Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.