annotate OrthancFramework/Resources/Graveyard/Multithreading/BagOfTasksProcessor.h @ 4361:98f55e7df5ab

rollback incorrect removal for msvc2008
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 08 Dec 2020 19:10:46 +0100
parents bf7b9edf6b81
children d9473bd5ed43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
3640
94f4a18a79cc upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
8 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
10 * the License, or (at your option) any later version.
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
15 * Lesser General Public License for more details.
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
18 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
19 * <http://www.gnu.org/licenses/>.
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #pragma once
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "BagOfTasks.h"
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "SharedMessageQueue.h"
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include <stdint.h>
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <map>
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 namespace Orthanc
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 class BagOfTasksProcessor : public boost::noncopyable
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 private:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 enum BagStatus
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 BagStatus_Running,
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 BagStatus_Canceled,
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 BagStatus_Failed
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 };
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 struct Bag
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 size_t size_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 size_t done_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 BagStatus status_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 Bag() :
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 size_(0),
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 done_(0),
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 status_(BagStatus_Failed)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
2223
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
57 explicit Bag(size_t size) :
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
58 size_(size),
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
59 done_(0),
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
60 status_(BagStatus_Running)
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 };
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 class Task;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 typedef std::map<uint64_t, Bag> Bags;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 typedef std::map<uint64_t, bool> ExitStatus;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 SharedMessageQueue queue_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 boost::mutex mutex_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 uint64_t countBags_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 Bags bags_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 std::vector<boost::thread*> threads_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 ExitStatus exitStatus_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 bool continue_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 boost::condition_variable bagFinished_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 static void Worker(BagOfTasksProcessor* that);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 void Cancel(int64_t bag);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 bool Join(int64_t bag);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 float GetProgress(int64_t bag);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
2110
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1923
diff changeset
90 void SignalProgress(Task& task,
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1923
diff changeset
91 Bag& bag);
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1923
diff changeset
92
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 public:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 class Handle : public boost::noncopyable
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 friend class BagOfTasksProcessor;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 private:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 BagOfTasksProcessor& that_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 uint64_t bag_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 bool hasJoined_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 bool status_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 Handle(BagOfTasksProcessor& that,
1923
6ac7f31fc543 fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1920
diff changeset
105 uint64_t bag,
6ac7f31fc543 fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1920
diff changeset
106 bool empty) :
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 that_(that),
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 bag_(bag),
1923
6ac7f31fc543 fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1920
diff changeset
109 hasJoined_(empty)
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 public:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 ~Handle()
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 Join();
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 void Cancel()
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 that_.Cancel(bag_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 bool Join();
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 float GetProgress()
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 return that_.GetProgress(bag_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 };
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132
2223
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
133 explicit BagOfTasksProcessor(size_t countThreads);
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 ~BagOfTasksProcessor();
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 Handle* Submit(BagOfTasks& tasks);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 };
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 }