annotate Core/MultiThreading/BagOfTasksProcessor.h @ 2232:3dd44baebc36

macro ORTHANC_ENABLE_LUA for orthanc-wsi
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 14 Dec 2016 16:57:21 +0100
parents 29689b30f9ae
children a3a65de1840f
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
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 * 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
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
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 * You should have received a copy of the GNU General Public License
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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
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 #pragma once
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 #include "BagOfTasks.h"
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include "SharedMessageQueue.h"
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 #include <stdint.h>
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include <map>
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 namespace Orthanc
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 class BagOfTasksProcessor : public boost::noncopyable
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 private:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 enum BagStatus
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 BagStatus_Running,
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 BagStatus_Canceled,
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 BagStatus_Failed
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 };
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 struct Bag
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 size_t size_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 size_t done_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 BagStatus status_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 Bag() :
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 size_(0),
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 done_(0),
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 status_(BagStatus_Failed)
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 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
2223
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
67 explicit Bag(size_t size) :
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
68 size_(size),
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
69 done_(0),
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
70 status_(BagStatus_Running)
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 {
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 };
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 class Task;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 typedef std::map<uint64_t, Bag> Bags;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 typedef std::map<uint64_t, bool> ExitStatus;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 SharedMessageQueue queue_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 boost::mutex mutex_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 uint64_t countBags_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 Bags bags_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 std::vector<boost::thread*> threads_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 ExitStatus exitStatus_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 bool continue_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 boost::condition_variable bagFinished_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 static void Worker(BagOfTasksProcessor* that);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 void Cancel(int64_t bag);
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 bool Join(int64_t bag);
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 float GetProgress(int64_t bag);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
2110
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1923
diff changeset
100 void SignalProgress(Task& task,
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1923
diff changeset
101 Bag& bag);
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1923
diff changeset
102
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 public:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 class Handle : public boost::noncopyable
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 friend class BagOfTasksProcessor;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 private:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 BagOfTasksProcessor& that_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 uint64_t bag_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 bool hasJoined_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 bool status_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 Handle(BagOfTasksProcessor& that,
1923
6ac7f31fc543 fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1920
diff changeset
115 uint64_t bag,
6ac7f31fc543 fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1920
diff changeset
116 bool empty) :
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 that_(that),
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 bag_(bag),
1923
6ac7f31fc543 fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1920
diff changeset
119 hasJoined_(empty)
1920
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 }
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 public:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 ~Handle()
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 Join();
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 void Cancel()
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 that_.Cancel(bag_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 bool Join();
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 float GetProgress()
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 return that_.GetProgress(bag_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 };
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142
2223
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
143 explicit BagOfTasksProcessor(size_t countThreads);
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 ~BagOfTasksProcessor();
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 Handle* Submit(BagOfTasks& tasks);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 };
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }