Skip to content

Commit

Permalink
weejobs.h - replace list::size with a safer/faster version to prevent…
Browse files Browse the repository at this point in the history
… a crash on older compilers
  • Loading branch information
gwaldron committed Mar 8, 2024
1 parent 194cfb9 commit 5702f02
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/osgEarth/weejobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ namespace WEEJOBS_NAMESPACE
{
std::lock_guard<std::mutex> lock(_queue_mutex);
_queue.clear();
_queue_size = 0;
_metrics.canceled += _metrics.pending;
_metrics.pending = 0;
}
Expand All @@ -559,6 +560,8 @@ namespace WEEJOBS_NAMESPACE
std::lock_guard<std::mutex> lock(_queue_mutex);

_queue.emplace_back(detail::job{ context, delegate });
_queue_size++;

_metrics.pending++;
_metrics.total++;
_block.notify_one();
Expand Down Expand Up @@ -586,7 +589,7 @@ namespace WEEJOBS_NAMESPACE
std::lock_guard<std::mutex> lock(_queue_mutex);
return _take_job(output, false);
}
else if (!_done && !_queue.empty())
else if (!_done && _queue_size > 0)
{
auto ptr = _queue.end();
float highest_priority = -FLT_MAX;
Expand All @@ -608,6 +611,7 @@ namespace WEEJOBS_NAMESPACE

output = std::move(*ptr);
_queue.erase(ptr);
_queue_size--;
_metrics.pending--;
return true;
}
Expand Down Expand Up @@ -638,6 +642,7 @@ namespace WEEJOBS_NAMESPACE

bool _can_steal_work = true;
std::list<detail::job> _queue;
std::atomic_int _queue_size = { 0 }; // don't use list::size(), it's slow and not atomic
mutable std::mutex _queue_mutex; // protect access to the queue
mutable std::mutex _quit_mutex; // protects access to _done
std::atomic<unsigned> _target_concurrency; // target number of concurrent threads in the pool
Expand Down Expand Up @@ -877,7 +882,7 @@ namespace WEEJOBS_NAMESPACE
// work-stealing enabled: wait until any queue is non-empty
_block.wait(lock, [this]() { return get_metrics()->total_pending() > 0 || _done; });

if (!_done && !_queue.empty())
if (!_done && _queue_size > 0)
{
have_next = _take_job(next, false);
}
Expand All @@ -893,9 +898,9 @@ namespace WEEJOBS_NAMESPACE
std::unique_lock<std::mutex> lock(_queue_mutex);

// wait until just our local queue is non-empty
_block.wait(lock, [this] { return !_queue.empty() || _done; });
_block.wait(lock, [this] { return (_queue_size > 0) || _done; });

if (!_done && !_queue.empty())
if (!_done && _queue_size > 0)
{
have_next = _take_job(next, false);
}
Expand Down Expand Up @@ -976,6 +981,7 @@ namespace WEEJOBS_NAMESPACE
}
}
_queue.clear();
_queue_size = 0;

// wake up all threads so they can exit
_block.notify_all();
Expand Down Expand Up @@ -1008,10 +1014,9 @@ namespace WEEJOBS_NAMESPACE
{
if (pool != thief)
{
auto num_jobs = pool->_queue.size();
if (num_jobs > max_num_jobs)
if (pool->_queue_size > max_num_jobs)
{
max_num_jobs = num_jobs;
max_num_jobs = pool->_queue_size;
pool_with_most_jobs = pool;
}
}
Expand Down

0 comments on commit 5702f02

Please sign in to comment.