comparison Core/MultiThreading/ArrayFilledByThreads.cpp @ 479:0cd977e94479

initial commit of the c++ client
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 16 Jul 2013 09:08:09 +0200
parents
children a811bdf8b8eb
comparison
equal deleted inserted replaced
478:888f8a778e70 479:0cd977e94479
1 #include "ArrayFilledByThreads.h"
2
3 #include "../MultiThreading/ThreadedCommandProcessor.h"
4 #include "../OrthancException.h"
5
6 namespace Orthanc
7 {
8 class ArrayFilledByThreads::Command : public ICommand
9 {
10 private:
11 ArrayFilledByThreads& that_;
12 size_t index_;
13
14 public:
15 Command(ArrayFilledByThreads& that,
16 size_t index) :
17 that_(that),
18 index_(index)
19 {
20 }
21
22 virtual bool Execute()
23 {
24 std::auto_ptr<IDynamicObject> obj(that_.filler_.GetFillerItem(index_));
25 if (obj.get() == NULL)
26 {
27 return false;
28 }
29 else
30 {
31 boost::mutex::scoped_lock lock(that_.mutex_);
32 that_.array_[index_] = obj.release();
33 return true;
34 }
35 }
36 };
37
38 void ArrayFilledByThreads::Clear()
39 {
40 for (size_t i = 0; i < array_.size(); i++)
41 {
42 if (array_[i])
43 delete array_[i];
44 }
45
46 array_.clear();
47 filled_ = false;
48 }
49
50 void ArrayFilledByThreads::Update()
51 {
52 if (!filled_)
53 {
54 array_.resize(filler_.GetFillerSize());
55
56 Orthanc::ThreadedCommandProcessor processor(threadCount_);
57 for (size_t i = 0; i < array_.size(); i++)
58 {
59 processor.Post(new Command(*this, i));
60 }
61
62 processor.Join();
63 filled_ = true;
64 }
65 }
66
67
68 ArrayFilledByThreads::ArrayFilledByThreads(IFiller& filler) : filler_(filler)
69 {
70 filled_ = false;
71 threadCount_ = 4;
72 }
73
74
75 ArrayFilledByThreads::~ArrayFilledByThreads()
76 {
77 Clear();
78 }
79
80
81 void ArrayFilledByThreads::Reload()
82 {
83 Clear();
84 Update();
85 }
86
87
88 void ArrayFilledByThreads::Invalidate()
89 {
90 Clear();
91 }
92
93
94 void ArrayFilledByThreads::SetThreadCount(unsigned int t)
95 {
96 if (t < 1)
97 {
98 throw OrthancException(ErrorCode_ParameterOutOfRange);
99 }
100
101 threadCount_ = t;
102 }
103
104
105 size_t ArrayFilledByThreads::GetSize()
106 {
107 Update();
108 return array_.size();
109 }
110
111
112 IDynamicObject& ArrayFilledByThreads::GetItem(size_t index)
113 {
114 if (index >= GetSize())
115 {
116 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
117 }
118
119 return *array_[index];
120 }
121 }