* add callback support for lua-llthreads

This commit is contained in:
YuLei Liao 2011-12-05 22:10:29 +08:00
parent a99b2f0675
commit df8231362c
2 changed files with 1254 additions and 1024 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,25 +2,23 @@
#ifndef __LLTHREADS_H_ #ifndef __LLTHREADS_H_
#define __LLTHREADS_H_ #define __LLTHREADS_H_
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lualib.h" #include "lualib.h"
#define REG_PACKAGE_IS_CONSTRUCTOR 0 #define REG_PACKAGE_IS_CONSTRUCTOR 0
#define REG_OBJECTS_AS_GLOBALS 0 #define REG_OBJECTS_AS_GLOBALS 0
#define OBJ_DATA_HIDDEN_METATABLE 1 #define OBJ_DATA_HIDDEN_METATABLE 1
#define LUAJIT_FFI 0 #define LUAJIT_FFI 0
#define USE_FIELD_GET_SET_METHODS 0 #define USE_FIELD_GET_SET_METHODS 0
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#ifdef _MSC_VER #ifdef _MSC_VER
#define __WINDOWS__ #define __WINDOWS__
#else #else
@ -113,77 +111,86 @@ typedef void (*dyn_caster_t)(void **obj, obj_type **type);
#define OBJ_TYPE_FLAG_WEAK_REF (1<<0) #define OBJ_TYPE_FLAG_WEAK_REF (1<<0)
#define OBJ_TYPE_SIMPLE (1<<1) #define OBJ_TYPE_SIMPLE (1<<1)
struct obj_type { struct obj_type
dyn_caster_t dcaster; /**< caster to support casting to sub-objects. */ {
int32_t id; /**< type's id. */ dyn_caster_t dcaster; /**< caster to support casting to sub-objects. */
uint32_t flags; /**< type's flags (weak refs) */ int32_t id; /**< type's id. */
const char *name; /**< type's object name. */ uint32_t flags; /**< type's flags (weak refs) */
const char *name; /**< type's object name. */
}; };
typedef struct obj_base { typedef struct obj_base
int32_t id; {
base_caster_t bcaster; int32_t id;
base_caster_t bcaster;
} obj_base; } obj_base;
typedef enum obj_const_type { typedef enum obj_const_type
CONST_UNKOWN = 0, {
CONST_BOOLEAN = 1, CONST_UNKOWN = 0,
CONST_NUMBER = 2, CONST_BOOLEAN = 1,
CONST_STRING = 3 CONST_NUMBER = 2,
CONST_STRING = 3
} obj_const_type; } obj_const_type;
typedef struct obj_const { typedef struct obj_const
const char *name; /**< constant's name. */ {
const char *str; const char *name; /**< constant's name. */
double num; const char *str;
obj_const_type type; double num;
obj_const_type type;
} obj_const; } obj_const;
typedef enum obj_field_type { typedef enum obj_field_type
TYPE_UNKOWN = 0, {
TYPE_UINT8 = 1, TYPE_UNKOWN = 0,
TYPE_UINT16 = 2, TYPE_UINT8 = 1,
TYPE_UINT32 = 3, TYPE_UINT16 = 2,
TYPE_UINT64 = 4, TYPE_UINT32 = 3,
TYPE_INT8 = 5, TYPE_UINT64 = 4,
TYPE_INT16 = 6, TYPE_INT8 = 5,
TYPE_INT32 = 7, TYPE_INT16 = 6,
TYPE_INT64 = 8, TYPE_INT32 = 7,
TYPE_DOUBLE = 9, TYPE_INT64 = 8,
TYPE_FLOAT = 10, TYPE_DOUBLE = 9,
TYPE_STRING = 11 TYPE_FLOAT = 10,
TYPE_STRING = 11
} obj_field_type; } obj_field_type;
typedef struct obj_field { typedef struct obj_field
const char *name; /**< field's name. */ {
uint32_t offset; /**< offset to field's data. */ const char *name; /**< field's name. */
obj_field_type type; /**< field's data type. */ uint32_t offset; /**< offset to field's data. */
uint32_t flags; /**< is_writable:1bit */ obj_field_type type; /**< field's data type. */
uint32_t flags; /**< is_writable:1bit */
} obj_field; } obj_field;
typedef struct reg_sub_module { typedef struct reg_sub_module
obj_type *type; {
int is_package; obj_type *type;
const luaL_reg *pub_funcs; int is_package;
const luaL_reg *methods; const luaL_reg *pub_funcs;
const luaL_reg *metas; const luaL_reg *methods;
const obj_base *bases; const luaL_reg *metas;
const obj_field *fields; const obj_base *bases;
const obj_const *constants; const obj_field *fields;
const obj_const *constants;
} reg_sub_module; } reg_sub_module;
#define OBJ_UDATA_FLAG_OWN (1<<0) #define OBJ_UDATA_FLAG_OWN (1<<0)
#define OBJ_UDATA_FLAG_LOOKUP (1<<1) #define OBJ_UDATA_FLAG_LOOKUP (1<<1)
#define OBJ_UDATA_LAST_FLAG (OBJ_UDATA_FLAG_LOOKUP) #define OBJ_UDATA_LAST_FLAG (OBJ_UDATA_FLAG_LOOKUP)
typedef struct obj_udata { typedef struct obj_udata
void *obj; {
uint32_t flags; /**< lua_own:1bit */ void *obj;
uint32_t flags; /**< lua_own:1bit */
} obj_udata; } obj_udata;
#if LUAJIT_FFI #if LUAJIT_FFI
typedef struct ffi_export_symbol { typedef struct ffi_export_symbol
const char *name; {
void *sym; const char *name;
void *sym;
} ffi_export_symbol; } ffi_export_symbol;
#endif #endif
@ -199,6 +206,62 @@ typedef struct ffi_export_symbol {
#define OBJ_DATA_HIDDEN_METATABLE 1 #define OBJ_DATA_HIDDEN_METATABLE 1
#endif #endif
#ifdef __WINDOWS__
#include <windows.h>
#include <stdio.h>
#include <process.h>
#else
#include <pthread.h>
#include <stdio.h>
#endif
typedef enum
{
TSTATE_NONE = 0,
TSTATE_STARTED = 1<<0,
TSTATE_DETACHED = 1<<1,
TSTATE_JOINED = 1<<2,
} Lua_TState;
typedef struct Lua_LLThread_child
{
lua_State *L;
int status;
int is_detached;
int callback;
} Lua_LLThread_child;
typedef struct Lua_LLThread
{
Lua_LLThread_child *child;
#ifdef __WINDOWS__
HANDLE thread;
#else
pthread_t thread;
#endif
Lua_TState state;
} Lua_LLThread;
typedef struct
{
lua_State *from_L;
lua_State *to_L;
int has_cache;
int cache_idx;
int is_arg;
} llthread_copy_state;
#define LLTHREAD_REFID_FUNC_MAPPING "llthread_refid_func_mapping"
#ifdef __WINDOWS__
#define RUN_CHILD_THREAD static void
#else
#define RUN_CHILD_THREAD static void*
#endif
int luaopen_llthreads(lua_State *L); int luaopen_llthreads(lua_State *L);
int llthread_push_results(lua_State *L, Lua_LLThread_child *child, int idx, int top);
#endif // __LLTHREADS_H_ #endif // __LLTHREADS_H_