File: threadpool.hpp

package info (click to toggle)
spades 3.15.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 96,896 kB
  • sloc: cpp: 850,748; ansic: 156,813; python: 23,134; perl: 4,547; sh: 2,352; makefile: 1,273; java: 890; pascal: 875; xml: 19
file content (580 lines) | stat: -rw-r--r-- 16,061 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#pragma once

#include <algorithm>
#include <condition_variable>
#include <functional>
#include <future>
#include <mutex>
#include <queue>
#include <random>
#include <thread>
#include <type_traits>
#include <vector>

#include "hooks.hpp"

#define CALL_HOOK_WORKER(HOOK)                                                                     \
  do                                                                                               \
  {                                                                                                \
    if (pool->hooks)                                                                               \
    {                                                                                              \
      pool->hooks->HOOK();                                                                         \
    }                                                                                              \
  } while (0)

#define CALL_HOOK_POOL(HOOK)                                                                       \
  do                                                                                               \
  {                                                                                                \
    if (hooks)                                                                                     \
    {                                                                                              \
      hooks->HOOK();                                                                               \
    }                                                                                              \
  } while (0)

namespace ThreadPool
{
/*! \brief ThreadPool implement a multiple queues/multiple workers threadpool.
 *
 *  When created, the pool will start the workers(threads) immediatly. The
 *  threads will only terminate when the pool is destroyed.
 *
 *  This class implements a one queue per worker strategy to dispatch work.
 */
class ThreadPool
{
public:
  /*! \brief Constructs a ThreadPool.
   *
   *  The number of workers will be deduced from hardware.
   */
  explicit ThreadPool();

  /*! \brief Constructs a ThreadPool.
   *  \param pool_size Number of threads to start.
   */
  explicit ThreadPool(std::size_t pool_size);

  /*! \brief Constructs a ThreadPool.
   *  \param hooks Hooks to register in the pool.
   */
  explicit ThreadPool(std::shared_ptr<Hooks> hooks);

  /*! \brief Constructs a ThreadPool.
   *  \param pool_size Number of threads to start.
   *  \param hooks Hooks to register in the pool.
   */
  ThreadPool(std::size_t pool_size, std::shared_ptr<Hooks> hooks);

  /*! \brief Stops the pool and clean all workers.
   */
  ~ThreadPool();

  ThreadPool(ThreadPool &&);
  
  /*! \brief Run a task in the SingleQueue.
   *  \returns Returns a future containing the result of the task.
   *
   *  When a task is ran in the SingleQueue, the callable object will be
   * packaged in a packaged_task and put in the inner task_queue. A waiting
   * worker will pick the task and execute it. If no workers are available, the
   * task will remain in the queue until a worker picks it up.
   */
  template <typename Function, typename... Args>
  auto run(Function&& f, Args&&... args)
    -> std::future<typename std::result_of<Function(Args...)>::type>;

  /*! \brief Stop the ThreadPool.
   *
   * A stopped ThreadPool will discard any task dispatched to it. All workers
   * will discard new tasks, but the threads will not exit.
   */
  void stop();

  /* I don't like this implementation for hooks with a shared pointer. I don't
   * know why but it makes me feel uncomfortable.
   *
   * Our options are:
   * shared_ptr: easy solution. But do we really need shared ownership ? I don't
   * think it's necessary for such a simple interface.
   * unique_ptr: user probably wants to keep ownership of the hooks if it uses
   * them to store data. It would require a way to give back ownership to user
   * (ie give/take ala rust).
   * weak_ptr: requires the user to make a shared_ptr. Would clear the weak_ptr
   * when the shared_ptr is destroyed (which does not happen with raw pointer)
   */

  /*! \brief Register a ThreadPool::Hooks class.
   *  \param hooks The class to be registered
   */
  void register_hooks(std::shared_ptr<Hooks> hooks);

  /*! \brief Check the state of the threadpool
   *  \returns True if the ThreadPool is stopped, false otherwise.
   */
  bool is_stopped() const noexcept;

