Imported Upstream version 3.1.9
[platform/upstream/Imath.git] / src / python / PyImath / PyImathTask.cpp
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 // clang-format off
7
8 #include "PyImathTask.h"
9
10 namespace PyImath {
11
12 static WorkerPool *_currentPool = nullptr;
13
14 // Its not worth dispatching parallel tasks unless the iteration count
15 // is high enough.  The time to create and launch parallel tasks takes
16 // longer than to just do the iterations directly.  This value of '200'
17 // is actually very conservative; in some tests, this number should
18 // probably be in the thousands.
19 static const size_t _minIterations = 200;
20
21 WorkerPool *
22 WorkerPool::currentPool()
23 {
24     return _currentPool;
25 }
26
27 void
28 WorkerPool::setCurrentPool(WorkerPool *pool)
29 {
30     _currentPool = pool;
31 }
32
33 void
34 dispatchTask(Task &task,size_t length)
35 {
36     if (length > _minIterations)
37     {
38         WorkerPool *curpool = WorkerPool::currentPool();
39         if (curpool && !curpool->inWorkerThread())
40         {
41             curpool->dispatch(task,length);
42             return;
43         }
44     }
45     task.execute(0,length,0);
46 }
47
48
49 size_t
50 workers()
51 {
52     WorkerPool *curpool = WorkerPool::currentPool();
53     if (curpool && !curpool->inWorkerThread())
54         return curpool->workers();
55     return 1;
56 }
57
58 }