LCOV - code coverage report
Current view: top level - src - scheduler.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 91.2 % 113 103
Test Date: 2024-08-28 04:44:32 Functions: 76.5 % 17 13
Branches: 55.7 % 106 59

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2015-2022 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                 :             : #include <scheduler.h>
       6                 :             : 
       7                 :             : #include <sync.h>
       8                 :             : #include <util/time.h>
       9                 :             : 
      10                 :             : #include <cassert>
      11                 :             : #include <functional>
      12                 :             : #include <utility>
      13                 :             : 
      14                 :         169 : CScheduler::CScheduler() = default;
      15                 :             : 
      16                 :         169 : CScheduler::~CScheduler()
      17                 :             : {
      18         [ -  + ]:         169 :     assert(nThreadsServicingQueue == 0);
      19   [ +  +  -  + ]:         169 :     if (stopWhenEmpty) assert(taskQueue.empty());
      20                 :         169 : }
      21                 :             : 
      22                 :             : 
      23                 :         182 : void CScheduler::serviceQueue()
      24                 :             : {
      25                 :         182 :     WAIT_LOCK(newTaskMutex, lock);
      26                 :         182 :     ++nThreadsServicingQueue;
      27                 :             : 
      28                 :             :     // newTaskMutex is locked throughout this loop EXCEPT
      29                 :             :     // when the thread is waiting or when the user's function
      30                 :             :     // is called.
      31         [ +  + ]:       52558 :     while (!shouldStop()) {
      32                 :             :         try {
      33   [ +  +  +  + ]:      123101 :             while (!shouldStop() && taskQueue.empty()) {
      34                 :             :                 // Wait until there is something to do.
      35         [ +  - ]:        9251 :                 newTaskScheduled.wait(lock);
      36                 :             :             }
      37                 :             : 
      38                 :             :             // Wait until either there is a new task, or until
      39                 :             :             // the time of the first item on the queue:
      40                 :             : 
      41   [ +  +  +  - ]:      104599 :             while (!shouldStop() && !taskQueue.empty()) {
      42         [ +  - ]:       52223 :                 std::chrono::steady_clock::time_point timeToWaitFor = taskQueue.begin()->first;
      43   [ -  +  +  - ]:       52223 :                 if (newTaskScheduled.wait_until(lock, timeToWaitFor) == std::cv_status::timeout) {
      44                 :             :                     break; // Exit loop after timeout, it means we reached the time of the event
      45                 :             :                 }
      46                 :             :             }
      47                 :             : 
      48                 :             :             // If there are multiple threads, the queue can empty while we're waiting (another
      49                 :             :             // thread may service the task we were waiting on).
      50   [ +  +  +  - ]:      104582 :             if (shouldStop() || taskQueue.empty())
      51                 :         170 :                 continue;
      52                 :             : 
      53         [ +  - ]:       52206 :             Function f = taskQueue.begin()->second;
      54                 :       52206 :             taskQueue.erase(taskQueue.begin());
      55                 :             : 
      56                 :       52206 :             {
      57                 :             :                 // Unlock before calling f, so it can reschedule itself or another task
      58                 :             :                 // without deadlocking:
      59         [ +  - ]:       52206 :                 REVERSE_LOCK(lock);
      60         [ +  - ]:       52206 :                 f();
      61                 :       52206 :             }
      62                 :       52206 :         } catch (...) {
      63                 :           0 :             --nThreadsServicingQueue;
      64                 :           0 :             throw;
      65                 :           0 :         }
      66                 :             :     }
      67                 :         182 :     --nThreadsServicingQueue;
      68         [ +  - ]:         182 :     newTaskScheduled.notify_one();
      69                 :         182 : }
      70                 :             : 
      71                 :       54167 : void CScheduler::schedule(CScheduler::Function f, std::chrono::steady_clock::time_point t)
      72                 :             : {
      73                 :       54167 :     {
      74                 :       54167 :         LOCK(newTaskMutex);
      75   [ +  -  +  -  :      108334 :         taskQueue.insert(std::make_pair(t, f));
                   +  - ]
      76                 :       54167 :     }
      77                 :       54167 :     newTaskScheduled.notify_one();
      78                 :       54167 : }
      79                 :             : 
      80                 :           1 : void CScheduler::MockForward(std::chrono::seconds delta_seconds)
      81                 :             : {
      82   [ +  -  -  + ]:           1 :     assert(delta_seconds > 0s && delta_seconds <= 1h);
      83                 :             : 
      84                 :           1 :     {
      85                 :           1 :         LOCK(newTaskMutex);
      86                 :             : 
      87                 :             :         // use temp_queue to maintain updated schedule
      88                 :           1 :         std::multimap<std::chrono::steady_clock::time_point, Function> temp_queue;
      89                 :             : 
      90         [ +  + ]:           4 :         for (const auto& element : taskQueue) {
      91         [ +  - ]:           3 :             temp_queue.emplace_hint(temp_queue.cend(), element.first - delta_seconds, element.second);
      92                 :             :         }
      93                 :             : 
      94                 :             :         // point taskQueue to temp_queue
      95                 :           1 :         taskQueue = std::move(temp_queue);
      96         [ +  - ]:           1 :     }
      97                 :             : 
      98                 :             :     // notify that the taskQueue needs to be processed
      99                 :           1 :     newTaskScheduled.notify_one();
     100                 :           1 : }
     101                 :             : 
     102                 :           0 : static void Repeat(CScheduler& s, CScheduler::Function f, std::chrono::milliseconds delta)
     103                 :             : {
     104                 :           0 :     f();
     105   [ #  #  #  #  :           0 :     s.scheduleFromNow([=, &s] { Repeat(s, f, delta); }, delta);
             #  #  #  # ]
     106                 :           0 : }
     107                 :             : 
     108                 :           0 : void CScheduler::scheduleEvery(CScheduler::Function f, std::chrono::milliseconds delta)
     109                 :             : {
     110   [ #  #  #  #  :           0 :     scheduleFromNow([this, f, delta] { Repeat(*this, f, delta); }, delta);
             #  #  #  # ]
     111                 :           0 : }
     112                 :             : 
     113                 :           4 : size_t CScheduler::getQueueInfo(std::chrono::steady_clock::time_point& first,
     114                 :             :                                 std::chrono::steady_clock::time_point& last) const
     115                 :             : {
     116                 :           4 :     LOCK(newTaskMutex);
     117         [ +  + ]:           4 :     size_t result = taskQueue.size();
     118         [ +  + ]:           4 :     if (!taskQueue.empty()) {
     119                 :           3 :         first = taskQueue.begin()->first;
     120                 :           3 :         last = taskQueue.rbegin()->first;
     121                 :             :     }
     122         [ +  - ]:           4 :     return result;
     123                 :           4 : }
     124                 :             : 
     125                 :         166 : bool CScheduler::AreThreadsServicingQueue() const
     126                 :             : {
     127                 :         166 :     LOCK(newTaskMutex);
     128         [ +  - ]:         166 :     return nThreadsServicingQueue;
     129                 :         166 : }
     130                 :             : 
     131                 :             : 
     132                 :       94820 : void SerialTaskRunner::MaybeScheduleProcessQueue()
     133                 :             : {
     134                 :       94820 :     {
     135                 :       94820 :         LOCK(m_callbacks_mutex);
     136                 :             :         // Try to avoid scheduling too many copies here, but if we
     137                 :             :         // accidentally have two ProcessQueue's scheduled at once its
     138                 :             :         // not a big deal.
     139         [ +  + ]:       94820 :         if (m_are_callbacks_running) return;
     140         [ +  + ]:       62894 :         if (m_callbacks_pending.empty()) return;
     141                 :       41057 :     }
     142         [ +  - ]:      159329 :     m_scheduler.schedule([this] { this->ProcessQueue(); }, std::chrono::steady_clock::now());
     143                 :             : }
     144                 :             : 
     145                 :       52100 : void SerialTaskRunner::ProcessQueue()
     146                 :             : {
     147         [ +  - ]:       52100 :     std::function<void()> callback;
     148                 :       52100 :     {
     149         [ +  - ]:       52100 :         LOCK(m_callbacks_mutex);
     150         [ +  + ]:       52100 :         if (m_are_callbacks_running) return;
     151         [ +  + ]:       52099 :         if (m_callbacks_pending.empty()) return;
     152                 :       47410 :         m_are_callbacks_running = true;
     153                 :             : 
     154                 :       47410 :         callback = std::move(m_callbacks_pending.front());
     155         [ +  - ]:       47410 :         m_callbacks_pending.pop_front();
     156                 :        4690 :     }
     157                 :             : 
     158                 :             :     // RAII the setting of fCallbacksRunning and calling MaybeScheduleProcessQueue
     159                 :             :     // to ensure both happen safely even if callback() throws.
     160                 :       47410 :     struct RAIICallbacksRunning {
     161                 :             :         SerialTaskRunner* instance;
     162                 :       47410 :         explicit RAIICallbacksRunning(SerialTaskRunner* _instance) : instance(_instance) {}
     163                 :       47410 :         ~RAIICallbacksRunning()
     164                 :             :         {
     165                 :       47410 :             {
     166                 :       47410 :                 LOCK(instance->m_callbacks_mutex);
     167         [ +  - ]:       47410 :                 instance->m_are_callbacks_running = false;
     168                 :       47410 :             }
     169                 :       47410 :             instance->MaybeScheduleProcessQueue();
     170                 :       47410 :         }
     171                 :       47410 :     } raiicallbacksrunning(this);
     172                 :             : 
     173         [ +  - ]:       47410 :     callback();
     174                 :       52100 : }
     175                 :             : 
     176                 :       47410 : void SerialTaskRunner::insert(std::function<void()> func)
     177                 :             : {
     178                 :       47410 :     {
     179                 :       47410 :         LOCK(m_callbacks_mutex);
     180         [ +  - ]:       47410 :         m_callbacks_pending.emplace_back(std::move(func));
     181                 :       47410 :     }
     182                 :       47410 :     MaybeScheduleProcessQueue();
     183                 :       47410 : }
     184                 :             : 
     185                 :         166 : void SerialTaskRunner::flush()
     186                 :             : {
     187         [ +  - ]:         166 :     assert(!m_scheduler.AreThreadsServicingQueue());
     188                 :             :     bool should_continue = true;
     189         [ +  + ]:         463 :     while (should_continue) {
     190                 :         297 :         ProcessQueue();
     191                 :         297 :         LOCK(m_callbacks_mutex);
     192         [ +  - ]:         297 :         should_continue = !m_callbacks_pending.empty();
     193                 :         297 :     }
     194                 :         166 : }
     195                 :             : 
     196                 :       18766 : size_t SerialTaskRunner::size()
     197                 :             : {
     198                 :       18766 :     LOCK(m_callbacks_mutex);
     199         [ +  - ]:       18766 :     return m_callbacks_pending.size();
     200                 :             : }
        

Generated by: LCOV version 2.0-1