@@ -1065,6 +1065,27 @@ namespace mamba
1065
1065
#pragma GCC diagnostic pop
1066
1066
#endif
1067
1067
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
+
1068
1089
class LockedFilesRegistry
1069
1090
{
1070
1091
public:
@@ -1105,10 +1126,10 @@ namespace mamba
1105
1126
}
1106
1127
1107
1128
const auto absolute_file_path = fs::absolute (file_path);
1108
- std::scoped_lock lock{ mutex } ;
1129
+ auto data = m_data. synchronize () ;
1109
1130
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 ())
1112
1133
{
1113
1134
if (auto lockedfile = it->second .lock ())
1114
1135
{
@@ -1123,8 +1144,8 @@ namespace mamba
1123
1144
{
1124
1145
auto lockedfile = std::make_shared<LockFileOwner>(absolute_file_path, timeout);
1125
1146
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);
1128
1149
assert (is_lockfile_locked (*lockedfile));
1129
1150
return lockedfile;
1130
1151
}
@@ -1135,9 +1156,9 @@ namespace mamba
1135
1156
bool is_locked (const fs::u8path& file_path) const
1136
1157
{
1137
1158
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 ())
1141
1162
{
1142
1163
return !it->second .expired ();
1143
1164
}
@@ -1150,9 +1171,9 @@ namespace mamba
1150
1171
// note: the resulting value will be obsolete before returning.
1151
1172
bool is_locked (int fd) const
1152
1173
{
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 ())
1156
1177
{
1157
1178
return is_locked (it->second );
1158
1179
}
@@ -1167,25 +1188,7 @@ namespace mamba
1167
1188
std::atomic_bool m_is_file_locking_allowed{ true };
1168
1189
std::atomic<std::chrono::seconds> m_default_lock_timeout{ std::chrono::seconds::zero () };
1169
1190
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;
1189
1192
};
1190
1193
1191
1194
static LockedFilesRegistry files_locked_by_this_process;
0 commit comments