mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
add sharemap , an example of stm
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "rwlock.h"
|
||||
#include "skynet_malloc.h"
|
||||
@@ -126,8 +127,16 @@ lcopy(lua_State *L) {
|
||||
|
||||
static int
|
||||
lnewwriter(lua_State *L) {
|
||||
void * msg = lua_touserdata(L, 1);
|
||||
uint32_t sz = (uint32_t)luaL_checkinteger(L, 2);
|
||||
void * msg;
|
||||
size_t sz;
|
||||
if (lua_isuserdata(L,1)) {
|
||||
msg = lua_touserdata(L, 1);
|
||||
sz = (size_t)luaL_checkinteger(L, 2);
|
||||
} else {
|
||||
const char * tmp = luaL_checklstring(L,1,&sz);
|
||||
msg = skynet_malloc(sz);
|
||||
memcpy(msg, tmp, sz);
|
||||
}
|
||||
struct boxstm * box = lua_newuserdata(L, sizeof(*box));
|
||||
box->obj = stm_new(msg,sz);
|
||||
lua_pushvalue(L, lua_upvalueindex(1));
|
||||
@@ -148,8 +157,16 @@ ldeletewriter(lua_State *L) {
|
||||
static int
|
||||
lupdate(lua_State *L) {
|
||||
struct boxstm * box = lua_touserdata(L, 1);
|
||||
void * msg = lua_touserdata(L, 2);
|
||||
uint32_t sz = (uint32_t)luaL_checkinteger(L, 3);
|
||||
void * msg;
|
||||
size_t sz;
|
||||
if (lua_isuserdata(L, 2)) {
|
||||
msg = lua_touserdata(L, 2);
|
||||
sz = (size_t)luaL_checkinteger(L, 3);
|
||||
} else {
|
||||
const char * tmp = luaL_checklstring(L,2,&sz);
|
||||
msg = skynet_malloc(sz);
|
||||
memcpy(msg, tmp, sz);
|
||||
}
|
||||
stm_update(box->obj, msg, sz);
|
||||
|
||||
return 0;
|
||||
@@ -186,6 +203,7 @@ static int
|
||||
lread(lua_State *L) {
|
||||
struct boxreader * box = lua_touserdata(L, 1);
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
|
||||
struct stm_copy * copy = stm_copy(box->obj);
|
||||
if (copy == box->lastcopy) {
|
||||
// not update
|
||||
@@ -197,10 +215,13 @@ lread(lua_State *L) {
|
||||
stm_releasecopy(box->lastcopy);
|
||||
box->lastcopy = copy;
|
||||
if (copy) {
|
||||
lua_settop(L, 3);
|
||||
lua_replace(L, 1);
|
||||
lua_settop(L, 2);
|
||||
lua_pushlightuserdata(L, copy->msg);
|
||||
lua_pushinteger(L, copy->sz);
|
||||
lua_call(L, 2, LUA_MULTRET);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_call(L, 3, LUA_MULTRET);
|
||||
lua_pushboolean(L, 1);
|
||||
lua_replace(L, 1);
|
||||
return lua_gettop(L);
|
||||
|
||||
Reference in New Issue
Block a user