lua协程一则报错解决“attempt to yield across metamethod/C-call boundary”

问题

attempt to yield across metamethod/C-call boundaryphp

 

需求跟以下帖子中描述一致:html

http://bbs.chinaunix.net/forum.php?mod=viewthread&action=printable&tid=4065715api

模拟一个场景,在C中建立出coroutine来执行Lua脚本,而且提供C API给Lua使用,当某些操做可能会阻塞时(如网络I/O),C函数中执行yield将协程切换出去,而后将来的某个时刻,若是条件知足则resume 继续执行后面的脚本.我写了个demo程序是这样的:网络

 

可是使用此帖子中的 方法的, 将resume和yield都放在c中实现, 也没有解决问题。函数

使用lua版本为 5.1.4post

解法

http://stackoverflow.com/questions/8459459/lua-coroutine-error-tempt-to-yield-across-metamethod-c-call-boundarythis

There are several things you can do if you cannot change your code to avoid the C/metamethod boundary:lua

  • If you are using standard Lua, and are compiling it yourself, try patching it with Coco — True C Coroutines for Lua..net

    True C coroutine semantics mean you can yield from a coroutine across a C call boundary and resume back to it.线程

  • Try using LuaJIT instead of the standard Lua interpreter. It uses a fully resumable VM meaning the boundary is not an issue.

  • Try using Lua 5.2. It features yieldable pcall and metamethods which means that it can handle your problem. However, there are some changes and incompatibilities between Lua 5.1 and Lua 5.2.

不改代码的话,有三种解法。

第一种,  打个补丁,最简单,对现有代码影响最小。

故使用这个方法。

 

coco patch

http://coco.luajit.org/

Coco is a small extension to get True C Coroutine semantics for Lua 5.1. Coco is available as a patch set against the standard Lua 5.1.5 source distribution.

Coco is also integrated into LuaJIT 1.x to allow yielding for JIT compiled functions. But note that Coco does not depend on LuaJIT and works fine with plain Lua.

Coco is Copyright © 2004-2016 Mike Pall. Coco is free software, released under the MIT license (same license as the Lua core).

 

因为其是基于5.1.5开发, 将patch移植到5.1.4版本还有一些比对工做。 如何将patch还原为old new, 能够使用前面博文介绍。

 

将patch移到版本中后发现,仍是不生效。

最后肯定是, c中开辟线程空间的时候, 没有使用 coco的接口, 仍是使用 lua的原生接口。

 

http://lua-users.org/lists/lua-l/2011-10/msg00461.html

 

On 10/15/2011 5:35 PM, Szymon Gatner wrote:
>   lua_State* coro = lua_newthread(L);

You need to use lua_newcthread(). Otherwise LuaJIT 1.x doesn't create a
C stack.

 

http://coco.luajit.org/api.html

lua_State *lua_newcthread(lua_State *L, int cstacksize)

This is an (optional) new function that allows you to create a coroutine with an associated C stack directly from the C API. Other than that it works the same as lua_newthread(L).

You have to declare this function as extern yourself, since it's not part of the official Lua API. This means that a C module that uses this call cannot be loaded with standard Lua. This may be intentional.

相关文章
相关标签/搜索