Skip to content

Commit 1e1d5fa

Browse files
Fix macOS and Windows shared mem wait for detach (#7321)
1 parent 8f7b36f commit 1e1d5fa

File tree

9 files changed

+339
-362
lines changed

9 files changed

+339
-362
lines changed

include/engine/data_watchdog.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "engine/datafacade_factory.hpp"
77

88
#include "storage/shared_datatype.hpp"
9-
#include "storage/shared_memory.hpp"
109
#include "storage/shared_monitor.hpp"
1110

1211
#include <boost/interprocess/sync/named_upgradable_mutex.hpp>
@@ -59,8 +58,8 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
5958
facade_factory =
6059
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
6160
std::make_shared<datafacade::SharedMemoryAllocator>(
62-
std::vector<storage::SharedRegionRegister::ShmKey>{
63-
static_region.shm_key, updatable_region.shm_key}));
61+
std::vector<storage::ProjID>{static_region.proj_id,
62+
updatable_region.proj_id}));
6463
}
6564
}
6665

@@ -112,18 +111,18 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
112111
updatable_region = *updatable_shared_region;
113112
}
114113

115-
util::Log() << "updated facade to regions " << (int)static_region.shm_key << " and "
116-
<< (int)updatable_region.shm_key << " with timestamps "
117-
<< static_region.timestamp << " and " << updatable_region.timestamp;
118-
119114
{
120115
boost::unique_lock<boost::shared_mutex> swap_lock(factory_mutex);
121116
facade_factory =
122117
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
123118
std::make_shared<datafacade::SharedMemoryAllocator>(
124-
std::vector<storage::SharedRegionRegister::ShmKey>{
125-
static_region.shm_key, updatable_region.shm_key}));
119+
std::vector<storage::ProjID>{static_region.proj_id,
120+
updatable_region.proj_id}));
126121
}
122+
123+
util::Log() << "updated facade to regions " << (int)static_region.proj_id << " and "
124+
<< (int)updatable_region.proj_id << " with timestamps "
125+
<< static_region.timestamp << " and " << updatable_region.timestamp;
127126
}
128127

129128
util::Log() << "DataWatchdog thread stopped";

include/engine/datafacade/shared_memory_allocator.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ namespace osrm::engine::datafacade
1919
class SharedMemoryAllocator final : public ContiguousBlockAllocator
2020
{
2121
public:
22-
explicit SharedMemoryAllocator(
23-
const std::vector<storage::SharedRegionRegister::ShmKey> &shm_keys);
22+
explicit SharedMemoryAllocator(const std::vector<storage::ProjID> &proj_ids);
2423
~SharedMemoryAllocator() override final;
2524

2625
// interface to give access to the datafacades

include/storage/shared_datatype.hpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
#define SHARED_DATA_TYPE_HPP
33

44
#include "storage/block.hpp"
5-
#include "storage/io_fwd.hpp"
65

76
#include "util/exception.hpp"
8-
#include "util/exception_utils.hpp"
97

108
#include <array>
119
#include <cstdint>
1210
#include <map>
13-
#include <numeric>
1411
#include <unordered_set>
1512

1613
namespace osrm::storage
@@ -67,10 +64,7 @@ class BaseDataLayout
6764
return GetBlock(name).byte_size;
6865
}
6966

70-
inline bool HasBlock(const std::string &name) const
71-
{
72-
return blocks.find(name) != blocks.end();
73-
}
67+
inline bool HasBlock(const std::string &name) const { return blocks.contains(name); }
7468

7569
// Depending on the name prefix this function either lists all blocks with the same prefix
7670
// or all entries in the sub-directory.
@@ -192,13 +186,19 @@ class TarDataLayout final : public BaseDataLayout
192186
}
193187
};
194188

189+
// The second parameter passed to ftok(). See: man 3 ftok
190+
// It should actually be an int, but for compatibility with earlier versions of OSRM it
191+
// is an uint16. It should't matter since, according to the man page, only the lowest 8
192+
// bits are used.
193+
using ProjID = uint16_t;
194+
195195
struct SharedRegion
196196
{
197197
static constexpr const int MAX_NAME_LENGTH = 254;
198198

199199
SharedRegion() : name{0}, timestamp{0} {}
200-
SharedRegion(const std::string &name_, std::uint64_t timestamp, std::uint16_t shm_key)
201-
: name{0}, timestamp{timestamp}, shm_key{shm_key}
200+
SharedRegion(const std::string &name_, std::uint64_t timestamp, ProjID proj_id)
201+
: name{0}, timestamp{timestamp}, proj_id{proj_id}
202202
{
203203
std::copy_n(name_.begin(), std::min<std::size_t>(MAX_NAME_LENGTH, name_.size()), name);
204204
}
@@ -207,7 +207,7 @@ struct SharedRegion
207207

208208
char name[MAX_NAME_LENGTH + 1];
209209
std::uint64_t timestamp;
210-
std::uint16_t shm_key = 0;
210+
ProjID proj_id = 0;
211211
};
212212

213213
// Keeps a list of all shared regions in a fixed-sized struct
@@ -216,7 +216,6 @@ struct SharedRegionRegister
216216
{
217217
using RegionID = std::uint16_t;
218218
static constexpr const RegionID INVALID_REGION_ID = std::numeric_limits<RegionID>::max();
219-
using ShmKey = decltype(SharedRegion::shm_key);
220219

221220
// Returns the key of the region with the given name
222221
RegionID Find(const std::string &name) const
@@ -238,7 +237,7 @@ struct SharedRegionRegister
238237
}
239238
}
240239

241-
RegionID Register(const std::string &name, ShmKey key)
240+
RegionID Register(const std::string &name, ProjID proj_id)
242241
{
243242
auto iter = std::find_if(
244243
regions.begin(), regions.end(), [&](const auto &region) { return region.IsEmpty(); });
@@ -250,7 +249,7 @@ struct SharedRegionRegister
250249
else
251250
{
252251
constexpr std::uint32_t INITIAL_TIMESTAMP = 1;
253-
*iter = SharedRegion{name, INITIAL_TIMESTAMP, key};
252+
*iter = SharedRegion{name, INITIAL_TIMESTAMP, proj_id};
254253
RegionID key = std::distance(regions.begin(), iter);
255254
return key;
256255
}
@@ -271,7 +270,7 @@ struct SharedRegionRegister
271270

272271
auto &GetRegion(const RegionID key) { return regions[key]; }
273272

274-
ShmKey ReserveKey()
273+
ProjID ReserveKey()
275274
{
276275
auto free_key_iter = std::find(shm_key_in_use.begin(), shm_key_in_use.end(), false);
277276
if (free_key_iter == shm_key_in_use.end())
@@ -283,7 +282,7 @@ struct SharedRegionRegister
283282
return std::distance(shm_key_in_use.begin(), free_key_iter);
284283
}
285284

286-
void ReleaseKey(ShmKey key) { shm_key_in_use[key] = false; }
285+
void ReleaseKey(ProjID proj_id) { shm_key_in_use[proj_id] = false; }
287286

288287
static constexpr const std::size_t MAX_SHARED_REGIONS = 512;
289288
static_assert(MAX_SHARED_REGIONS < std::numeric_limits<RegionID>::max(),

0 commit comments

Comments
 (0)