Skip to content

Commit

Permalink
Simplify RequestDispatcher creation and thread exit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
csciguy8 committed Nov 6, 2023
1 parent 5327545 commit 34db4a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ struct TileLoadWork {
}
};

class RequestDispatcher
: public CesiumUtility::ReferenceCountedNonThreadSafe<RequestDispatcher> {
class RequestDispatcher {

public:
RequestDispatcher(
Expand All @@ -93,7 +92,7 @@ class RequestDispatcher

// Thread safe members
std::mutex _requestsLock;
bool _requestDispatcherIdle = true;
bool _dispatcherIdle = true;
bool _exitSignaled = false;
std::vector<TileLoadWork> _queuedRequests;
std::vector<TileLoadWork> _doneRequests;
Expand Down Expand Up @@ -559,7 +558,7 @@ class CESIUM3DTILESSELECTION_API Tileset final {
// scratch variable so that it can allocate only when growing bigger.
std::vector<const TileOcclusionRendererProxy*> _childOcclusionProxies;

CesiumUtility::IntrusivePointer<RequestDispatcher> _pRequestDispatcher;
RequestDispatcher _requestDispatcher;

CesiumUtility::IntrusivePointer<TilesetContentManager>
_pTilesetContentManager;
Expand Down
33 changes: 13 additions & 20 deletions Cesium3DTilesSelection/src/Tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ Tileset::Tileset(
_previousFrameNumber(0),
_distances(),
_childOcclusionProxies(),
_pRequestDispatcher(
new RequestDispatcher(_asyncSystem, _externals.pAssetAccessor)),
_requestDispatcher(_asyncSystem, _externals.pAssetAccessor),
_pTilesetContentManager{new TilesetContentManager(
_externals,
_options,
Expand All @@ -64,8 +63,7 @@ Tileset::Tileset(
_previousFrameNumber(0),
_distances(),
_childOcclusionProxies(),
_pRequestDispatcher(
new RequestDispatcher(_asyncSystem, _externals.pAssetAccessor)),
_requestDispatcher(_asyncSystem, _externals.pAssetAccessor),
_pTilesetContentManager{new TilesetContentManager(
_externals,
_options,
Expand All @@ -84,8 +82,7 @@ Tileset::Tileset(
_previousFrameNumber(0),
_distances(),
_childOcclusionProxies(),
_pRequestDispatcher(
new RequestDispatcher(_asyncSystem, _externals.pAssetAccessor)),
_requestDispatcher(_asyncSystem, _externals.pAssetAccessor),
_pTilesetContentManager{new TilesetContentManager(
_externals,
_options,
Expand Down Expand Up @@ -1458,12 +1455,12 @@ void Tileset::_processWorkerThreadLoadQueue() {
return;

// Add completed request work
_pRequestDispatcher->TakeCompletedWork(availableSlots, workToDispatch);
_requestDispatcher.TakeCompletedWork(availableSlots, workToDispatch);
availableSlots -= (int32_t)workToDispatch.size();
assert(availableSlots >= 0);

// Add processing work
if (newProcessingWork.size () > 0 && availableSlots > 0) {
if (newProcessingWork.size() > 0 && availableSlots > 0) {
std::sort(newProcessingWork.begin(), newProcessingWork.end());
int countToAdd =
std::min((int32_t)newProcessingWork.size(), availableSlots);
Expand All @@ -1474,7 +1471,7 @@ void Tileset::_processWorkerThreadLoadQueue() {
}

// Dispatch it
if (workToDispatch.size () > 0)
if (workToDispatch.size() > 0)
dispatchProcessingWork(workToDispatch);

/*
Expand Down Expand Up @@ -1687,11 +1684,11 @@ void Tileset::addWorkToRequestDispatcher(
pTile->setState(TileLoadState::ContentLoading);
}

_pRequestDispatcher->QueueRequestWork(
_requestDispatcher.QueueRequestWork(
workVector,
this->_pTilesetContentManager->getRequestHeaders());

_pRequestDispatcher->WakeIfNeeded();
_requestDispatcher.WakeIfNeeded();
}

void Tileset::dispatchProcessingWork(std::vector<TileLoadWork>& workVector) {
Expand Down Expand Up @@ -1827,9 +1824,9 @@ void RequestDispatcher::TakeCompletedWork(
void RequestDispatcher::WakeIfNeeded() {
{
std::lock_guard<std::mutex> lock(_requestsLock);
if (!_requestDispatcherIdle)
if (!_dispatcherIdle)
return;
_requestDispatcherIdle = false;
_dispatcherIdle = false;
}

_asyncSystem.runInThreadPool(this->_requestThreadPool, [this]() {
Expand Down Expand Up @@ -1857,16 +1854,12 @@ void RequestDispatcher::WakeIfNeeded() {
// Continue loop until our queue is empty or exit is signaled
{
std::lock_guard<std::mutex> lock(_requestsLock);
if (_queuedRequests.empty() || _exitSignaled)
if (_queuedRequests.empty() || _exitSignaled) {
this->_dispatcherIdle = true;
break;
}
}
}

// All work is queued. Exit and let caller wake us later
{
std::lock_guard<std::mutex> lock(_requestsLock);
this->_requestDispatcherIdle = true;
}
});
}

Expand Down

0 comments on commit 34db4a4

Please sign in to comment.