-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlfdb.lua
90 lines (71 loc) · 2.81 KB
/
lfdb.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
local lfdbL = require "lfdb_lib"
local ffi = require "ffi"
lfdbL.setup_ffi(ffi)
local C = ffi.C
local fdb = ffi.load("fdb_c")
local pthread = ffi.load("pthread")
local unique_id = tostring( {} ):sub(8)
local chunk = [[
local unique_id = tostring( {} ):sub(8)
local lfdbL = require "lfdb_lib"
local ffi = require "ffi"
lfdbL.setup_ffi(ffi)
local fdb = ffi.load("fdb_c")
print(string.format("{%s} Spawned thread: {%s}", unique_id, unique_id))
local function fdb_network_thread()
return lfdbL.run_network(ffi, fdb)
end
cb_fdb_network_thread = tonumber(ffi.cast('intptr_t', ffi.cast('void *(*)(void *)', fdb_network_thread)))
]]
local function spawn_thread()
local pid = ffi.new("pthread_t[1]")
local L = assert(C.luaL_newstate())
C.luaL_openlibs(L)
local res = C.luaL_loadstring(L, chunk)
assert(res == 0)
--res = C.lua_pcall(L, 0, 1, 0)
res = C.lua_pcall(L, 0, 0, 0)
if res ~= 0 then
print(string.format("thread error[%s]: %s", unique_id, ffi.string(C.lua_tolstring(L, -1, nil))))
end
assert(res == 0)
C.lua_getfield(L, C.LUA_GLOBALSINDEX, "cb_fdb_network_thread")
local fun = C.lua_tointeger(L, -1)
print(string.format("THREAD FUN IS: %s", fun))
C.lua_settop(L, -2)
print(string.format("{%s} THREAD CREATING", unique_id))
res = pthread.pthread_create(pid, nil, ffi.cast('void *(*)(void *)', fun), nil)
print(string.format("{%s} THREAD CREATED", unique_id))
assert(res == 0)
return pid, L
end
lfdbL.select_api_version(ffi, fdb)
lfdbL.setup_network(ffi, fdb)
print(string.format("fdb.C.fdb_get_error(1): %s", lfdbL.error_to_string(1, ffi, fdb)))
print(string.format("{%s} INVOKING THE THREAD!", unique_id))
local thread, thread_L = spawn_thread()
print(string.format("{%s} DONE INVOKING THE THREAD!", unique_id))
print(string.format("THREAD IS: %s", thread[0]))
-- busy loop networking thread coordination hack
local t = 1
for i=1,100000 do t = t * i * math.log(t*3) end
print("CREATING FDB_DB")
local fdb_db = lfdbL.create_database(ffi, fdb)
print(string.format("FDB_DB IS: %s", fdb_db))
local fdb_tx = lfdbL.create_transaction(ffi, fdb, fdb_db)
print(string.format("FDB_TX IS: %s", fdb_tx))
lfdbL.do_transaction(ffi, fdb, fdb_tx, function(tx) print("IN TRANSACTION!"); assert(true) end)
lfdbL.destroy_transaction(fdb, fdb_tx)
lfdbL.transactional(ffi, fdb, fdb_db, function(tx) print("Failing the transaction!"); assert(false) end)
local key = "asdffdsa"
local val = lfdbL.db_get(ffi, fdb, fdb_db, key)
print(string.format("GOT VAL FOR KEY{%s}: %s", key, val))
lfdbL.db_set(ffi, fdb, fdb_db, key, "12344321")
val = lfdbL.db_get(ffi, fdb, fdb_db, key)
print(string.format("GOT VAL FOR KEY{%s}: %s", key, val))
print("Closing shop...")
local res = pthread.pthread_cancel(thread[0])
assert(res == 0)
lfdbL.destroy_database(fdb, fdb_db)
C.lua_close(thread_L)
print("Exiting...")