  /*! \brief Check on the number of threads not currently working.
   *  \returns The number of threads currently waiting for a task.
   *
   * The number might be imprecise, as between the time the value is read and
   * returned, a thread might become unavailable.
   */
  std::size_t threads_available() const noexcept;

  /*! \brief Check on the number of threads currently working.
   *  \returns The number of threads currently working.
   *
   * The number might be imprecise, as between the time the value is read and
   * returned, a thread might finish a task and become available.
   */
  std::size_t threads_working() const noexcept;

private:
  using task_type = std::packaged_task<void()>;

  /*! \brief Starts the pool when the pool is constructed.
   *
   *  It will starts _pool_size threads.
   */
  void start_pool();

  /*! \brief Clean the pool and join threads of dead workers.
   *
   *  This method may be called at any time by any thread putting a job in the
   *  queue. This function acquires a lock on the pool vector.
   */
  void clean();

  /*! \brief Joins all threads in the pool.
   *
   *  Should only be called from destructor. This method will stop all the
   *  worker and join the corresponding thread.
   */
  void terminate();

  /*! \brief Find the worker for which to dispatch the tasks
   *  \return The index in the worker array to which a task should be
   * dispatch.
   */
  std::size_t get_dispatch_worker();

  /*! \brief Dispatch a task to a given worker
   *  \param idx Index of the worker to dispatch the work at
   *  \param task Task to dispatch into the worker
   */
  template <typename TaskType>
  void dispatch_work(const std::size_t idx, TaskType task);

  /*! \brief Inner worker class. Capture the ThreadPool when built.
   *
   *  The only job of this class is to run tasks. It will use the captured
   *  ThreadPool to interact with it.
   */
  struct Worker
  {
  public:
    /*! \brief Construct a worker.
     *  \param pool The ThreadPool the worker works for.
     *  \param idx
     */
    explicit Worker(ThreadPool* pool, std::size_t idx);

    /*! \brief Poll task from the queue.
     *  \param nb_task Number of tasks to run and then exit. If 0 then run until
     *  the ThreadPool stops.
     */
    void operator()();

    /*! \brief Stop the Worker
     */
    void stop();

    /*! \brief Start the Worker
     */
    void start();

    /*! \brief Check the state of the worker
     *  \returns True if the worker is stopped, false otherwise.
     */
    bool is_stopped() const noexcept;

    /*! \brief The task queue.
     *
     *  Access to this task queue should **always** be done while locking using
     *  tasks_lock mutex.
     */
    std::queue<task_type> tasks;

    /*! \brief Mutex regulating acces to tasks.
     */
    mutable std::mutex tasks_lock;

    /*! \brief Condition variable used to wake the worker for when a task is
     *  available.
     */
    std::condition_variable cv_variable;

    std::atomic<std::size_t> queue_size;

  private:
    task_type extract_task(std::queue<task_type>& task_queue);
    std::pair<bool, task_type> work_steal();
    std::pair<bool, task_type> find_task();
    void wait_for_start();

    /*! \brief Captured ThreadPool that the worker works for.
     */
    ThreadPool* pool;

    std::atomic<bool> stopped;

    std::atomic<bool> started;

    const std::size_t idx;
  };

  /*! \brief Check if the pool can spawn more workers, and spawn one for a
   *  single task
   *
   *  It will check the current number of spawned threads and if it can spawn
   *  or not a new thread. If a thread can be spawned, one is created.
   */
  void check_spawn_single_worker();

  /*! \brief Vector of thread, the actual thread pool.
   *
   *  Emplacing in this vector construct and launch a thread.
   */
  std::vector<std::pair<std::thread, std::unique_ptr<Worker>>> pool;

  /*! \brief Mutex regulating acces to the pool
   */
  mutable std::mutex pool_lock;

  /*! \brief Number of waiting threads in the pool.
   */
  std::atomic<std::size_t> waiting_threads;

  /*! \brief Number of threads executing a task in the pool.
   */
  std::atomic<std::size_t> working_threads;

  /*! \brief Boolean representing if the pool is stopped.
   *
   */
  std::atomic<bool> stopped;

  /*! \brief Struct containing all hooks the threadpool will call.
   */
  std::shared_ptr<Hooks> hooks;

