comparison OrthancCppClient/ArrayFilledByThreads.cpp @ 1:fd402e53d263

new files
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 01 Jun 2015 11:12:20 +0200
parents
children d5027f9f676a
comparison
equal deleted inserted replaced
0:ebc1e38ef615 1:fd402e53d263
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "../PrecompiledHeaders.h"
34 #include "ArrayFilledByThreads.h"
35
36 #include "../MultiThreading/ThreadedCommandProcessor.h"
37 #include "../OrthancException.h"
38
39 namespace Orthanc
40 {
41 class ArrayFilledByThreads::Command : public ICommand
42 {
43 private:
44 ArrayFilledByThreads& that_;
45 size_t index_;
46
47 public:
48 Command(ArrayFilledByThreads& that,
49 size_t index) :
50 that_(that),
51 index_(index)
52 {
53 }
54
55 virtual bool Execute()
56 {
57 std::auto_ptr<IDynamicObject> obj(that_.filler_.GetFillerItem(index_));
58 if (obj.get() == NULL)
59 {
60 return false;
61 }
62 else
63 {
64 boost::mutex::scoped_lock lock(that_.mutex_);
65 that_.array_[index_] = obj.release();
66 return true;
67 }
68 }
69 };
70
71 void ArrayFilledByThreads::Clear()
72 {
73 for (size_t i = 0; i < array_.size(); i++)
74 {
75 if (array_[i])
76 delete array_[i];
77 }
78
79 array_.clear();
80 filled_ = false;
81 }
82
83 void ArrayFilledByThreads::Update()
84 {
85 if (!filled_)
86 {
87 array_.resize(filler_.GetFillerSize());
88
89 Orthanc::ThreadedCommandProcessor processor(threadCount_);
90 for (size_t i = 0; i < array_.size(); i++)
91 {
92 processor.Post(new Command(*this, i));
93 }
94
95 processor.Join();
96 filled_ = true;
97 }
98 }
99
100
101 ArrayFilledByThreads::ArrayFilledByThreads(IFiller& filler) : filler_(filler)
102 {
103 filled_ = false;
104 threadCount_ = 4;
105 }
106
107
108 ArrayFilledByThreads::~ArrayFilledByThreads()
109 {
110 Clear();
111 }
112
113
114 void ArrayFilledByThreads::Reload()
115 {
116 Clear();
117 Update();
118 }
119
120
121 void ArrayFilledByThreads::Invalidate()
122 {
123 Clear();
124 }
125
126
127 void ArrayFilledByThreads::SetThreadCount(unsigned int t)
128 {
129 if (t < 1)
130 {
131 throw OrthancException(ErrorCode_ParameterOutOfRange);
132 }
133
134 threadCount_ = t;
135 }
136
137
138 size_t ArrayFilledByThreads::GetSize()
139 {
140 Update();
141 return array_.size();
142 }
143
144
145 IDynamicObject& ArrayFilledByThreads::GetItem(size_t index)
146 {
147 if (index >= GetSize())
148 {
149 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
150 }
151
152 return *array_[index];
153 }
154 }