File: vtkThreadedCallbackQueue.cxx

package info (click to toggle)
vtk9 9.3.0%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 267,116 kB
  • sloc: cpp: 2,195,914; ansic: 285,452; python: 104,858; sh: 4,061; yacc: 4,035; java: 3,977; xml: 2,771; perl: 2,189; lex: 1,762; objc: 153; makefile: 150; javascript: 90; tcl: 59
file content (322 lines) | stat: -rw-r--r-- 10,809 bytes parent folder | download | duplicates (2)
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

#include "vtkThreadedCallbackQueue.h"
#include "vtkObjectFactory.h"

#include <algorithm>

VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkThreadedCallbackQueue);

//=============================================================================
class vtkThreadedCallbackQueue::ThreadWorker
{
public:
  ThreadWorker(vtkThreadedCallbackQueue* queue, std::shared_ptr<std::atomic_int>& threadIndex)
    : Queue(queue)
    , ThreadIndex(threadIndex)
  {
  }

  ThreadWorker(ThreadWorker&& other) noexcept
    : Queue(other.Queue)
    , ThreadIndex(std::move(other.ThreadIndex))
  {
  }

  void operator()()
  {
    while (this->Pop())
    {
    }
    std::lock_guard<std::mutex> lock(this->Queue->ControlMutex);
    this->Queue->ThreadIdToIndex.erase(std::this_thread::get_id());
  }

private:
  /**
   * Pops an invoker from the queue and runs it if the queue is running and if the thread
   * is in service (meaning its thread id is still higher than Queue->NumberOfThreads).
   * It returns true if the queue has been able to be popped and false otherwise.
   */
  bool Pop()
  {
    std::unique_lock<std::mutex> lock(this->Queue->Mutex);

    if (this->OnHold())
    {
      this->Queue->ConditionVariable.wait(lock, [this] { return !this->OnHold(); });
    }

    // Note that if the queue is empty at this point, it means that either the current thread id
    // is now out of bound, or the queue is being destroyed.
    if (!this->Continue())
    {
      return false;
    }

    auto& invokerQueue = this->Queue->InvokerQueue;

    SharedFutureBasePointer invoker = std::move(invokerQueue.front());
    invokerQueue.pop_front();

    this->Queue->PopFrontNullptr();
    lock.unlock();

    std::unique_lock<std::mutex> stateLock(invoker->Mutex);
    this->Queue->Invoke(std::move(invoker), stateLock);

    return true;
  }

  /**
   * Thread is on hold if its thread id is not out of bounds, while the queue is not calling
   * its destructor, while the queue is running, while the queue is empty.
   */
  bool OnHold() const
  {
    return *this->ThreadIndex < this->Queue->NumberOfThreads && !this->Queue->Destroying &&
      this->Queue->InvokerQueue.empty();
  }

  /**
   * We can continue popping elements if the thread id is not out of bounds while
   * the queue is running and the queue is not empty.
   */
  bool Continue() const
  {
    return *this->ThreadIndex < this->Queue->NumberOfThreads && !this->Queue->InvokerQueue.empty();
  }

  vtkThreadedCallbackQueue* Queue;
  std::shared_ptr<std::atomic_int> ThreadIndex;
};

//-----------------------------------------------------------------------------
vtkThreadedCallbackQueue::vtkThreadedCallbackQueue()
{
  this->SetNumberOfThreads(1);
}

//-----------------------------------------------------------------------------
vtkThreadedCallbackQueue::~vtkThreadedCallbackQueue()
{
  {
    std::lock_guard<std::mutex> destroyLock(this->DestroyMutex);
    {
      std::lock_guard<std::mutex> lock(this->Mutex);
      this->Destroying = true;
    }
  }

  this->ConditionVariable.notify_all();
  this->Sync();
}

//-----------------------------------------------------------------------------
void vtkThreadedCallbackQueue::SetNumberOfThreads(int numberOfThreads)
{
  this->PushControl([this, numberOfThreads]() {
    int size = static_cast<int>(this->Threads.size());

    std::lock_guard<std::mutex> destroyLock(this->DestroyMutex);
    if (this->Destroying)
    {
      return;
    }
    if (size == numberOfThreads)
    {
      // Nothing to do
      return;
    }
    // If we are expanding the number of threads, then we just need to spawn
    // the missing threads.
    else if (size < numberOfThreads)
    {
      this->NumberOfThreads = numberOfThreads;

      std::generate_n(std::back_inserter(this->Threads), numberOfThreads - size, [this] {
        auto threadIndex =
          std::make_shared<std::atomic_int>(static_cast<int>(this->Threads.size()));
        auto thread = std::thread(ThreadWorker(this, threadIndex));
        {
          std::lock_guard<std::mutex> threadIdLock(this->ThreadIdToIndexMutex);
          this->ThreadIdToIndex.emplace(thread.get_id(), threadIndex);
        }
        return thread;
      });
    }
    // If we are shrinking the number of threads, let's notify all threads
    // so the threads whose id is more than the updated NumberOfThreads terminate.
    else
    {
      // If we have a thread index larger than the new number of threads, we swap ourself with
      // thread 0. We now know we will live after this routine and can synchronize terminating
      // threads ourselves.
      {
        std::unique_lock<std::mutex> lock(this->ThreadIdToIndexMutex);
        std::atomic_int& threadIndex = *this->ThreadIdToIndex.at(std::this_thread::get_id());
        if (threadIndex && threadIndex >= numberOfThreads)
        {
          std::atomic_int& thread0Index = *this->ThreadIdToIndex.at(this->Threads[0].get_id());
          lock.unlock();

          std::swap(this->Threads[threadIndex], this->Threads[0]);

          // Swapping the value of atomic ThreadIndex inside ThreadWorker.
          int tmp = thread0Index;
          thread0Index.exchange(threadIndex);
          threadIndex = tmp;
        }
      }

      this->NumberOfThreads = numberOfThreads;
      this->ConditionVariable.notify_all();
      this->Sync(this->NumberOfThreads);

      // Excess threads are done, we can resize
      this->Threads.resize(numberOfThreads);
    }
  });
}

