Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method 'exists' to core/shm #990

Merged
merged 1 commit into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,10 @@ If *readonly* is non-nil the shared object is mapped in read-only mode.
*Readonly* defaults to nil. Fails if the shared object does not already exist.
Returns a pointer to the mapped object.

— Function **shm.exists** *name*

Checks whether shared object *name* exists.

— Function **shm.unmap** *pointer*

Deletes the memory mapping for *pointer*.
Expand Down
14 changes: 14 additions & 0 deletions src/core/shm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ function open (name, type, readonly)
return map(name, type, readonly, false)
end

function exists (name)
local path = resolve(name)
local fd = S.open(root..'/'..path, "rdonly")
return fd and fd:close()
end

function resolve (name)
local q, p = name:match("^(/*)(.*)") -- split qualifier (/)
local result = p
Expand Down Expand Up @@ -191,6 +197,14 @@ function selftest ()
unmap(p1)
unmap(p2)

print("checking exists..")
assert(not exists(name))
local p1 = create(name, "struct { int x, y, z; }")
assert(exists(name))
assert(unlink(name))
unmap(p1)
assert(not exists(name))

-- Test that we can open and cleanup many objects
print("checking many objects..")
local path = 'shm/selftest/manyobj'
Expand Down