  /*! \brief Size of the pool.
   */
  std::size_t pool_size;
};

// ThreadPool impl
inline ThreadPool::ThreadPool()
  : ThreadPool(std::thread::hardware_concurrency(), nullptr)
{
}

inline ThreadPool::ThreadPool(std::size_t pool_size)
  : ThreadPool(pool_size, nullptr)
{
}

inline ThreadPool::ThreadPool(std::shared_ptr<Hooks> hooks)
  : ThreadPool(std::thread::hardware_concurrency(), hooks)
{
}

inline ThreadPool::ThreadPool(std::size_t pool_size, std::shared_ptr<Hooks> hooks)
  : waiting_threads(0)
  , working_threads(0)
  , stopped(false)
  , hooks(hooks)
  , pool_size(pool_size)
{
  start_pool();
}

inline ThreadPool::~ThreadPool()
{
  stop();
  terminate();
}

inline ThreadPool::ThreadPool(ThreadPool &&that)
    : waiting_threads(0),
      working_threads(0),
      stopped(false),
      hooks(that.hooks),
      pool_size(that.pool_size) {
  that.stop();
  start_pool();
}

template <typename Function, typename... Args>
auto ThreadPool::run(Function&& f, Args&&... args)
  -> std::future<typename std::result_of<Function(Args...)>::type>
{
  using task_return_type = typename std::result_of<Function(Args...)>::type;

  // Create a packaged task from the callable object to fetch its result
  // with get_future()
  auto task = std::packaged_task<task_return_type()>(
    std::bind(std::forward<Function&&>(f), std::forward<Args&&...>(args)...));
  auto result = task.get_future();

  std::size_t idx = this->get_dispatch_worker();

  if (stopped)
  {
    return result;
  }

  dispatch_work(idx, std::move(task));
  return result;
}

inline std::size_t ThreadPool::get_dispatch_worker()
{
  // For now we dispatch at random. If the random index is on a worker that is
  // stopped but now yet cleaned, return another idx;
  static std::random_device seeder;
  static std::mt19937 engine(seeder());
  std::uniform_int_distribution<int> dist(0, pool.size() - 1);
  return dist(engine);
}

template <typename TaskType>
inline void ThreadPool::dispatch_work(const std::size_t idx, TaskType task)
{
  auto& worker = pool[idx];
  {
    std::lock_guard<decltype(Worker::tasks_lock)> lk(worker.second->tasks_lock);
    worker.second->tasks.emplace(std::move(task));
  }
  worker.second->queue_size--;
  worker.second->cv_variable.notify_one();
}

inline void ThreadPool::stop()
{
  stopped = true;
  std::lock_guard<decltype(pool_lock)> pl(pool_lock);
  for (const auto& w : pool)
  {    
    w.second->stop();
  }
}

inline void ThreadPool::terminate()
{
  std::lock_guard<decltype(pool_lock)> pl(pool_lock);
  // Join everything
  for (auto& w : pool)
  {
    w.first.join();
    CALL_HOOK_POOL(on_worker_die);
  }
}

inline void ThreadPool::start_pool()
{
  std::lock_guard<decltype(pool_lock)> pl(pool_lock);
  for (std::size_t i = 0; i < pool_size; i++)
  {
    auto w = std::unique_ptr<Worker>(new Worker(this, i));
    pool.push_back(
      std::pair<std::thread, std::unique_ptr<Worker>>(std::thread(std::ref(*w)), std::move(w)));
    CALL_HOOK_POOL(on_worker_add);
  }

  for (auto& w : pool)
  {
    w.second->start();
  }
}

inline bool ThreadPool::is_stopped() const noexcept
{
  return stopped;
}

inline std::size_t ThreadPool::threads_available() const noexcept
{
  return waiting_threads.load();
}

inline std::size_t ThreadPool::threads_working() const noexcept
{
  return working_threads.load();
}

inline void ThreadPool::register_hooks(std::shared_ptr<Hooks> hooks)
{
  this->hooks = hooks;
}

// Worker impl
inline ThreadPool::Worker::Worker(ThreadPool* pool, std::size_t idx)
  : queue_size(0)
  , pool(pool)
  , stopped(false)
  , started(false)
  , idx(idx)
{
}

inline void ThreadPool::Worker::operator()()
{
  wait_for_start();
  for (;;)
  {
    // Thread is waiting
    pool->waiting_threads += 1;

    auto task = find_task();
    if (!task.first)
    {
      break;
    }

    CALL_HOOK_WORKER(pre_task_hook);

    pool->waiting_threads -= 1;
    pool->working_threads += 1;

    task.second();

    CALL_HOOK_WORKER(post_task_hook);
    pool->working_threads -= 1;
  }
}

inline void ThreadPool::Worker::wait_for_start()
{
  std::unique_lock<decltype(tasks_lock)> lock(tasks_lock);
  cv_variable.wait(lock, [&] { return this->started == true; });
}

inline std::pair<bool, ThreadPool::task_type> ThreadPool::Worker::find_task()
{
  for (;;)
  {
#ifndef THREADPOOL_DISABLE_WORK_STEALING
    if (tasks.empty())
    {
      // Try work stealing
      auto res = work_steal();
      if (res.first)
      {
        return res;
      }
    }
#endif

    // Wait for a task in our queue
    std::unique_lock<decltype(tasks_lock)> lock(tasks_lock);
    cv_variable.wait(lock, [&] { return pool->stopped || this->stopped || !tasks.empty(); });

    // Pool is stopped
    if (pool->stopped || this->stopped)
    {
      return std::pair<bool, task_type>(false, task_type{});
    }

    return std::pair<bool, task_type>(true, extract_task(tasks));
  }
}

inline std::pair<bool, ThreadPool::task_type> ThreadPool::Worker::work_steal()
{
#ifndef THREADPOOL_DISABLE_NEIGHBORS_WORK_STEALING
  if (pool->stopped || this->stopped)
  {
    return std::pair<bool, task_type>(false, task_type{});
  }

  if (idx > 0)
  {
    auto& worker = pool->pool[idx - 1];
    if (worker.second->queue_size != 0)
    {
      std::unique_lock<decltype(Worker::tasks_lock)> lk(worker.second->tasks_lock,
                                                        std::try_to_lock);
      if (lk.owns_lock() && !worker.second->tasks.empty())
      {
        return std::pair<bool, task_type>(true, extract_task(worker.second->tasks));
      }
    }
  }

  if (idx < (pool->pool.size() - 1))
  {
    auto& worker = pool->pool[idx + 1];
    if (worker.second->queue_size != 0)
    {
      std::unique_lock<decltype(Worker::tasks_lock)> lk(worker.second->tasks_lock,
                                                        std::try_to_lock);
      if (lk.owns_lock() && !worker.second->tasks.empty())
      {
        return std::pair<bool, task_type>(true, extract_task(worker.second->tasks));
      }
    }
  }
#else
  for (auto& w : pool->pool)
  {
    if (pool->stopped || this->stopped)
    {
      return std::pair<bool, task_type>(false, task_type{});
    }

    // FIXME: add try lock
    std::lock_guard<decltype(Worker::tasks_lock)> lk(w.second->tasks_lock);
    if (w.second->tasks.empty())
    {
      continue;
    }

    return std::pair<bool, task_type>(true, extract_task(w.second->tasks));
  }
#endif
  return std::pair<bool, task_type>(false, task_type{});
}

inline ThreadPool::task_type
ThreadPool::Worker::extract_task(std::queue<ThreadPool::task_type>& task_queue)
{
  task_type task = std::move(task_queue.front());
  task_queue.pop();
  queue_size--;
  return task;
}

inline void ThreadPool::Worker::stop()
{
  {
    std::lock_guard<decltype(Worker::tasks_lock)> lock(tasks_lock);
    stopped = true;
  }
  cv_variable.notify_one();
}

inline void ThreadPool::Worker::start()
{
  {
    std::lock_guard<decltype(Worker::tasks_lock)> lock(tasks_lock);
    started = true;
  }
  cv_variable.notify_one();
}

inline bool ThreadPool::Worker::is_stopped() const noexcept
{
  return stopped;
}
} // namespace ThreadPool