Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #146 from mapbox/146-remove_request_coalescing
Browse files Browse the repository at this point in the history
Remove request coalescing
  • Loading branch information
mikemorris committed Aug 14, 2015
2 parents 7b72f9e + d80d2d5 commit 6ec8290
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 47 deletions.
58 changes: 18 additions & 40 deletions src/node_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include <mbgl/storage/request.hpp>

#include <algorithm>

namespace node_mbgl {

struct NodeFileSource::Action {
Expand Down Expand Up @@ -39,16 +37,13 @@ mbgl::Request* NodeFileSource::request(const mbgl::Resource& resource, uv_loop_t
auto req = new mbgl::Request(resource, loop, std::move(callback));

std::lock_guard<std::mutex> lock(observersMutex);
auto it = observers.find(resource);
if (it == observers.end()) {
observers[resource] = { req };

// This function can be called from any thread. Make sure we're executing the actual call in the
// file source loop by sending it over the queue. It will be processed in processAction().
queue->send(Action{ Action::Add, resource });
} else {
it->second.emplace_back(req);
}
assert(observers.find(resource) == observers.end());
observers[resource] = req;

// This function can be called from any thread. Make sure we're executing the actual call in the
// file source loop by sending it over the queue. It will be processed in processAction().
queue->send(Action{ Action::Add, resource });

return req;
}
Expand All @@ -58,25 +53,16 @@ void NodeFileSource::cancel(mbgl::Request* req) {

std::lock_guard<std::mutex> lock(observersMutex);

auto observersIter = observers.find(req->resource);
if (observersIter == observers.end()) {
return;
}

auto& observersList = observersIter->second;
auto observersListIter = std::find(observersList.begin(), observersList.end(), req);
if (observersListIter == observersList.end()) {
auto it = observers.find(req->resource);
if (it == observers.end()) {
return;
}

observersList.erase(observersListIter);
if (observersList.empty()) {
observers.erase(observersIter);
observers.erase(it);

// This function can be called from any thread. Make sure we're executing the actual call in the
// file source loop by sending it over the queue. It will be processed in processAction().
queue->send(Action{ Action::Cancel, req->resource });
}
// This function can be called from any thread. Make sure we're executing the actual call in the
// file source loop by sending it over the queue. It will be processed in processAction().
queue->send(Action{ Action::Cancel, req->resource });

req->destruct();
}
Expand Down Expand Up @@ -161,23 +147,15 @@ void NodeFileSource::notify(const mbgl::Resource& resource, const std::shared_pt
}
}

std::vector<mbgl::Request*> observersList;

{
std::lock_guard<std::mutex> lock(observersMutex);

auto observersIter = observers.find(resource);
if (observersIter == observers.end()) {
return;
}
std::lock_guard<std::mutex> lock(observersMutex);

observersList.swap(observersIter->second);
observers.erase(observersIter);
auto observersIt = observers.find(resource);
if (observersIt == observers.end()) {
return;
}

for (auto request : observersList) {
request->notify(response);
}
observersIt->second->notify(response);
observers.erase(observersIt);
}

}
12 changes: 5 additions & 7 deletions src/node_file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>

namespace node_mbgl {

Expand Down Expand Up @@ -47,13 +46,12 @@ class NodeFileSource : public mbgl::FileSource {
#endif

// The observers list will hold pointers to all the requests waiting
// for a particular resource. It is also used for coalescing requests,
// so we don't ask for the same resources twice. The access must be
// guarded by a mutex because the list is also accessed by a thread
// from the mbgl::Map object and from the main thread when notifying
// requests of completion. Concurrent access is specially needed when
// for a particular resource. The access must be guarded by a mutex
// because the list is also accessed by a thread from the mbgl::Map
// object and from the main thread when notifying requests of
// completion. Concurrent access is specially needed when
// canceling a request to avoid a deadlock (see #129).
std::unordered_map<mbgl::Resource, std::vector<mbgl::Request*>, mbgl::Resource::Hash> observers;
std::unordered_map<mbgl::Resource, mbgl::Request*, mbgl::Resource::Hash> observers;
std::mutex observersMutex;

Queue *queue = nullptr;
Expand Down

0 comments on commit 6ec8290

Please sign in to comment.