Skip to content

Commit c67065b

Browse files
committed
LockedFilesRegistry uses synchronized_value
1 parent 9bef8a7 commit c67065b

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

libmamba/src/core/util.cpp

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,27 @@ namespace mamba
10651065
#pragma GCC diagnostic pop
10661066
#endif
10671067

1068+
struct LockedFilesData
1069+
{
1070+
// TODO: replace by something like boost::multiindex or equivalent to avoid having to
1071+
// handle 2 hashmaps
1072+
std::unordered_map<fs::u8path, std::weak_ptr<LockFileOwner>> locked_files; // TODO:
1073+
// consider
1074+
// replacing
1075+
// by real
1076+
// concurrent
1077+
// set to
1078+
// avoid
1079+
// having to
1080+
// lock the
1081+
// whole
1082+
// container
1083+
1084+
std::unordered_map<int, fs::u8path> fd_to_locked_path; // this is a workaround the
1085+
// usage of file descriptors on
1086+
// linux instead of paths
1087+
};
1088+
10681089
class LockedFilesRegistry
10691090
{
10701091
public:
@@ -1105,10 +1126,10 @@ namespace mamba
11051126
}
11061127

11071128
const auto absolute_file_path = fs::absolute(file_path);
1108-
std::scoped_lock lock{ mutex };
1129+
auto data = m_data.synchronize();
11091130

1110-
const auto it = locked_files.find(absolute_file_path);
1111-
if (it != locked_files.end())
1131+
const auto it = data->locked_files.find(absolute_file_path);
1132+
if (it != data->locked_files.end())
11121133
{
11131134
if (auto lockedfile = it->second.lock())
11141135
{
@@ -1123,8 +1144,8 @@ namespace mamba
11231144
{
11241145
auto lockedfile = std::make_shared<LockFileOwner>(absolute_file_path, timeout);
11251146
auto tracker = std::weak_ptr{ lockedfile };
1126-
locked_files.insert_or_assign(absolute_file_path, std::move(tracker));
1127-
fd_to_locked_path.insert_or_assign(lockedfile->fd(), absolute_file_path);
1147+
data->locked_files.insert_or_assign(absolute_file_path, std::move(tracker));
1148+
data->fd_to_locked_path.insert_or_assign(lockedfile->fd(), absolute_file_path);
11281149
assert(is_lockfile_locked(*lockedfile));
11291150
return lockedfile;
11301151
}
@@ -1135,9 +1156,9 @@ namespace mamba
11351156
bool is_locked(const fs::u8path& file_path) const
11361157
{
11371158
const auto absolute_file_path = fs::absolute(file_path);
1138-
std::scoped_lock lock{ mutex };
1139-
auto it = locked_files.find(file_path);
1140-
if (it != locked_files.end())
1159+
auto data = m_data.synchronize();
1160+
auto it = data->locked_files.find(file_path);
1161+
if (it != data->locked_files.end())
11411162
{
11421163
return !it->second.expired();
11431164
}
@@ -1150,9 +1171,9 @@ namespace mamba
11501171
// note: the resulting value will be obsolete before returning.
11511172
bool is_locked(int fd) const
11521173
{
1153-
std::scoped_lock lock{ mutex };
1154-
const auto it = fd_to_locked_path.find(fd);
1155-
if (it != fd_to_locked_path.end())
1174+
auto data = m_data.synchronize();
1175+
const auto it = data->fd_to_locked_path.find(fd);
1176+
if (it != data->fd_to_locked_path.end())
11561177
{
11571178
return is_locked(it->second);
11581179
}
@@ -1167,25 +1188,7 @@ namespace mamba
11671188
std::atomic_bool m_is_file_locking_allowed{ true };
11681189
std::atomic<std::chrono::seconds> m_default_lock_timeout{ std::chrono::seconds::zero() };
11691190

1170-
// TODO: replace by something like boost::multiindex or equivalent to avoid having to
1171-
// handle 2 hashmaps
1172-
std::unordered_map<fs::u8path, std::weak_ptr<LockFileOwner>> locked_files; // TODO:
1173-
// consider
1174-
// replacing
1175-
// by real
1176-
// concurrent
1177-
// set to
1178-
// avoid
1179-
// having to
1180-
// lock the
1181-
// whole
1182-
// container
1183-
1184-
std::unordered_map<int, fs::u8path> fd_to_locked_path; // this is a workaround the
1185-
// usage of file descriptors on
1186-
// linux instead of paths
1187-
mutable std::recursive_mutex mutex; // TODO: replace by synchronized_value once
1188-
// available
1191+
util::synchronized_value<LockedFilesData, std::recursive_mutex> m_data;
11891192
};
11901193

11911194
static LockedFilesRegistry files_locked_by_this_process;

0 commit comments

Comments
 (0)