Skip to content

Commit

Permalink
back out capture changes
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSylvester committed Nov 20, 2024
1 parent f446e37 commit 0d90208
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 90 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/util/traits.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/type_list.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/unitbezier.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/unique_function.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/util.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/variant.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/vectors.hpp
Expand Down
1 change: 0 additions & 1 deletion bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,6 @@ MLN_CORE_HEADERS = [
"include/mbgl/util/traits.hpp",
"include/mbgl/util/type_list.hpp",
"include/mbgl/util/unitbezier.hpp",
"include/mbgl/util/unique_function.hpp",
"include/mbgl/util/util.hpp",
"include/mbgl/util/variant.hpp",
"include/mbgl/util/vectors.hpp",
Expand Down
71 changes: 0 additions & 71 deletions include/mbgl/util/unique_function.hpp

This file was deleted.

38 changes: 21 additions & 17 deletions src/mbgl/tile/tile_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <mbgl/actor/scheduler.hpp>
#include <mbgl/util/instrumentation.hpp>
#include <mbgl/util/unique_function.hpp>

#include <cassert>

Expand Down Expand Up @@ -41,17 +40,17 @@ void TileCache::setSize(size_t size_) {
}

namespace {
// Capturing `std::vector<std::unique_ptr<Tile>>` directly produces an error related to
// copying, but this somehow avoids the same problem. It may be possible to eliminate it.
/// This exists solely to prevent a problem where temporary lambda captures
/// are retained for the duration of the scope instead of being destroyed immediately.
struct CaptureWrapper {
std::vector<std::unique_ptr<Tile>> releases;

CaptureWrapper(std::vector<std::unique_ptr<Tile>>&& x)
: releases(std::move(x)) {}
CaptureWrapper(std::vector<std::unique_ptr<Tile>>&& items_)
: items(items_.size()) {
std::ranges::move(items_, items.begin());
}
CaptureWrapper(CaptureWrapper&&) = default;
CaptureWrapper& operator=(CaptureWrapper&&) = default;
CaptureWrapper(CaptureWrapper const&) = delete;
CaptureWrapper& operator=(CaptureWrapper const&) = delete;
CaptureWrapper(const CaptureWrapper& other)
: items(other.items) {}
std::vector<std::shared_ptr<Tile>> items;
};
} // namespace

Expand Down Expand Up @@ -79,18 +78,23 @@ void TileCache::deferPendingReleases() {
CaptureWrapper wrap{std::move(pendingReleases)};
pendingReleases.clear();

threadPool.schedule(util::unique_function<void()>{[this, wrap_{std::move(wrap)}]() mutable {
// The `std::function` must be created in a separate statement from the `schedule` call.
// Creating a `std::function` from a lambda involves a copy, which is why we must use
// `shared_ptr` rather than `unique_ptr` for the capture. As a result, a temporary holds
// a reference until the construction is complete and the lambda is destroyed.
// If this temporary outlives the `schedule` call, and the function is executed immediately
// by a waiting thread and is already complete, that temporary reference ends up being the
// last one and the destruction actually occurs here on this thread.
std::function<void()> func{[tile_{CaptureWrapper{std::move(wrap)}}, this]() mutable {
MLN_TRACE_ZONE(deferPendingReleases lambda);
MLN_ZONE_VALUE(wrap_.releases.size());
tile_.items.clear();

// Run the deletions
wrap_.releases.clear();

// Wake up a waiting destructor
std::lock_guard counterLock{deferredSignalLock};
std::lock_guard<std::mutex> counterLock(deferredSignalLock);
deferredDeletionsPending--;
deferredSignal.notify_all();
}});
}};
threadPool.schedule(std::move(func));
}

void TileCache::add(const OverscaledTileID& key, std::unique_ptr<Tile>&& tile) {
Expand Down

0 comments on commit 0d90208

Please sign in to comment.