//-----------------------------------------------------------------------------
void vtkThreadedCallbackQueue::Sync(int startId)
{
  std::for_each(this->Threads.begin() + startId, this->Threads.end(),
    [](std::thread& thread) { thread.join(); });
}

//-----------------------------------------------------------------------------
void vtkThreadedCallbackQueue::PopFrontNullptr()
{
  while (!this->InvokerQueue.empty() && !this->InvokerQueue.front())
  {
    this->InvokerQueue.pop_front();
  }
}

//-----------------------------------------------------------------------------
void vtkThreadedCallbackQueue::Invoke(
  vtkSharedFutureBase* invoker, std::unique_lock<std::mutex>& lock)
{
  invoker->Status = RUNNING;
  lock.unlock();
  (*invoker)();
  this->SignalDependentSharedFutures(invoker);
}

//-----------------------------------------------------------------------------
void vtkThreadedCallbackQueue::SignalDependentSharedFutures(vtkSharedFutureBase* invoker)
{
  // We put invokers to launch in a separate container so we can separate the usage of mutexes as
  // much as possible
  std::vector<SharedFutureBasePointer> invokersToLaunch;
  {
    // We are iterating on our dependents, which mean we cannot let any dependent add themselves to
    // this container. At this point we're "ready" anyway so no dependents should be waiting in most
    // cases.
    std::lock_guard<std::mutex> lock(invoker->Mutex);
    for (auto& dependent : invoker->Dependents)
    {
      // We're locking the dependent future. When the lock is released, either the future is not
      // done constructing and we have nothing to do, we can let it run itself, or the future is
      // done constructing, in which case if we hit zero prior futures remaining, we've gotta move
      // its associated invoker in the running queue.
      std::unique_lock<std::mutex> dependentLock(dependent->Mutex);
      --dependent->NumberOfPriorSharedFuturesRemaining;
      if (dependent->Status == ON_HOLD && !dependent->NumberOfPriorSharedFuturesRemaining)
      {
        // Invoker is high priority if it comes from vtkThreadedCallbackQueue::Wait for example.
        if (dependent->IsHighPriority)
        {
          this->Invoke(dependent, dependentLock);
        }
        else
        {
          // We can unlock at this point, we don't touch the future anymore
          dependentLock.unlock();
          invokersToLaunch.emplace_back(std::move(dependent));
        }
      }
    }
  }
  if (!invokersToLaunch.empty())
  {
    std::lock_guard<std::mutex> lock(this->Mutex);
    // We need to handle the invoker index.
    // If the InvokerQueue is empty, then we set it such that after this look, the front has index
    // 0.
    std::size_t index = this->InvokerQueue.empty() ? invokersToLaunch.size()
                                                   : this->InvokerQueue.front()->InvokerIndex;
    for (SharedFutureBasePointer& inv : invokersToLaunch)
    {
      assert(inv->Status == ON_HOLD && "Status should be ON_HOLD");
      inv->InvokerIndex = --index;

      std::lock_guard<std::mutex> stateLock(inv->Mutex);
      inv->Status = ENQUEUED;

      // This dependent has been waiting enough, let's give him some priority.
      // Anyway, the invoker is past due if it was put inside InvokersOnHold.
      this->InvokerQueue.emplace_front(std::move(inv));
    }
  }
  for (std::size_t i = 0; i < invokersToLaunch.size(); ++i)
  {
    this->ConditionVariable.notify_one();
  }
}

//-----------------------------------------------------------------------------
bool vtkThreadedCallbackQueue::TryInvoke(vtkSharedFutureBase* invoker)
{
  std::unique_lock<std::mutex> invokerLock(invoker->Mutex);
  if (![this, &invoker] {
        // We need to check again if we cannot run in case the thread worker just popped this
        // invoker. We are guarded by this->Mutex so there cannot be a conflict here.
        if (invoker->Status != ENQUEUED)
        {
          // Someone picked up the invoker right before us, we can abort.
          return SharedFutureBasePointer(nullptr);
        }

        std::lock_guard<std::mutex> lock(this->Mutex);

        if (this->InvokerQueue.empty())
        {
          return SharedFutureBasePointer(nullptr);
        }

        // There has to be a front if we are here.
        vtkIdType index = invoker->InvokerIndex - this->InvokerQueue.front()->InvokerIndex;
        SharedFutureBasePointer result = std::move(this->InvokerQueue[index]);

        // If we just picked the front invoker, let's pop the queue.
        if (index == 0)
        {
          this->InvokerQueue.pop_front();
          this->PopFrontNullptr();
        }
        return result;
      }())
  {
    return false;
  }

  this->Invoke(invoker, invokerLock);
  return true;
}

//-----------------------------------------------------------------------------
void vtkThreadedCallbackQueue::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);

  std::lock_guard<std::mutex> lock1(this->Mutex);
  os << indent << "Threads: " << this->NumberOfThreads << std::endl;
  os << indent << "Callback queue size: " << this->InvokerQueue.size() << std::endl;
}

VTK_ABI_NAMESPACE_END