annotate OrthancFramework/Resources/Graveyard/Multithreading/BagOfTasksProcessor.h @ 5095:b52fe770aec0

more logs in HttpClient
author Alain Mazy <am@osimis.io>
date Wed, 12 Oct 2022 09:22:08 +0200
parents 43e613a7756b
children 0ea402b4d901
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
4870
43e613a7756b upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4831
diff changeset
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
43e613a7756b upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4831
diff changeset
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * 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
9 * 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
10 * 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
11 * the License, or (at your option) any later version.
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * 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
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * 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
16 * Lesser General Public License for more details.
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
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
18 * 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
19 * 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
20 * <http://www.gnu.org/licenses/>.
1920
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #pragma once
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "BagOfTasks.h"
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include "SharedMessageQueue.h"
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <stdint.h>
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include <map>
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 namespace Orthanc
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 class BagOfTasksProcessor : public boost::noncopyable
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 private:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 enum BagStatus
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 BagStatus_Running,
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 BagStatus_Canceled,
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 BagStatus_Failed
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 struct Bag
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 size_t size_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 size_t done_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 BagStatus status_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 Bag() :
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 size_(0),
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 done_(0),
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 status_(BagStatus_Failed)
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 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
2223
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
58 explicit Bag(size_t size) :
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
59 size_(size),
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
60 done_(0),
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
61 status_(BagStatus_Running)
1920
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 class Task;
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 typedef std::map<uint64_t, Bag> Bags;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 typedef std::map<uint64_t, bool> ExitStatus;
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 SharedMessageQueue queue_;
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 boost::mutex mutex_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 uint64_t countBags_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 Bags bags_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 std::vector<boost::thread*> threads_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 ExitStatus exitStatus_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 bool continue_;
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 boost::condition_variable bagFinished_;
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 static void Worker(BagOfTasksProcessor* that);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 void Cancel(int64_t bag);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 bool Join(int64_t bag);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 float GetProgress(int64_t bag);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
2110
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1923
diff changeset
91 void SignalProgress(Task& task,
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1923
diff changeset
92 Bag& bag);
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1923
diff changeset
93
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 public:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 class Handle : public boost::noncopyable
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 friend class BagOfTasksProcessor;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 private:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 BagOfTasksProcessor& that_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 uint64_t bag_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 bool hasJoined_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 bool status_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 Handle(BagOfTasksProcessor& that,
1923
6ac7f31fc543 fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1920
diff changeset
106 uint64_t bag,
6ac7f31fc543 fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1920
diff changeset
107 bool empty) :
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 that_(that),
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 bag_(bag),
1923
6ac7f31fc543 fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1920
diff changeset
110 hasJoined_(empty)
1920
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 public:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 ~Handle()
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 Join();
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 void Cancel()
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 that_.Cancel(bag_);
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 bool Join();
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 float GetProgress()
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 return that_.GetProgress(bag_);
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
2223
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2110
diff changeset
134 explicit BagOfTasksProcessor(size_t countThreads);
1920
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 ~BagOfTasksProcessor();
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 Handle* Submit(BagOfTasks& tasks);
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 }