Skip to content

Commit

Permalink
implement Notify C API
Browse files Browse the repository at this point in the history
  • Loading branch information
osch committed Nov 3, 2021
1 parent 2afea5e commit 460b13f
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 208 deletions.
43 changes: 0 additions & 43 deletions CMakeLists.txt

This file was deleted.

86 changes: 0 additions & 86 deletions Makefile

This file was deleted.

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ assert(thread:join())
simple data types (string, number, boolean, nil, light user data).

This function returns a state referencing lua object with *state:isowner() == true*.

State objects implement the *Notify C API*, see [src/notify_capi.h](./src/notify_capi.h),
i.e. the state object has an an associated meta table entry *_capi_notify* delivered by
the C API function *notify_get_capi()* and the associated C API function *toNotifier()* returns
a valid pointer for a given state object. The notify will invoke the state's callback
function without arguments. See also [example06.lua](./examples/example06.lua).

Possible errors: *mtstates.error.invoking_state*,
*mtstates.error.state_result*
Expand Down
68 changes: 68 additions & 0 deletions examples/example06.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--[[
This examples demonstrates the usage of the *Notify C API* in a multithreading scenario.
Mtstates' state objects implement the *Notify C API*, see [src/notify_capi.h](../src/notify_capi.h),
i.e. the state object has an an associated meta table entry *_capi_notify* delivered by
the C API function *notify_get_capi()* and the associated C API function *toNotifier()* returns
a valid pointer for a given state object.
In this example the *Notify C API* is used to notify a state object by invoking the state's
callback function without arguments each time a message is added to a [mtmsg](https://github.com/osch/lua-mtmsg)
buffer object.
This is done by connecting the state object as a notifier object to the mtmsg buffer
object.
--]]

local llthreads = require("llthreads2.ex")
local mtmsg = require("mtmsg")
local mtstates = require("mtstates")

local buffer = mtmsg.newbuffer()

local state = mtstates.newstate(function(bufferId)
local mtmsg = require("mtmsg")
local buffer = mtmsg.buffer(bufferId)
local list = {}
local cmds = {
add = function(arg) list[#list + 1] = arg end,
get = function() return table.concat(list, " ") end
}
return function(cmd, ...)
if cmd then
return cmds[cmd](...)
else
assert(select("#", ...) == 0)
local a1, a2 = buffer:nextmsg()
print("State received", a1, a2)
list[#list + 1] = a1
list[#list + 1] = a2
end
end
end, buffer:id())

buffer:notifier(state) -- state will be notified each time a message is added to the buffer

local thread = llthreads.new(function(bufferId)
local mtmsg = require("mtmsg")
local buffer = mtmsg.buffer(bufferId)
for i = 2, 4 do
print("Thread puts into buffer ", i)
buffer:addmsg("fromThread:", i)
end
end,
buffer:id())

state:call("add", "init")

buffer:addmsg("fromMain:", 1)

print("Starting Thread")
thread:start()
thread:join()
print("Thread finished")
buffer:addmsg("fromMain:", 5)
local list = state:call("get")
print("list:", list)
assert(list == "init fromMain: 1 fromThread: 2 fromThread: 3 fromThread: 4 fromMain: 5")

49 changes: 49 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.PHONY: default mtstates
default: mtstates

BUILD_DATE := $(shell date "+%Y-%m-%dT%H:%M:%S")

LNX_GCC_RUN := gcc -shared -fPIC -O2
WIN_GCC_RUN := gcc -shared -fPIC -O2
MAC_GCC_RUN := MACOSX_DEPLOYMENT_TARGET=10.8 gcc -O2 -bundle -undefined dynamic_lookup -all_load

LNX_COPTS :=
WIN_COPTS := -I/mingw64/include/lua5.1
MAC_COPTS := -I/usr/local/opt/lua/include/lua5.3

LNX_LOPTS := -lpthread
WIN_LOPTS := -lkernel32
MAC_LOPTS := -lpthread

LNX_SO_EXT := so
WIN_SO_EXT := dll
MAC_SO_EXT := so

GCC_RUN :=
SO_EXT :=
COPTS :=
LOPTS :=

# platforms: LNX, WIN, MAC
# (may be set in sandbox.mk)

PLATFORM := LNX
LUA_VERSION := 5.4

-include sandbox.mk

GCC_RUN := $(or $(GCC_RUN), $($(PLATFORM)_GCC_RUN))
SO_EXT := $(or $(SO_EXT), $($(PLATFORM)_SO_EXT))
COPTS := $(or $(COPTS), $($(PLATFORM)_COPTS))
LOPTS := $(or $(LOPTS), $($(PLATFORM)_LOPTS))

mtstates:
@mkdir -p build/lua$(LUA_VERSION)/
$(GCC_RUN) $(COPTS) \
-D MTSTATES_VERSION=Makefile"-$(BUILD_DATE)" \
main.c state.c error.c util.c \
async_util.c mtstates_compat.c \
$(LOPTS) \
-o build/lua$(LUA_VERSION)/mtstates.$(SO_EXT)


18 changes: 9 additions & 9 deletions examples/setup.sh → src/activate.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# Setup script for invoking examples from development sandbox when
# Activations script for add the mtstates module to the lua path when
# building mtstates using the Makefile.
# Source this into interactive shell by invoking ". setup.sh" from this directory
# Source this into interactive shell by invoking ". activates.sh" from this directory
# This is not necessary if mtstates is installed, e.g. via luarocks.

this_dir=$(pwd)

luamtstates_dir=$(cd "$this_dir"/..; pwd)

if [ ! -e "$luamtstates_dir/examples/setup.sh" -o ! -e "$luamtstates_dir/src/main.c" ]; then
if [ ! -e "$luamtstates_dir/src/activate.sh" -o ! -e "$luamtstates_dir/src/main.c" ]; then

echo '**** ERROR: ". setup.sh" must be invoked from "examples" directory ***'
echo '**** ERROR: ". activate.sh" must be invoked from "examples" directory ***'

else

echo "Setting lua paths for: $luamtstates_dir"

add_lua_path="$luamtstates_dir/src/?.lua;$luamtstates_dir/src/?/init.lua"
add_lua_cpath="$luamtstates_dir/build/?.so"
add_lua_cpath="$luamtstates_dir/src/build"

# unset LUA_PATH_5_3 LUA_CPATH_5_3 LUA_PATH_5_2 LUA_CPATH_5_2 LUA_PATH LUA_CPATH

Expand All @@ -26,11 +26,11 @@ else
echo "Setting path for lua (version=$default_version)"
if [ "$default_version" = "5.1" ]; then
export LUA_PATH="$add_lua_path;$(lua -e 'print(package.path)')"
export LUA_CPATH="$add_lua_cpath;$(lua -e 'print(package.cpath)')"
export LUA_CPATH="$add_lua_cpath/lua5.1/?.so;$(lua -e 'print(package.cpath)')"
else
lua_path_vers=$(echo $default_version|sed 's/\./_/')
eval "export LUA_PATH_$lua_path_vers=\"$add_lua_path;$(lua -e 'print(package.path)')\""
eval "export LUA_CPATH_$lua_path_vers=\"$add_lua_cpath;$(lua -e 'print(package.cpath)')\""
eval "export LUA_CPATH_$lua_path_vers=\"$add_lua_cpath/lua$default_version/?.so;$(lua -e 'print(package.cpath)')\""
fi
fi

Expand All @@ -47,11 +47,11 @@ else
echo "Setting path for $lua_cmd (version=$lua_version)"
if [ "$lua_version" = "5.1" ]; then
export LUA_PATH="$add_lua_path;$($lua_cmd -e 'print(package.path)')"
export LUA_CPATH="$add_lua_cpath;$($lua_cmd -e 'print(package.cpath)')"
export LUA_CPATH="$add_lua_cpath/lua5.1/?.so;$($lua_cmd -e 'print(package.cpath)')"
else
lua_path_vers=$(echo $lua_version|sed 's/\./_/')
eval "export LUA_PATH_$lua_path_vers=\"$add_lua_path;$($lua_cmd -e 'print(package.path)')\""
eval "export LUA_CPATH_$lua_path_vers=\"$add_lua_cpath;$($lua_cmd -e 'print(package.cpath)')\""
eval "export LUA_CPATH_$lua_path_vers=\"$add_lua_cpath/lua$lua_version/?.so;$($lua_cmd -e 'print(package.cpath)')\""
fi
fi
fi
Expand Down
Loading

0 comments on commit 460b13f

Please sign in to comment.