mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-24 11:30:29 +00:00
Add shared memory apis to redbean
You can now do things like implement mutexes using futexes in your redbean lua code. This provides the fastest possible inter-process communication for your production systems when SQLite alone as ipc or things like pipes aren't sufficient.
This commit is contained in:
parent
81ee11a16e
commit
7822917fc2
21 changed files with 988 additions and 23 deletions
113
test/tool/net/futex_test.lua
Normal file
113
test/tool/net/futex_test.lua
Normal file
|
@ -0,0 +1,113 @@
|
|||
-- Copyright 2022 Justine Alexandra Roberts Tunney
|
||||
--
|
||||
-- Permission to use, copy, modify, and/or distribute this software for
|
||||
-- any purpose with or without fee is hereby granted, provided that the
|
||||
-- above copyright notice and this permission notice appear in all copies.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
-- DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
-- PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
-- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
-- PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
assert(unix.pledge("stdio proc"))
|
||||
|
||||
-- let's use futexes to implement a scalable mutex in lua
|
||||
--
|
||||
-- this example is designed to be the kind of thing that would be
|
||||
-- extremely expensive if we were using spin locks, because we'll
|
||||
-- have a lot of processes that wait a long time for something to
|
||||
-- happen. futexes solve that by asking the kernel to help us out
|
||||
-- and let the waiting processes sleep until the actual event.
|
||||
--
|
||||
-- here we have fifty processes, waiting one just one process, to
|
||||
-- to finish a long-running task. if we used spin locks, then the
|
||||
-- waiter procesess would eat up all the cpu. if you benchmark it
|
||||
-- then be sure to note that WALL TIME will be the same, it's the
|
||||
-- CPU USER TIME that gets pwnd.
|
||||
--
|
||||
-- uses 67 ms cpu time with futexes
|
||||
-- uses 5,000 ms cpu time with spinlocks
|
||||
millis = 300
|
||||
waiters = 100
|
||||
|
||||
words = 2
|
||||
mem = unix.mapshared(words * 8)
|
||||
LOCK = 0 -- word index of our lock
|
||||
RESULT = 1 -- word index of our result
|
||||
|
||||
-- From Futexes Are Tricky Version 1.1 § Mutex, Take 3;
|
||||
-- Ulrich Drepper, Red Hat Incorporated, June 27, 2004.
|
||||
function Lock()
|
||||
local ok, old = mem:cmpxchg(LOCK, 0, 1)
|
||||
if not ok then
|
||||
if old == 1 then
|
||||
old = mem:xchg(LOCK, 2)
|
||||
end
|
||||
while old > 0 do
|
||||
mem:wait(LOCK, 2)
|
||||
old = mem:xchg(LOCK, 2)
|
||||
end
|
||||
end
|
||||
end
|
||||
function Unlock()
|
||||
local old = mem:add(LOCK, -1)
|
||||
if old == 2 then
|
||||
mem:store(LOCK, 0)
|
||||
mem:wake(LOCK, 1)
|
||||
end
|
||||
end
|
||||
|
||||
-- -- try it out with spin locks instead
|
||||
-- function Lock()
|
||||
-- while mem:xchg(LOCK, 1) == 1 do
|
||||
-- end
|
||||
-- end
|
||||
-- function Unlock()
|
||||
-- mem:store(LOCK, 0)
|
||||
-- end
|
||||
|
||||
function Worker()
|
||||
assert(unix.nanosleep(math.floor(millis / 1000),
|
||||
millis % 1000 * 1000 * 1000))
|
||||
mem:store(RESULT, 123)
|
||||
Unlock()
|
||||
end
|
||||
|
||||
function Waiter()
|
||||
Lock()
|
||||
assert(mem:load(RESULT) == 123)
|
||||
Unlock()
|
||||
end
|
||||
|
||||
Lock()
|
||||
|
||||
for i = 1,waiters do
|
||||
pid = assert(unix.fork())
|
||||
if pid == 0 then
|
||||
Waiter()
|
||||
unix.exit(0)
|
||||
end
|
||||
end
|
||||
|
||||
Worker()
|
||||
|
||||
while true do
|
||||
rc, ws = unix.wait(0)
|
||||
if not rc then
|
||||
assert(ws:errno() == unix.ECHILD)
|
||||
break
|
||||
end
|
||||
if unix.WIFEXITED(ws) then
|
||||
if unix.WEXITSTATUS(ws) ~= 0 then
|
||||
print('process %d exited with %s' % {rc, unix.WEXITSTATUS(ws)})
|
||||
unix.exit(1)
|
||||
end
|
||||
else
|
||||
print('process %d terminated with %s' % {rc, unix.WTERMSIG(ws)})
|
||||
unix.exit(1)
|
||||
end
|
||||
end
|
85
test/tool/net/mapshared_test.lua
Normal file
85
test/tool/net/mapshared_test.lua
Normal file
|
@ -0,0 +1,85 @@
|
|||
-- Copyright 2022 Justine Alexandra Roberts Tunney
|
||||
--
|
||||
-- Permission to use, copy, modify, and/or distribute this software for
|
||||
-- any purpose with or without fee is hereby granted, provided that the
|
||||
-- above copyright notice and this permission notice appear in all copies.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
-- DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
-- PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
-- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
-- PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
assert(unix.pledge("stdio proc"))
|
||||
|
||||
words = 2
|
||||
processes = 8
|
||||
iterations = 10000
|
||||
|
||||
mem = unix.mapshared(words * 8)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- test shared memory string reading and writing
|
||||
|
||||
mem:write('hello')
|
||||
assert(mem:read() == 'hello')
|
||||
mem:write('hi')
|
||||
assert(mem:read() == 'hi')
|
||||
assert(mem:read(0, 5) == 'hi\0lo')
|
||||
mem:write('H', 0, 1)
|
||||
assert(mem:read(0, 5) == 'Hi\0lo')
|
||||
assert(mem:read(1, 1) == 'i')
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- test shared memory locking primitives
|
||||
|
||||
mem:store(0, 0)
|
||||
assert(mem:xchg(0, 1) == 0)
|
||||
assert(mem:xchg(0, 2) == 1)
|
||||
|
||||
mem:store(0, 0)
|
||||
ok, old = mem:cmpxchg(0, 0, 1)
|
||||
assert(ok and old == 0)
|
||||
ok, old = mem:cmpxchg(0, 666, 777)
|
||||
assert(not ok and old == 1)
|
||||
assert(mem:add(0, 3) == 1)
|
||||
assert(mem:load(0) == 4)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- test atomic addition across concurrent processes
|
||||
|
||||
function Worker()
|
||||
for i = 1,iterations do
|
||||
mem:add(0, 1)
|
||||
end
|
||||
end
|
||||
|
||||
mem:store(0, 0)
|
||||
for i = 1,processes do
|
||||
pid = assert(unix.fork())
|
||||
if pid == 0 then
|
||||
Worker()
|
||||
unix.exit(0)
|
||||
end
|
||||
end
|
||||
while true do
|
||||
rc, ws = unix.wait(0)
|
||||
if not rc then
|
||||
assert(ws:errno() == unix.ECHILD)
|
||||
break
|
||||
end
|
||||
if unix.WIFEXITED(ws) then
|
||||
if unix.WEXITSTATUS(ws) ~= 0 then
|
||||
print('process %d exited with %s' % {rc, unix.WEXITSTATUS(ws)})
|
||||
unix.exit(1)
|
||||
end
|
||||
else
|
||||
print('process %d terminated with %s' % {rc, unix.WTERMSIG(ws)})
|
||||
unix.exit(1)
|
||||
end
|
||||
end
|
||||
|
||||
assert(mem:load(0) == processes * iterations)
|
76
test/tool/net/spinlock_test.lua
Normal file
76
test/tool/net/spinlock_test.lua
Normal file
|
@ -0,0 +1,76 @@
|
|||
-- Copyright 2022 Justine Alexandra Roberts Tunney
|
||||
--
|
||||
-- Permission to use, copy, modify, and/or distribute this software for
|
||||
-- any purpose with or without fee is hereby granted, provided that the
|
||||
-- above copyright notice and this permission notice appear in all copies.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
-- DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
-- PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
-- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
-- PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
assert(unix.pledge("stdio proc"))
|
||||
|
||||
words = 2
|
||||
processes = 8
|
||||
iterations = 10000
|
||||
|
||||
mem = unix.mapshared(words * 8)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- let's use atomics to implement a spin lock in lua
|
||||
|
||||
LOCK = 0 -- word index of our lock
|
||||
COUNTER = 1 -- word index of our number
|
||||
|
||||
function Lock()
|
||||
while mem:xchg(LOCK, 1) == 1 do
|
||||
end
|
||||
end
|
||||
|
||||
function Unlock()
|
||||
mem:store(LOCK, 0)
|
||||
end
|
||||
|
||||
function Worker()
|
||||
local x
|
||||
for i = 1,iterations do
|
||||
Lock()
|
||||
x = mem:load(COUNTER)
|
||||
x = x + 1
|
||||
mem:store(COUNTER, x)
|
||||
Unlock()
|
||||
end
|
||||
end
|
||||
|
||||
mem:store(LOCK, 0)
|
||||
mem:store(COUNTER, 0)
|
||||
for i = 1,processes do
|
||||
pid = assert(unix.fork())
|
||||
if pid == 0 then
|
||||
Worker()
|
||||
unix.exit(0)
|
||||
end
|
||||
end
|
||||
while true do
|
||||
rc, ws = unix.wait(0)
|
||||
if not rc then
|
||||
assert(ws:errno() == unix.ECHILD)
|
||||
break
|
||||
end
|
||||
if unix.WIFEXITED(ws) then
|
||||
if unix.WEXITSTATUS(ws) ~= 0 then
|
||||
print('process %d exited with %s' % {rc, unix.WEXITSTATUS(ws)})
|
||||
unix.exit(1)
|
||||
end
|
||||
else
|
||||
print('process %d terminated with %s' % {rc, unix.WTERMSIG(ws)})
|
||||
unix.exit(1)
|
||||
end
|
||||
end
|
||||
|
||||
assert(mem:load(COUNTER) == processes * iterations)
|
Loading…
Add table
Add a link
Reference in a new issue