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

Garbage collect remote SHM segments when origin Participant is closed… > 2.0.x [9029] #1330

Merged
merged 1 commit into from
Sep 4, 2020
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
78 changes: 54 additions & 24 deletions src/cpp/rtps/transport/shared_mem/RobustExclusiveLock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <io.h>
#else
#include <sys/file.h>
#endif
#endif // ifdef _MSC_VER

#include <sys/types.h>
#include <sys/stat.h>
Expand All @@ -42,7 +42,7 @@ class RobustExclusiveLock
/**
* Open or create and acquire the interprocess lock.
* @param in name Is the object's interprocess global name, visible for all processes in the same machine.
* @param out was_lock_created If the lock succeeded, this parameter return whether the lock has been created
* @param out was_lock_created If the lock succeeded, this parameter return whether the lock has been created
* or it already exist.
* @throw std::exception if lock coulnd't be acquired
*/
Expand All @@ -52,7 +52,10 @@ class RobustExclusiveLock
{
auto file_path = RobustLock::get_file_path(name);

fd_ = open_and_lock_file(file_path, was_lock_created);
if ((fd_ = open_and_lock_file(file_path, was_lock_created)) == -1)
{
throw std::runtime_error("open_and_lock_file failed");
}

name_ = name;
}
Expand All @@ -69,17 +72,42 @@ class RobustExclusiveLock

auto file_path = RobustLock::get_file_path(name);

fd_ = open_and_lock_file(file_path, &was_lock_created);
if ((fd_ = open_and_lock_file(file_path, &was_lock_created)) == -1)
{
throw std::runtime_error("open_and_lock_file failed");
}

name_ = name;
}

/**
* Checks whether the file is locked.
* @param in name Is the object interprocess global name, visible for all processes in the same machine.
*/
static bool is_locked(
const std::string& name)
{
bool was_lock_created;

auto file_path = RobustLock::get_file_path(name);

int fd;
if ((fd = open_and_lock_file(file_path, &was_lock_created)) == -1)
{
return true;
}

unlock_and_close(fd, name);

return false;
}

/**
* Unlock the interprocess lock.
*/
~RobustExclusiveLock()
{
unlock_and_close();
unlock_and_close(fd_, name_);
}

/**
Expand All @@ -99,9 +127,9 @@ class RobustExclusiveLock

#ifdef _MSC_VER

int open_and_lock_file(
static int open_and_lock_file(
const std::string& file_path,
bool* was_lock_created) const
bool* was_lock_created)
{
int test_exist;
auto ret = _sopen_s(&test_exist, file_path.c_str(), O_RDONLY, _SH_DENYRW, _S_IREAD | _S_IWRITE);
Expand All @@ -117,31 +145,31 @@ class RobustExclusiveLock

if (ret != 0)
{
char errmsg[1024];
strerror_s(errmsg, sizeof(errmsg), errno);
throw std::runtime_error("failed to open/create " + file_path + " " + std::string(errmsg));
return -1;
}

*was_lock_created = true;

return fd;
}

void unlock_and_close()
static void unlock_and_close(
int fd,
const std::string& name)
{
_close(fd_);
_close(fd);

if (0 != std::remove(RobustLock::get_file_path(name_).c_str()))
if (0 != std::remove(RobustLock::get_file_path(name).c_str()))
{
logWarning(RTPS_TRANSPORT_SHM, "Failed to remove " << RobustLock::get_file_path(name_));
logWarning(RTPS_TRANSPORT_SHM, "Failed to remove " << RobustLock::get_file_path(name));
}
}

#else

int open_and_lock_file(
static int open_and_lock_file(
const std::string& file_path,
bool* was_lock_created) const
bool* was_lock_created)
{
auto fd = open(file_path.c_str(), O_RDONLY, 0666);

Expand All @@ -157,31 +185,33 @@ class RobustExclusiveLock

if (fd == -1)
{
throw std::runtime_error(("failed to open/create " + file_path + " " + std::strerror(errno)).c_str());
return -1;
}

// Lock the file
if (0 != flock(fd, LOCK_EX | LOCK_NB))
{
close(fd);
throw std::runtime_error(("failed to lock " + file_path).c_str());
return -1;
}

return fd;
}

void unlock_and_close()
static void unlock_and_close(
int fd,
const std::string& name)
{
flock(fd_, LOCK_UN | LOCK_NB);
close(fd_);
flock(fd, LOCK_UN | LOCK_NB);
close(fd);

if (0 != std::remove(RobustLock::get_file_path(name_).c_str()))
if (0 != std::remove(RobustLock::get_file_path(name).c_str()))
{
logWarning(RTPS_TRANSPORT_SHM, "Failed to remove " << RobustLock::get_file_path(name_));
logWarning(RTPS_TRANSPORT_SHM, "Failed to remove " << RobustLock::get_file_path(name));
}
}

#endif
#endif // ifdef _MSC_VER

};

Expand Down
Loading