-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
450 additions
and
208 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.