Blame view

3rdparty/opencv-4.5.4/modules/core/src/async.cpp 12.5 KB
f4334277   Hu Chunming   提交3rdparty
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
  // This file is part of OpenCV project.
  // It is subject to the license terms in the LICENSE file found in the top-level directory
  // of this distribution and at http://opencv.org/license.html.
  
  #include "precomp.hpp"
  //#undef CV_CXX11  // debug non C++11 mode
  #include "opencv2/core/async.hpp"
  #include "opencv2/core/detail/async_promise.hpp"
  
  #include "opencv2/core/cvstd.hpp"
  
  #include <opencv2/core/utils/logger.defines.hpp>
  #undef CV_LOG_STRIP_LEVEL
  #define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_DEBUG + 1
  #include <opencv2/core/utils/logger.hpp>
  
  #ifndef OPENCV_DISABLE_THREAD_SUPPORT
  
  #ifdef CV_CXX11
  #include <mutex>
  #include <condition_variable>
  #include <chrono>
  #endif
  
  namespace cv {
  
  /**
  Manages shared state of asynchronous result
  */
  struct AsyncArray::Impl
  {
      int refcount;
      void addrefFuture() CV_NOEXCEPT { CV_XADD(&refcount_future, 1); CV_XADD(&refcount, 1); } \
      void releaseFuture() CV_NOEXCEPT { CV_XADD(&refcount_future, -1); if(1 == CV_XADD(&refcount, -1)) delete this; } \
      int refcount_future;
      void addrefPromise() CV_NOEXCEPT { CV_XADD(&refcount_promise, 1); CV_XADD(&refcount, 1); } \
      void releasePromise() CV_NOEXCEPT { CV_XADD(&refcount_promise, -1); if(1 == CV_XADD(&refcount, -1)) delete this; } \
      int refcount_promise;
  
  #ifdef CV_CXX11
      mutable std::mutex mtx;
      mutable std::condition_variable cond_var;
  #else
      mutable cv::Mutex mtx;
  #endif
  
      mutable bool has_result; // Mat, UMat or exception
  
      mutable cv::Ptr<Mat> result_mat;
      mutable cv::Ptr<UMat> result_umat;
  
  
      bool has_exception;
  #if CV__EXCEPTION_PTR
      std::exception_ptr exception;
  #endif
      cv::Exception cv_exception;
  
      mutable bool result_is_fetched;
  
      bool future_is_returned;
  
      Impl()
          : refcount(1), refcount_future(0), refcount_promise(1)
          , has_result(false)
          , has_exception(false)
          , result_is_fetched(false)
          , future_is_returned(false)
      {
          // nothing
      }
  
      ~Impl()
      {
          if (has_result && !result_is_fetched)
          {
              CV_LOG_INFO(NULL, "Asynchronous result has not been fetched");
          }
      }
  
      bool get(OutputArray dst, int64 timeoutNs) const
      {
          CV_Assert(!result_is_fetched);
          if (!has_result)
          {
              if(refcount_promise == 0)
                  CV_Error(Error::StsInternal, "Asynchronous result producer has been destroyed");
              if (!wait_for(timeoutNs))
                  return false;
          }
  #ifdef CV_CXX11
          std::unique_lock<std::mutex> lock(mtx);
  #else
          cv::AutoLock lock(mtx);
  #endif
          if (has_result)
          {
              if (!result_mat.empty())
              {
                  dst.move(*result_mat.get());
                  result_mat.release();
                  result_is_fetched = true;
                  return true;
              }
              if (!result_umat.empty())
              {
                  dst.move(*result_umat.get());
                  result_umat.release();
                  result_is_fetched = true;
                  return true;
              }
  #if CV__EXCEPTION_PTR
              if (has_exception && exception)
              {
                  result_is_fetched = true;
                  std::rethrow_exception(exception);
              }
  #endif
              if (has_exception)
              {
                  result_is_fetched = true;
                  throw cv_exception;
              }
              CV_Error(Error::StsInternal, "AsyncArray: invalid state of 'has_result = true'");
          }
          CV_Assert(!has_result);
          CV_Assert(timeoutNs < 0);
          return false;
      }
  
      bool valid() const CV_NOEXCEPT
      {
          if (result_is_fetched)
              return false;
          if (refcount_promise == 0 && !has_result)
              return false;
          return true;
      }
  
      bool wait_for(int64 timeoutNs) const
      {
          CV_Assert(valid());
          if (has_result)
              return has_result;
          if (timeoutNs == 0)
              return has_result;
          CV_LOG_INFO(NULL, "Waiting for async result ...");
  #ifdef CV_CXX11
          std::unique_lock<std::mutex> lock(mtx);
          const auto cond_pred = [&]{ return has_result == true; };
          if (timeoutNs > 0)
              return cond_var.wait_for(lock, std::chrono::nanoseconds(timeoutNs), cond_pred);
          else
          {
              cond_var.wait(lock, cond_pred);
              CV_Assert(has_result);
              return true;
          }
  #else
          CV_Error(Error::StsNotImplemented, "OpenCV has been built without async waiting support (C++11 is required)");
  #endif
      }
  
      AsyncArray getArrayResult()
      {
          CV_Assert(refcount_future == 0);
          AsyncArray result;
          addrefFuture();
          result.p = this;
          future_is_returned = true;
          return result;
      }
  
      void setValue(InputArray value)
      {
          if (future_is_returned && refcount_future == 0)
              CV_Error(Error::StsError, "Associated AsyncArray has been destroyed");
  #ifdef CV_CXX11
          std::unique_lock<std::mutex> lock(mtx);
  #else
          cv::AutoLock lock(mtx);
  #endif
          CV_Assert(!has_result);
          int k = value.kind();
          if (k == _InputArray::UMAT)
          {
              result_umat = makePtr<UMat>();
              value.copyTo(*result_umat.get());
          }
          else
          {
              result_mat = makePtr<Mat>();
              value.copyTo(*result_mat.get());
          }
          has_result = true;
  #ifdef CV_CXX11
          cond_var.notify_all();
  #endif
      }
  
  #if CV__EXCEPTION_PTR
      void setException(std::exception_ptr e)
      {
          if (future_is_returned && refcount_future == 0)
              CV_Error(Error::StsError, "Associated AsyncArray has been destroyed");
  #ifdef CV_CXX11
          std::unique_lock<std::mutex> lock(mtx);
  #else
          cv::AutoLock lock(mtx);
  #endif
          CV_Assert(!has_result);
          has_exception = true;
          exception = e;
          has_result = true;
  #ifdef CV_CXX11
          cond_var.notify_all();
  #endif
      }
  #endif
  
      void setException(const cv::Exception e)
      {
          if (future_is_returned && refcount_future == 0)
              CV_Error(Error::StsError, "Associated AsyncArray has been destroyed");
  #ifdef CV_CXX11
          std::unique_lock<std::mutex> lock(mtx);
  #else
          cv::AutoLock lock(mtx);
  #endif
          CV_Assert(!has_result);
          has_exception = true;
          cv_exception = e;
          has_result = true;
  #ifdef CV_CXX11
          cond_var.notify_all();
  #endif
      }
  };
  
  }  // namespace
  
  #else  // OPENCV_DISABLE_THREAD_SUPPORT
  
  namespace cv {
  
  // no threading
  struct AsyncArray::Impl
  {
      int refcount;
      void addrefFuture() CV_NOEXCEPT { refcount_future++; refcount++; }
      void releaseFuture() CV_NOEXCEPT { refcount_future--; if (0 == --refcount) delete this; }
      int refcount_future;
      void addrefPromise() CV_NOEXCEPT { refcount_promise++; refcount++; } \
      void releasePromise() CV_NOEXCEPT { refcount_promise--; if (0 == --refcount) delete this; }
      int refcount_promise;
  
      mutable bool has_result; // Mat, UMat or exception
  
      mutable cv::Ptr<Mat> result_mat;
      mutable cv::Ptr<UMat> result_umat;
  
  
      bool has_exception;
  #if CV__EXCEPTION_PTR
      std::exception_ptr exception;
  #endif
      cv::Exception cv_exception;
  
      mutable bool result_is_fetched;
  
      bool future_is_returned;
  
      Impl()
          : refcount(1), refcount_future(0), refcount_promise(1)
          , has_result(false)
          , has_exception(false)
          , result_is_fetched(false)
          , future_is_returned(false)
      {
          // nothing
      }
  
      ~Impl()
      {
          if (has_result && !result_is_fetched)
          {
              CV_LOG_INFO(NULL, "Asynchronous result has not been fetched");
          }
      }
  
      bool get(OutputArray dst, int64 timeoutNs) const
      {
          CV_Assert(!result_is_fetched);
          if (!has_result)
          {
              CV_UNUSED(timeoutNs);
              CV_Error(Error::StsError, "Result is not produced (unable to wait for result in OPENCV_DISABLE_THREAD_SUPPORT mode)");
          }
          if (!result_mat.empty())
          {
              dst.move(*result_mat.get());
              result_mat.release();
              result_is_fetched = true;
              return true;
          }
          if (!result_umat.empty())
          {
              dst.move(*result_umat.get());
              result_umat.release();
              result_is_fetched = true;
              return true;
          }
  #if CV__EXCEPTION_PTR
          if (has_exception && exception)
          {
              result_is_fetched = true;
              std::rethrow_exception(exception);
          }
  #endif
          if (has_exception)
          {
              result_is_fetched = true;
              throw cv_exception;
          }
          CV_Error(Error::StsInternal, "AsyncArray: invalid state of 'has_result = true'");
          return false;
      }
  
      bool valid() const CV_NOEXCEPT
      {
          if (result_is_fetched)
              return false;
          if (refcount_promise == 0 && !has_result)
              return false;
          return true;
      }
  
      bool wait_for(int64 timeoutNs) const
      {
          CV_Assert(valid());
          if (has_result)
              return has_result;
          if (timeoutNs == 0)
              return has_result;
          CV_Error(Error::StsError, "Unable to wait in OPENCV_DISABLE_THREAD_SUPPORT mode");
      }
  
      AsyncArray getArrayResult()
      {
          CV_Assert(refcount_future == 0);
          AsyncArray result;
          addrefFuture();
          result.p = this;
          future_is_returned = true;
          return result;
      }
  
      void setValue(InputArray value)
      {
          if (future_is_returned && refcount_future == 0)
              CV_Error(Error::StsError, "Associated AsyncArray has been destroyed");
          CV_Assert(!has_result);
          int k = value.kind();
          if (k == _InputArray::UMAT)
          {
              result_umat = makePtr<UMat>();
              value.copyTo(*result_umat.get());
          }
          else
          {
              result_mat = makePtr<Mat>();
              value.copyTo(*result_mat.get());
          }
          has_result = true;
      }
  
  #if CV__EXCEPTION_PTR
      void setException(std::exception_ptr e)
      {
          if (future_is_returned && refcount_future == 0)
              CV_Error(Error::StsError, "Associated AsyncArray has been destroyed");
          CV_Assert(!has_result);
          has_exception = true;
          exception = e;
          has_result = true;
      }
  #endif
  
      void setException(const cv::Exception e)
      {
          if (future_is_returned && refcount_future == 0)
              CV_Error(Error::StsError, "Associated AsyncArray has been destroyed");
          CV_Assert(!has_result);
          has_exception = true;
          cv_exception = e;
          has_result = true;
      }
  };
  
  }
  
  #endif  // OPENCV_DISABLE_THREAD_SUPPORT
  
  namespace cv {
  
  AsyncArray::AsyncArray() CV_NOEXCEPT
      : p(NULL)
  {
  }
  
  AsyncArray::~AsyncArray() CV_NOEXCEPT
  {
      release();
  }
  
  AsyncArray::AsyncArray(const AsyncArray& o) CV_NOEXCEPT
      : p(o.p)
  {
      if (p)
          p->addrefFuture();
  }
  
  AsyncArray& AsyncArray::operator=(const AsyncArray& o) CV_NOEXCEPT
  {
      Impl* newp = o.p;
      if (newp)
          newp->addrefFuture();
      release();
      p = newp;
      return *this;
  }
  
  void AsyncArray::release() CV_NOEXCEPT
  {
      Impl* impl = p;
      p = NULL;
      if (impl)
          impl->releaseFuture();
  }
  
  bool AsyncArray::get(OutputArray dst, int64 timeoutNs) const
  {
      CV_Assert(p);
      return p->get(dst, timeoutNs);
  }
  
  void AsyncArray::get(OutputArray dst) const
  {
      CV_Assert(p);
      bool res = p->get(dst, -1);
      CV_Assert(res);
  }
  
  bool AsyncArray::wait_for(int64 timeoutNs) const
  {
      CV_Assert(p);
      return p->wait_for(timeoutNs);
  }
  
  bool AsyncArray::valid() const CV_NOEXCEPT
  {
      if (!p) return false;
      return p->valid();
  }
  
  
  //
  // AsyncPromise
  //
  
  AsyncPromise::AsyncPromise() CV_NOEXCEPT
      : p(new AsyncArray::Impl())
  {
  }
  
  AsyncPromise::~AsyncPromise() CV_NOEXCEPT
  {
      release();
  }
  
  AsyncPromise::AsyncPromise(const AsyncPromise& o) CV_NOEXCEPT
      : p(o.p)
  {
      if (p)
          p->addrefPromise();
  }
  
  AsyncPromise& AsyncPromise::operator=(const AsyncPromise& o) CV_NOEXCEPT
  {
      Impl* newp = o.p;
      if (newp)
          newp->addrefPromise();
      release();
      p = newp;
      return *this;
  }
  
  void AsyncPromise::release() CV_NOEXCEPT
  {
      Impl* impl = p;
      p = NULL;
      if (impl)
          impl->releasePromise();
  }
  
  AsyncArray AsyncPromise::getArrayResult()
  {
      CV_Assert(p);
      return p->getArrayResult();
  }
  
  void AsyncPromise::setValue(InputArray value)
  {
      CV_Assert(p);
      return p->setValue(value);
  }
  
  void AsyncPromise::setException(const cv::Exception& exception)
  {
      CV_Assert(p);
      return p->setException(exception);
  }
  
  #if CV__EXCEPTION_PTR
  void AsyncPromise::setException(std::exception_ptr exception)
  {
      CV_Assert(p);
      return p->setException(exception);
  }
  #endif
  
  } // namespace