Skip to content

Commit

Permalink
Threading: simplify the job dispatch signature to not require a templ…
Browse files Browse the repository at this point in the history
…ate argument for the return type (like rocky)
  • Loading branch information
gwaldron committed Feb 6, 2024
1 parent 45f4283 commit 595ac60
Show file tree
Hide file tree
Showing 17 changed files with 34 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ void computeIntersectionsThreaded(osg::Node* node, std::vector< IntersectionQuer
Job job;
job.setArena("oe.intersections");
job.setGroup(&intersections);
job.dispatch([node, curStart, curSize, &queries](Cancelable*) {
job.dispatch_and_forget([node, curStart, curSize, &queries](Cancelable*) {
computeIntersections(node, queries, curStart, curSize);
});
++numJobs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ main(int argc, char** argv)

for(const auto key : keys)
{
Job(&arena).dispatch(
[&app, key](Cancelable*)
Job(&arena).dispatch_and_forget([&app, key](Cancelable*)
{
app.exportKey(key);
}
Expand Down
7 changes: 2 additions & 5 deletions src/osgEarth/ElevationPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,12 +1059,9 @@ AsyncElevationSampler::getSample(const GeoPoint& p)
}

Future<ElevationSample>
AsyncElevationSampler::getSample(
const GeoPoint& point,
const Distance& resolution)
AsyncElevationSampler::getSample(const GeoPoint& point, const Distance& resolution)
{
return Job(_arena).dispatch<ElevationSample>(
[=](Cancelable* cancelable)
return Job(_arena).dispatch([=](Cancelable* cancelable)
{
ElevationSample sample;
if (cancelable == nullptr || !cancelable->isCanceled())
Expand Down
2 changes: 1 addition & 1 deletion src/osgEarth/ImGui/LayersGUI
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ namespace osgEarth

if (key.valid())
{
_imageLayerValueUnderMouse = Job().dispatch<Result<ValueUnderMouse>>([this, key, p](Cancelable* c)
_imageLayerValueUnderMouse = Job().dispatch([this, key, p](Cancelable* c)
{
ValueUnderMouse value;
osg::ref_ptr<ProgressCallback> prog = new ProgressCallback(c);
Expand Down
3 changes: 1 addition & 2 deletions src/osgEarth/ImageLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,8 +932,7 @@ FutureTexture2D::dispatch() const
// prioritize higher LOD tiles.
job.setPriority(key.getLOD());

_result = job.dispatch<GeoImage>(
[layer_ptr, key](Cancelable* progress) mutable
_result = job.dispatch([layer_ptr, key](Cancelable* progress) mutable
{
GeoImage result;
osg::ref_ptr<ImageLayer> safe(layer_ptr);
Expand Down
2 changes: 1 addition & 1 deletion src/osgEarth/PagedNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ PagedNode2::load(float priority, const osg::Object* host)

_job.setPriority(priority);

_loaded = _job.dispatch<Loaded>(
_loaded = _job.dispatch(
[load, callbacks_weakptr, preCompile](Cancelable* c)
{
Loaded result;
Expand Down
6 changes: 2 additions & 4 deletions src/osgEarth/TDTiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,7 @@ namespace
parentTileset, uri, options);

JobArena* arena = JobArena::get("oe.3dtiles");
return Job(arena).dispatch<ReadTileData>(
[operation, options](Cancelable* progress)
return Job(arena).dispatch([operation, options](Cancelable* progress)
{
return operation->loadTileSet(progress);
}
Expand All @@ -575,8 +574,7 @@ namespace
{
JobArena* arena = JobArena::get("oe.3dtiles");

return Job(arena).dispatch<ReadTileData>(
[uri, options](Cancelable* progress)
return Job(arena).dispatch([uri, options](Cancelable* progress)
{
osg::ref_ptr<osg::Node> node = uri.getNode(options.get(), nullptr);
if (node.valid())
Expand Down
22 changes: 11 additions & 11 deletions src/osgEarth/Threading
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ namespace osgEarth { namespace Threading
*
* Job job;
*
* Future<int> result = job.dispatch<int>(
* Future<int> result = job.dispatch(
* [a, b](Cancelable* progress) {
* return (a + b);
* }
Expand Down Expand Up @@ -854,14 +854,13 @@ namespace osgEarth { namespace Threading
//! @func Function to execute
//! @return Future result. If this objects goes out of scope,
//! the job will be canceled and may not run at all.
template<typename T>
Future<T> dispatch(
std::function<T(Cancelable*)> func) const;
template<typename FUNC, typename RETURN_TYPE = std::result_of<FUNC(Cancelable*)>::type>
Future<RETURN_TYPE> dispatch(FUNC function) const;

//! Dispatch the job for asynchronous execution and forget
//! about it. No return value.
inline void dispatch(
std::function<void(Cancelable*)> func) const;
template<typename FUNC>
void dispatch_and_forget(FUNC function) const;

private:
std::string _name;
Expand Down Expand Up @@ -1077,11 +1076,11 @@ namespace osgEarth { namespace Threading
friend class Job; // allow access to private dispatch method
};

template<typename T>
Future<T> Job::dispatch(std::function<T(Cancelable*)> function) const
template<typename FUNC, typename RETURN_TYPE>
Future<RETURN_TYPE> Job::dispatch(FUNC function) const
{
Promise<T> promise;
Future<T> future = promise; // .getFuture();
Promise<RETURN_TYPE> promise;
Future<RETURN_TYPE> future = promise;
JobArena::Delegate delegate = [function, promise]() mutable
{
bool good = !promise.empty();
Expand All @@ -1094,7 +1093,8 @@ namespace osgEarth { namespace Threading
return std::move(future);
}

void Job::dispatch(std::function<void(Cancelable*)> function) const
template<typename FUNC>
void Job::dispatch_and_forget(FUNC function) const
{
auto name = _arena ? _arena->_name : _arenaName;
JobArena* arena = JobArena::get(name);
Expand Down
2 changes: 1 addition & 1 deletion src/osgEarth/TileVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ bool MultithreadedTileVisitor::handleTile(const TileKey& key)

Job job(JobArena::get(MTTV), &_group);
job.setName("handleTile");
job.dispatch(delegate);
job.dispatch_and_forget(delegate);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/osgEarthDrivers/cache_filesystem/FileSystemCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ namespace
_writeCacheRWM.write_unlock();

// asynchronous write
Job(_jobArena.get()).dispatch(write_op);
Job(_jobArena.get()).dispatch_and_forget(write_op);
}

else
Expand Down
2 changes: 1 addition & 1 deletion src/osgEarthDrivers/engine_rex/LoadTileData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ LoadTileDataOperation::dispatch(bool async)
Job job;
job.setArena(ARENA_LOAD_TILE);
job.setPriorityFunction(priority_func);
_result = job.dispatch<LoadResult>(load);
_result = job.dispatch(load);
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions src/osgEarthDrivers/engine_rex/RexTerrainEngineNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,9 @@ RexTerrainEngineNode::refresh(bool forceDirty)
tileNode->initializeData();

// And load the tile's data
load.dispatch([tileNode](Cancelable*) {
tileNode->loadSync();
load.dispatch_and_forget([tileNode](Cancelable*)
{
tileNode->loadSync();
});

OE_DEBUG << " - " << (i + 1) << "/" << keys.size() << " : " << keys[i].str() << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions src/osgEarthDrivers/engine_rex/TileNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ TileNode::createChildren()
Job job;
job.setArena(ARENA_CREATE_CHILD);
job.setName(_key.str());
_createChildrenFutureResult = job.dispatch<CreateChildrenResult>(createChildrenOperation);
_createChildrenFutureResult = job.dispatch(createChildrenOperation);
}

else if (_createChildrenFutureResult.available())
Expand Down Expand Up @@ -753,7 +753,7 @@ TileNode::createChildren()
job.setArena(ARENA_CREATE_CHILD);
job.setName(childkey.str());

_createChildResults.emplace_back(job.dispatch<CreateChildResult>(createChildOperation));
_createChildResults.emplace_back(job.dispatch(createChildOperation));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/osgEarthProcedural/ImGui/LifeMapLayerGUI
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ namespace osgEarth
TileKey key = _lifemap->getBestAvailableTileKey(keyUnderMouse, false);
if (key.valid())
{
_lifemapUnderMouse = Job().dispatch<osg::Vec4>([this, key, pointUnderMouse](Cancelable* c)
_lifemapUnderMouse = Job().dispatch([this, key, pointUnderMouse](Cancelable* c)
{
osg::Vec4 result(0, 0, 0, 1);
osg::ref_ptr<ProgressCallback> prog = new ProgressCallback(c);
Expand All @@ -255,7 +255,7 @@ namespace osgEarth
TileKey key = coverage->getBestAvailableTileKey(keyUnderMouse, false);
if (key.valid())
{
_landcoverUnderMouse = Job().dispatch<LandCoverSample>([this, key, pointUnderMouse](Cancelable *c)
_landcoverUnderMouse = Job().dispatch([this, key, pointUnderMouse](Cancelable *c)
{
LandCoverSample result;
osg::ref_ptr<ProgressCallback> prog = new ProgressCallback(c);
Expand Down
2 changes: 1 addition & 1 deletion src/osgEarthProcedural/ImGui/VegetationLayerGUI
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ namespace osgEarth
if (key.valid())
{
_biomeUnderMouseKey = key;
_biomeUnderMouse = Job().dispatch<const Biome*>([&, key, p](Cancelable* c)
_biomeUnderMouse = Job().dispatch([&, key, p](Cancelable* c)
{
const Biome* result = nullptr;
osg::ref_ptr<ProgressCallback> prog = new ProgressCallback(c);
Expand Down
2 changes: 1 addition & 1 deletion src/osgEarthProcedural/TextureSplattingLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ TextureSplattingLayer::prepareForRendering(TerrainEngine* engine)
};

// Load material asynchronously
_materialsJob = Job().dispatch<Materials::Ptr>(loadMaterials);
_materialsJob = Job().dispatch(loadMaterials);
}
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/osgEarthProcedural/VegetationLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ VegetationLayer::checkForNewAssets() const
Job job;
job.setName("VegetationLayer asset loader");
job.setArena(JOB_ARENA_VEGETATION);
_newAssets = job.dispatch<AssetsByGroup>(loadNewAssets);
_newAssets = job.dispatch(loadNewAssets);

return true;
}
Expand Down Expand Up @@ -1229,7 +1229,7 @@ VegetationLayer::createDrawableAsync(
job.setName("Vegetation create drawable");
job.setArena(JOB_ARENA_VEGETATION);
job.setPriority(-range); // closer is sooner
return job.dispatch<osg::ref_ptr<osg::Drawable>>(function);
return job.dispatch(function);
}

#undef RAND
Expand Down

0 comments on commit 595ac60

Please sign in to comment.