luacoco 增强lua的coroutine功能

Coco is a small extension to get True C Coroutine semantics for Lua 5.1.

Coco is both available as a stand-alone release and integrated into LuaJIT.

The stand-alone release is a patchset against the standard Lua 5.1.3 distribution. There are no dependencies on LuaJIT. However LuaJIT depends on Coco to allow yielding for JIT compiled functions.

这个luacoco patch也是匠心独特 以最小的侵入修改让lua支持原生的coroutine,解除了原版的coroutine的几个限制:

    * Yield across all metamethods (not advised for __gc).
    * Yield across iterator functions (for x in func do).
    * Yield across callbacks (table.foreach(), dofile(), ...).
    * Yield across protected callbacks (pcall(), xpcall(), ...).
    * Yield from C functions and resume back to them.

在实现方面主要是利用以下几个macro来实现修改的:

/*
@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State
@* (the data goes just *before* the lua_State pointer).
** CHANGE (define) this if you really need that. This value must be
** a multiple of the maximum alignment required for your machine.
*/
#define LUAI_EXTRASPACE 0


/*
@@ luai_userstate* allow user-specific actions on threads.
** CHANGE them if you defined LUAI_EXTRASPACE and need to do something
** extra when a thread is created/deleted/resumed/yielded.
*/
#define luai_userstateopen(L) ((void)L)
#define luai_userstateclose(L) ((void)L)
#define luai_userstatethread(L,L1) ((void)L)
#define luai_userstatefree(L) ((void)L)
#define luai_userstateresume(L,n) ((void)L)
#define luai_userstateyield(L,n) ((void)L)

再拜下萝卜神!!!

你可能感兴趣的:(thread,C++,c,C#,lua)