Fix naming for redbean shared memory atomics

This commit is contained in:
Justine Tunney 2022-10-06 09:41:22 -07:00
parent a5b483f2d4
commit 60b68d7152
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
4 changed files with 18 additions and 12 deletions

View file

@ -54,7 +54,7 @@ function Lock()
end end
end end
function Unlock() function Unlock()
local old = mem:add(LOCK, -1) local old = mem:fetch_add(LOCK, -1)
if old == 2 then if old == 2 then
mem:store(LOCK, 0) mem:store(LOCK, 0)
mem:wake(LOCK, 1) mem:wake(LOCK, 1)

View file

@ -45,15 +45,21 @@ ok, old = mem:cmpxchg(0, 0, 1)
assert(ok and old == 0) assert(ok and old == 0)
ok, old = mem:cmpxchg(0, 666, 777) ok, old = mem:cmpxchg(0, 666, 777)
assert(not ok and old == 1) assert(not ok and old == 1)
assert(mem:add(0, 3) == 1) assert(mem:fetch_add(0, 3) == 1)
assert(mem:load(0) == 4) assert(mem:load(0) == 0b00000100)
assert(mem:fetch_xor(0,0b00000110) == 0b00000100)
assert(mem:load(0) == 0b00000010)
assert(mem:fetch_and(0,0b00000110) == 0b00000010)
assert(mem:load(0) == 0b00000010)
assert(mem:fetch_or(0, 0b00000110) == 0b00000010)
assert(mem:load(0) == 0b00000110)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- test atomic addition across concurrent processes -- test atomic addition across concurrent processes
function Worker() function Worker()
for i = 1,iterations do for i = 1,iterations do
mem:add(0, 1) mem:fetch_add(0, 1)
end end
end end

View file

@ -2882,10 +2882,10 @@ static const luaL_Reg kLuaUnixMemoryMeth[] = {
{"store", LuaUnixMemoryStore}, // {"store", LuaUnixMemoryStore}, //
{"xchg", LuaUnixMemoryXchg}, // {"xchg", LuaUnixMemoryXchg}, //
{"cmpxchg", LuaUnixMemoryCmpxchg}, // {"cmpxchg", LuaUnixMemoryCmpxchg}, //
{"add", LuaUnixMemoryAdd}, // {"fetch_add", LuaUnixMemoryAdd}, //
{"and", LuaUnixMemoryAnd}, // {"fetch_and", LuaUnixMemoryAnd}, //
{"or", LuaUnixMemoryOr}, // {"fetch_or", LuaUnixMemoryOr}, //
{"xor", LuaUnixMemoryXor}, // {"fetch_xor", LuaUnixMemoryXor}, //
{"wait", LuaUnixMemoryWait}, // {"wait", LuaUnixMemoryWait}, //
{"wake", LuaUnixMemoryWake}, // {"wake", LuaUnixMemoryWake}, //
{0}, // {0}, //

View file

@ -4620,7 +4620,7 @@ UNIX MODULE
This operation happens atomically and provides the same memory This operation happens atomically and provides the same memory
barrier semantics as the aligned x86 LOCK CMPXCHG instruction. barrier semantics as the aligned x86 LOCK CMPXCHG instruction.
unix.Memory:add(word_index:int, value:int) unix.Memory:fetch_add(word_index:int, value:int)
└─→ old:int └─→ old:int
Fetches then adds value. Fetches then adds value.
@ -4632,7 +4632,7 @@ UNIX MODULE
This operation is atomic and provides the same memory barrier This operation is atomic and provides the same memory barrier
semantics as the aligned x86 LOCK XADD instruction. semantics as the aligned x86 LOCK XADD instruction.
unix.Memory:and(word_index:int, value:int) unix.Memory:fetch_and(word_index:int, value:int)
└─→ int └─→ int
Fetches and bitwise ands value. Fetches and bitwise ands value.
@ -4640,7 +4640,7 @@ UNIX MODULE
This operation happens atomically and provides the same memory This operation happens atomically and provides the same memory
barrier ordering semantics as its x86 implementation. barrier ordering semantics as its x86 implementation.
unix.Memory:or(word_index:int, value:int) unix.Memory:fetch_or(word_index:int, value:int)
└─→ int └─→ int
Fetches and bitwise ors value. Fetches and bitwise ors value.
@ -4648,7 +4648,7 @@ UNIX MODULE
This operation happens atomically and provides the same memory This operation happens atomically and provides the same memory
barrier ordering semantics as its x86 implementation. barrier ordering semantics as its x86 implementation.
unix.Memory:xor(word_index:int, value:int) unix.Memory:fetch_xor(word_index:int, value:int)
└─→ int └─→ int
Fetches and bitwise xors value. Fetches and bitwise xors value.