Coverage Report

Created: 2026-07-25 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vvdec/source/Lib/Utilities/ThreadPool.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
The copyright in this software is being made available under the Clear BSD
3
License, included below. No patent rights, trademark rights and/or 
4
other Intellectual Property Rights other than the copyrights concerning 
5
the Software are granted under this license.
6
7
The Clear BSD License
8
9
Copyright (c) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC Authors.
10
All rights reserved.
11
12
Redistribution and use in source and binary forms, with or without modification,
13
are permitted (subject to the limitations in the disclaimer below) provided that
14
the following conditions are met:
15
16
     * Redistributions of source code must retain the above copyright notice,
17
     this list of conditions and the following disclaimer.
18
19
     * Redistributions in binary form must reproduce the above copyright
20
     notice, this list of conditions and the following disclaimer in the
21
     documentation and/or other materials provided with the distribution.
22
23
     * Neither the name of the copyright holder nor the names of its
24
     contributors may be used to endorse or promote products derived from this
25
     software without specific prior written permission.
26
27
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
28
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
36
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
POSSIBILITY OF SUCH DAMAGE.
39
40
41
------------------------------------------------------------------------------------------- */
42
43
#include "ThreadPool.h"
44
45
#include <chrono>
46
47
#if __linux
48
# include <pthread.h>
49
#endif
50
51
52
namespace vvdec
53
{
54
using namespace std::chrono_literals;
55
56
std::mutex Barrier::s_exceptionLock{};
57
58
// block threads after busy-waiting this long
59
253
const static auto VVDEC_BUSY_WAIT_TIME_MIN = [] {
60
253
  const char* env = getenv( "VVDEC_BUSY_WAIT_TIME_MIN" );
61
253
  if( env )
62
0
    return std::chrono::microseconds( int( atof( env ) * 1000 ) );
63
253
  return std::chrono::microseconds( 1ms );
64
253
}();
65
66
// block last waiting thread after this time
67
253
const static auto VVDEC_BUSY_WAIT_TIME_MAX = [] {
68
253
  const char* env = getenv( "VVDEC_BUSY_WAIT_TIME_MAX" );
69
253
  if( env )
70
0
    return atoi( env ) * 1ms;
71
253
  return 5ms;
72
253
}();
73
74
75
struct ThreadPool::TaskException : public std::exception
76
{
77
  explicit TaskException( std::exception_ptr e, ThreadPool::Slot& task )
78
10.5k
    : m_originalException( e )
79
10.5k
    , m_task( task )
80
10.5k
  {}
81
  std::exception_ptr m_originalException;
82
  ThreadPool::Slot&  m_task;
83
};
84
85
class ScopeIncDecCounter
86
{
87
  std::atomic_uint& m_cntr;
88
89
public:
90
27.5k
  explicit ScopeIncDecCounter( std::atomic_uint& counter ): m_cntr( counter ) { m_cntr.fetch_add( 1, std::memory_order_relaxed ); }
91
27.5k
  ~ScopeIncDecCounter()                                                       { m_cntr.fetch_sub( 1, std::memory_order_relaxed ); }
92
  CLASS_COPY_MOVE_DELETE( ScopeIncDecCounter )
93
};
94
95
// ---------------------------------------------------------------------------
96
// Thread Pool
97
// ---------------------------------------------------------------------------
98
99
ThreadPool::ThreadPool( int numThreads, const char* threadPoolName )
100
935
  : m_poolName( threadPoolName )
101
935
  , m_threads( numThreads < 0 ? std::thread::hardware_concurrency() : numThreads )
102
935
  , m_poolPause( m_threads.size() )
103
935
{
104
935
  int tid = 0;
105
935
  for( auto& t: m_threads )
106
29.9k
  {
107
29.9k
    t = std::thread( &ThreadPool::threadProc, this, tid++ );
108
29.9k
  }
109
935
}
110
111
ThreadPool::~ThreadPool()
112
935
{
113
935
  m_exitThreads = true;
114
115
935
  waitForThreads();
116
935
}
117
118
bool ThreadPool::processTasksOnMainThread()
119
0
{
120
0
  CHECK_FATAL( m_threads.size() != 0, "should not be used with multiple threads" );
121
122
0
  TaskIterator taskIt;
123
0
  while( true )
124
0
  {
125
0
    try
126
0
    {
127
0
      if( !taskIt.isValid() )
128
0
      {
129
0
        taskIt = findNextTask( 0, m_tasks.begin() );
130
0
      }
131
0
      else
132
0
      {
133
0
        taskIt = findNextTask( 0, taskIt );
134
0
      }
135
136
0
      if( !taskIt.isValid() )
137
0
      {
138
0
        break;
139
0
      }
140
0
      processTask( 0, *taskIt );
141
0
    }
142
0
    catch( TaskException& e )
143
0
    {
144
0
      handleTaskException( e.m_originalException, e.m_task.done, e.m_task.counter, &e.m_task.state );
145
0
    }
146
0
  }
147
148
  // return true if all done (-> false if some tasks blocked due to barriers)
149
0
  return std::all_of( m_tasks.begin(), m_tasks.end(), []( Slot& t ) { return t.state == FREE; } );
150
0
}
151
152
void ThreadPool::shutdown( bool block )
153
935
{
154
935
  m_exitThreads = true;
155
935
  if( block )
156
935
  {
157
935
    waitForThreads();
158
935
  }
159
935
}
160
161
void ThreadPool::waitForThreads()
162
1.87k
{
163
1.87k
  m_poolPause.unpauseIfPaused( m_poolPause.acquireLock() );
164
165
1.87k
  for( auto& t: m_threads )
166
59.8k
  {
167
59.8k
    if( t.joinable() )
168
29.9k
      t.join();
169
59.8k
  }
170
1.87k
}
171
172
void ThreadPool::checkAndThrowThreadPoolException()
173
10.6k
{
174
10.6k
  if( !m_exceptionFlag.load() )
175
10.6k
  {
176
10.6k
    return;
177
10.6k
  }
178
179
0
  msg( WARNING, "ThreadPool is in exception state." );
180
181
0
  std::exception_ptr tmp = m_threadPoolException;
182
0
  m_threadPoolException  = tmp;
183
0
  m_exceptionFlag.store( false );
184
185
0
  std::rethrow_exception( tmp );
186
0
}
187
188
void ThreadPool::threadProc( int threadId )
189
29.9k
{
190
29.9k
#if __linux
191
29.9k
  if( !m_poolName.empty() )
192
29.9k
  {
193
29.9k
    std::string threadName( m_poolName + std::to_string( threadId ) );
194
29.9k
    pthread_setname_np( pthread_self(), threadName.c_str() );
195
29.9k
  }
196
29.9k
#endif
197
198
29.9k
  auto nextTaskIt = m_tasks.begin();
199
44.5k
  while( !m_exitThreads )
200
44.4k
  {
201
44.4k
    try
202
44.4k
    {
203
44.4k
      auto taskIt = findNextTask( threadId, nextTaskIt );
204
44.4k
      if( !taskIt.isValid() )   // immediately try again without any delay
205
38.2k
      {
206
38.2k
        taskIt = findNextTask( threadId, nextTaskIt );
207
38.2k
      }
208
44.4k
      if( !taskIt.isValid() )   // still nothing found, go into idle loop searching for more tasks
209
37.6k
      {
210
37.6k
        ITT_TASKSTART( itt_domain_thrd, itt_handle_TPspinWait );
211
212
37.6k
        std::unique_lock<std::mutex> idleLock( m_idleMutex, std::defer_lock );
213
214
37.6k
        const auto startWait = std::chrono::steady_clock::now();
215
37.6k
        bool       didBlock  = false;   // if the previous iteration did block we don't want to yield in this iteration
216
867k
        while( !m_exitThreads )
217
840k
        {
218
840k
          if( !didBlock )
219
836k
          {
220
836k
            std::this_thread::yield();
221
836k
          }
222
840k
          didBlock = false;
223
224
840k
          taskIt = findNextTask( threadId, nextTaskIt );
225
840k
          if( taskIt.isValid() || m_exitThreads.load( std::memory_order_relaxed ) )
226
10.5k
          {
227
10.5k
            break;
228
10.5k
          }
229
230
830k
          if( !idleLock.owns_lock() )
231
238k
          {
232
238k
            if( VVDEC_BUSY_WAIT_TIME_MIN.count() == 0 || std::chrono::steady_clock::now() - startWait > VVDEC_BUSY_WAIT_TIME_MIN )
233
27.5k
            {
234
27.5k
              ITT_TASKSTART( itt_domain_thrd, itt_handle_TPblocked );
235
27.5k
              ScopeIncDecCounter cntr( m_poolPause.m_waitingForLockThreads );
236
27.5k
              idleLock.lock();
237
27.5k
              didBlock = true;
238
27.5k
              ITT_TASKEND( itt_domain_thrd, itt_handle_TPblocked );
239
27.5k
            }
240
238k
          }
241
591k
          else if( std::chrono::steady_clock::now() - startWait > VVDEC_BUSY_WAIT_TIME_MAX )
242
272k
          {
243
#if THREAD_POOL_TASK_NAMES
244
            printWaitingTasks();
245
#endif
246
272k
            didBlock = m_poolPause.pauseIfAllOtherThreadsWaiting(
247
272k
              [&]
248
272k
              {
249
31
                taskIt = findNextTask( threadId, nextTaskIt );
250
31
                return taskIt.isValid() || m_exitThreads;
251
31
              } );
252
272k
            if( taskIt.isValid() )
253
0
            {
254
0
              break;
255
0
            }
256
272k
          }
257
830k
        }
258
259
37.6k
        ITT_TASKEND( itt_domain_thrd, itt_handle_TPspinWait );
260
37.6k
      }
261
44.4k
      if( m_exitThreads )
262
29.7k
      {
263
29.7k
        return;
264
29.7k
      }
265
266
14.7k
      processTask( threadId, *taskIt );
267
268
14.7k
      nextTaskIt = taskIt;
269
14.7k
      nextTaskIt.incWrap();
270
14.7k
    }
271
44.4k
    catch( TaskException& e )
272
44.4k
    {
273
10.5k
      handleTaskException( e.m_originalException, e.m_task.done, e.m_task.counter, &e.m_task.state );
274
10.5k
    }
275
44.4k
    catch( std::exception& e )
276
44.4k
    {
277
0
      msg( ERROR, "ERROR: Caught unexpected exception from within the thread pool: %s", e.what() );
278
279
0
      if( m_exceptionFlag.exchange( true ) )
280
0
      {
281
0
        msg( ERROR, "ERROR: Another exception has already happend in the thread pool, but we can only store one." );
282
0
        return;
283
0
      }
284
0
      m_threadPoolException = std::current_exception();
285
0
      return;
286
0
    }
287
44.4k
  }
288
29.9k
}
289
290
bool ThreadPool::checkTaskReady( int threadId, CBarrierVec& barriers, ThreadPool::TaskFunc readyCheck, void* taskParam )
291
7.47M
{
292
7.47M
  if( !barriers.empty() )
293
6.82M
  {
294
    // don't break early, because isBlocked() also checks exception state
295
6.82M
    if( std::count_if( barriers.cbegin(), barriers.cend(), []( const Barrier* b ) { return b && b->isBlocked(); } ) )
296
6.81M
    {
297
6.81M
      return false;
298
6.81M
    }
299
10.6k
    barriers.clear();
300
10.6k
  }
301
302
663k
  if( readyCheck && readyCheck( threadId, taskParam ) == false )
303
651k
  {
304
651k
    return false;
305
651k
  }
306
307
12.2k
  return true;
308
663k
}
309
310
ThreadPool::TaskIterator ThreadPool::findNextTask( int threadId, TaskIterator startSearch )
311
923k
{
312
923k
  if( !startSearch.isValid() )
313
0
  {
314
0
    startSearch = m_tasks.begin();
315
0
  }
316
923k
  bool first = true;
317
96.7M
  for( auto it = startSearch; it != startSearch || first; it.incWrap() )
318
95.8M
  {
319
95.8M
    first = false;
320
95.8M
    try
321
95.8M
    {
322
95.8M
      Slot& task     = *it;
323
95.8M
      auto  expected = WAITING;
324
95.8M
      if( task.state == expected && task.state.compare_exchange_strong( expected, RUNNING ) )
325
7.47M
      {
326
7.47M
        if( checkTaskReady( threadId, task.barriers, task.readyCheck, task.param ) )
327
5.15k
        {
328
5.15k
          return it;
329
5.15k
        }
330
331
        // reschedule
332
7.47M
        task.state = WAITING;
333
7.47M
      }
334
95.8M
    }
335
95.8M
    catch( ... )
336
95.8M
    {
337
9.51k
      throw TaskException( std::current_exception(), *it );
338
9.51k
    }
339
95.8M
  }
340
904k
  return {};
341
923k
}
342
343
bool ThreadPool::processTask( int threadId, ThreadPool::Slot& task )
344
5.15k
{
345
5.15k
  try
346
5.15k
  {
347
5.15k
    const bool success = task.func( threadId, task.param );
348
5.15k
    if( !success )
349
4.04k
    {
350
4.04k
      task.state = WAITING;
351
4.04k
      return false;
352
4.04k
    }
353
354
1.10k
    if( task.done != nullptr )
355
6
    {
356
6
      task.done->unlock();
357
6
    }
358
1.10k
    if( task.counter != nullptr )
359
24
    {
360
24
      task.counter->decrement_nothrow();
361
24
    }
362
1.10k
  }
363
5.15k
  catch( ... )
364
5.15k
  {
365
1.08k
    throw TaskException( std::current_exception(), task );
366
1.08k
  }
367
368
24
  task.state = FREE;
369
370
24
  return true;
371
5.15k
}
372
373
bool ThreadPool::bypassTaskQueue( TaskFunc func, void* param, WaitCounter* counter, Barrier* done, CBarrierVec& barriers, TaskFunc readyCheck )
374
0
{
375
0
  CHECKD( numThreads() > 0, "the task queue should only be bypassed, when running single-threaded." );
376
0
  try
377
0
  {
378
    // if singlethreaded, execute all pending tasks
379
0
    bool waiting_tasks = m_nextFillSlot != m_tasks.begin();
380
0
    bool is_ready      = checkTaskReady( 0, barriers, (TaskFunc)readyCheck, param );
381
0
    if( !is_ready && waiting_tasks )
382
0
    {
383
0
      waiting_tasks = processTasksOnMainThread();
384
0
      is_ready      = checkTaskReady( 0, barriers, (TaskFunc)readyCheck, param );
385
0
    }
386
387
    // when no barriers block this task, execute it directly
388
0
    if( is_ready )
389
0
    {
390
0
      if( func( 0, param ) )
391
0
      {
392
0
        if( done != nullptr )
393
0
        {
394
0
          done->unlock();
395
0
        }
396
397
0
        if( waiting_tasks )
398
0
        {
399
0
          processTasksOnMainThread();
400
0
        }
401
0
        return true;
402
0
      }
403
0
    }
404
0
  }
405
0
  catch( ... )
406
0
  {
407
0
    handleTaskException( std::current_exception(), done, counter, nullptr );
408
0
  }
409
410
  // direct execution of the task failed
411
0
  return false;
412
0
}
413
414
void ThreadPool::handleTaskException( const std::exception_ptr e, Barrier* done, WaitCounter* counter, std::atomic<TaskState>* slot_state )
415
10.5k
{
416
10.5k
  if( done != nullptr )
417
1.39k
  {
418
1.39k
    done->setException( e );
419
1.39k
  }
420
10.5k
  if( counter != nullptr )
421
10.5k
  {
422
10.5k
    counter->setException( e );
423
10.5k
    counter->decrement_nothrow();
424
10.5k
  }
425
426
10.5k
  if( slot_state != nullptr )
427
10.5k
  {
428
10.5k
    *slot_state = FREE;
429
10.5k
  }
430
10.5k
}
431
432
#if THREAD_POOL_TASK_NAMES
433
void ThreadPool::printWaitingTasks()
434
{
435
  std::cerr << "Waiting tasks:" << std::endl;
436
  int count = 0;
437
  for( auto& t: m_tasks )
438
  {
439
    if( t.state == WAITING )
440
    {
441
      ++count;
442
      std::cerr << t.taskName << std::endl;
443
    }
444
  }
445
  std::cerr << std::endl << count << " total tasks waiting" << std::endl;
446
}
447
#endif  // THREAD_POOL_TASK_NAMES
448
449
// ---------------------------------------------------------------------------
450
// Chunked Task Queue
451
// ---------------------------------------------------------------------------
452
453
ThreadPool::ChunkedTaskQueue::~ChunkedTaskQueue()
454
935
{
455
935
  Chunk* next = m_firstChunk.m_next;
456
935
  while( next )
457
0
  {
458
0
    Chunk* curr = next;
459
0
    next = curr->m_next;
460
0
    delete curr;
461
0
  }
462
935
}
463
464
ThreadPool::ChunkedTaskQueue::Iterator ThreadPool::ChunkedTaskQueue::grow()
465
0
{
466
0
  std::lock_guard<std::mutex> l( m_resizeMutex );   // prevent concurrent growth of the queue. Read access while growing is no problem
467
468
0
  m_lastChunk->m_next = new Chunk( &m_firstChunk );
469
0
  m_lastChunk         = m_lastChunk->m_next;
470
471
0
  return Iterator{ &m_lastChunk->m_slots.front(), m_lastChunk };
472
0
}
473
474
ThreadPool::ChunkedTaskQueue::Iterator& ThreadPool::ChunkedTaskQueue::Iterator::operator++()
475
0
{
476
0
  CHECKD( m_slot == nullptr, "incrementing invalid iterator" );
477
0
  CHECKD( m_chunk == nullptr, "incrementing invalid iterator" );
478
479
0
  if( m_slot != &m_chunk->m_slots.back() )
480
0
  {
481
0
    ++m_slot;
482
0
  }
483
0
  else
484
0
  {
485
0
    m_chunk = m_chunk->m_next;
486
0
    if( m_chunk )
487
0
    {
488
0
      m_slot = &m_chunk->m_slots.front();
489
0
    }
490
0
    else
491
0
    {
492
0
      m_slot = nullptr;
493
0
    }
494
0
  }
495
0
  return *this;
496
0
}
497
498
ThreadPool::ChunkedTaskQueue::Iterator& ThreadPool::ChunkedTaskQueue::Iterator::incWrap()
499
95.7M
{
500
95.7M
  CHECKD( m_slot == nullptr, "incrementing invalid iterator" );
501
95.7M
  CHECKD( m_chunk == nullptr, "incrementing invalid iterator" );
502
503
95.7M
  if( m_slot != &m_chunk->m_slots.back() )
504
95.0M
  {
505
95.0M
    ++m_slot;
506
95.0M
  }
507
649k
  else
508
649k
  {
509
649k
    if( m_chunk->m_next )
510
0
    {
511
0
      m_chunk = m_chunk->m_next;
512
0
    }
513
649k
    else
514
649k
    {
515
649k
      m_chunk = &m_chunk->m_firstChunk;
516
649k
    }
517
649k
    m_slot = &m_chunk->m_slots.front();
518
649k
  }
519
95.7M
  return *this;
520
95.7M
}
521
522
void ThreadPool::PoolPause::unpauseIfPaused( std::unique_lock<std::mutex> lockOwnership )
523
13.4k
{
524
13.4k
  CHECKD( lockOwnership.mutex() != &m_allThreadsWaitingMutex, "wrong mutex passed into ThreadPool::PoolPause::unpauseIfPaused()" );
525
13.4k
  CHECKD( !lockOwnership.owns_lock(), "lock passed into ThreadPool::PoolPause::unpauseIfPaused() does not own lock" );
526
  // All threads may be sleeping. If so, wake up.
527
13.4k
  m_allThreadsWaiting = false;
528
13.4k
  m_allThreadsWaitingCV.notify_all();
529
13.4k
}
530
531
template<typename Predicate>
532
bool ThreadPool::PoolPause::pauseIfAllOtherThreadsWaiting( Predicate predicate )
533
272k
{
534
272k
  if( m_nrThreads == 0 )
535
0
  {
536
0
    return false;
537
0
  }
538
272k
  const auto nrWaiting = m_waitingForLockThreads.load( std::memory_order_relaxed );
539
272k
  if( nrWaiting < m_nrThreads - 1 )
540
272k
  {
541
272k
    return false;
542
272k
  }
543
544
  // All threads are waiting. This (current) threads is the one which locked `idleLock`. All
545
  // other threads are waiting in the above condition for `idleLock.lock();`.
546
  // The only way how more work for the threads can come in is if addBarrierTask is called
547
  // or if the thread pool is closed or destroyed.
548
31
  std::unique_lock<std::mutex> lock( m_allThreadsWaitingMutex );
549
31
  m_allThreadsWaiting = true;
550
62
  m_allThreadsWaitingCV.wait( lock, [this, &predicate] { return !m_allThreadsWaiting || predicate(); } );
551
31
  return true;
552
272k
}
553
554
}   // namespace vvdec