/ src / util / task_runner.h
task_runner.h
 1  // Copyright (c) 2024-present The Bitcoin Core developers
 2  // Distributed under the MIT software license, see the accompanying
 3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
 4  
 5  #ifndef BITCOIN_UTIL_TASK_RUNNER_H
 6  #define BITCOIN_UTIL_TASK_RUNNER_H
 7  
 8  #include <cstddef>
 9  #include <functional>
10  
11  namespace util {
12  
13  /** @file
14   * This header provides an interface and simple implementation for a task
15   * runner. Another threaded, serial implementation using a queue is available in
16   * the scheduler module's SerialTaskRunner.
17   */
18  
19  class TaskRunnerInterface
20  {
21  public:
22      virtual ~TaskRunnerInterface() = default;
23  
24      /**
25       * The callback can either be queued for later/asynchronous/threaded
26       * processing, or be executed immediately for synchronous processing.
27       */
28  
29      virtual void insert(std::function<void()> func) = 0;
30  
31      /**
32       * Forces the processing of all pending events.
33       */
34      virtual void flush() = 0;
35  
36      /**
37       * Returns the number of currently pending events.
38       */
39      virtual size_t size() = 0;
40  };
41  
42  class ImmediateTaskRunner : public TaskRunnerInterface
43  {
44  public:
45      void insert(std::function<void()> func) override { func(); }
46      void flush() override {}
47      size_t size() override { return 0; }
48  };
49  
50  } // namespace util
51  
52  #endif // BITCOIN_UTIL_TASK_RUNNER_H