OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimJobQueue.cpp
Go to the documentation of this file.
2 
3 #include <algorithm> /* for std::find */
4 
6 {
7 }
8 
9 void ossimJobQueue::add(std::shared_ptr<ossimJob> job,
10  bool guaranteeUniqueFlag)
11 {
12  std::shared_ptr<Callback> cb;
13  {
14  {
15  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
16 
17  if(guaranteeUniqueFlag)
18  {
19  if(findByPointer(job) != m_jobQueue.end())
20  {
21  m_block.set(true);
22  return;
23  }
24  }
25  cb = m_callback;
26  }
27  if(cb) cb->adding(getSharedFromThis(), job);
28 
29  job->ready();
30  m_jobQueueMutex.lock();
31  m_jobQueue.push_back(job);
32  m_jobQueueMutex.unlock();
33  }
34  if(cb)
35  {
36  cb->added(getSharedFromThis(), job);
37  }
38  m_block.set(true);
39 }
40 
41 std::shared_ptr<ossimJob> ossimJobQueue::removeByName(const ossimString& name)
42 {
43  std::shared_ptr<ossimJob> result;
44  std::shared_ptr<Callback> cb;
45  if(name.empty()) return result;
46  {
47  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
48  ossimJob::List::iterator iter = findByName(name);
49  if(iter!=m_jobQueue.end())
50  {
51  result = *iter;
52  m_jobQueue.erase(iter);
53  }
54  cb = m_callback;
55  }
56  m_block.set(!m_jobQueue.empty());
57 
58  if(cb&&result)
59  {
60  cb->removed(getSharedFromThis(), result);
61  }
62  return result;
63 }
64 std::shared_ptr<ossimJob> ossimJobQueue::removeById(const ossimString& id)
65 {
66  std::shared_ptr<ossimJob> result;
67  std::shared_ptr<Callback> cb;
68  if(id.empty()) return result;
69  {
70  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
71  ossimJob::List::iterator iter = findById(id);
72  if(iter!=m_jobQueue.end())
73  {
74  result = *iter;
75  m_jobQueue.erase(iter);
76  }
77  cb = m_callback;
78  m_block.set(!m_jobQueue.empty());
79  }
80  if(cb&&result)
81  {
82  cb->removed(getSharedFromThis(), result);
83  }
84  return result;
85 }
86 
87 void ossimJobQueue::remove(const std::shared_ptr<ossimJob> job)
88 {
89  std::shared_ptr<ossimJob> removedJob;
90  std::shared_ptr<Callback> cb;
91  {
92  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
93  ossimJob::List::iterator iter = std::find(m_jobQueue.begin(), m_jobQueue.end(), job);
94  if(iter!=m_jobQueue.end())
95  {
96  removedJob = (*iter);
97  m_jobQueue.erase(iter);
98  }
99  cb = m_callback;
100  }
101  if(cb&&removedJob)
102  {
103  cb->removed(getSharedFromThis(), removedJob);
104  }
105 }
106 
108 {
109  ossimJob::List removedJobs;
110  std::shared_ptr<Callback> cb;
111  {
112  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
113  cb = m_callback;
114  ossimJob::List::iterator iter = m_jobQueue.begin();
115  while(iter!=m_jobQueue.end())
116  {
117  if((*iter)->isStopped())
118  {
119  removedJobs.push_back(*iter);
120  iter = m_jobQueue.erase(iter);
121  }
122  else
123  {
124  ++iter;
125  }
126  }
127  }
128  if(!removedJobs.empty())
129  {
130  if(cb)
131  {
132  ossimJob::List::iterator iter = removedJobs.begin();
133  while(iter!=removedJobs.end())
134  {
135  cb->removed(getSharedFromThis(), (*iter));
136  ++iter;
137  }
138  }
139  removedJobs.clear();
140  }
141 }
142 
144 {
145  ossimJob::List removedJobs(m_jobQueue);
146  std::shared_ptr<Callback> cb;
147  {
148  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
149  m_jobQueue.clear();
150  cb = m_callback;
151  }
152  if(cb)
153  {
154  for(ossimJob::List::iterator iter=removedJobs.begin();iter!=removedJobs.end();++iter)
155  {
156  cb->removed(getSharedFromThis(), (*iter));
157  }
158  }
159 }
160 
161 std::shared_ptr<ossimJob> ossimJobQueue::nextJob(bool blockIfEmptyFlag)
162 {
163  m_jobQueueMutex.lock();
164  bool emptyFlag = m_jobQueue.empty();
165  m_jobQueueMutex.unlock();
166  if (blockIfEmptyFlag && emptyFlag)
167  {
168  m_block.block();
169  }
170 
171  std::shared_ptr<ossimJob> result;
172  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
173 
174  if (m_jobQueue.empty())
175  {
176  m_block.set(false);
177  return result;
178  }
179 
180  ossimJob::List::iterator iter= m_jobQueue.begin();
181  while((iter != m_jobQueue.end())&&
182  (((*iter)->isCanceled())))
183  {
184  (*iter)->finished(); // mark the ob as being finished
185  iter = m_jobQueue.erase(iter);
186  }
187  if(iter != m_jobQueue.end())
188  {
189  result = *iter;
190  m_jobQueue.erase(iter);
191  }
192  m_block.set(!m_jobQueue.empty());
193 
194  return result;
195 }
197 {
198  m_block.release();
199 }
201 {
202  // std::lock_guard<std::mutex> lock(m_jobQueueMutex);
203  // return m_jobQueue.empty();
204  m_jobQueueMutex.lock();
205  bool result = m_jobQueue.empty();
206  m_jobQueueMutex.unlock();
207  return result;
208 }
209 
211 {
212  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
213  return (ossim_uint32) m_jobQueue.size();
214 }
215 
216 ossimJob::List::iterator ossimJobQueue::findById(const ossimString& id)
217 {
218  if(id.empty()) return m_jobQueue.end();
219  ossimJob::List::iterator iter = m_jobQueue.begin();
220  while(iter != m_jobQueue.end())
221  {
222  if(id == (*iter)->id())
223  {
224  return iter;
225  }
226  ++iter;
227  }
228  return m_jobQueue.end();
229 }
230 
231 ossimJob::List::iterator ossimJobQueue::findByName(const ossimString& name)
232 {
233  if(name.empty()) return m_jobQueue.end();
234  ossimJob::List::iterator iter = m_jobQueue.begin();
235  while(iter != m_jobQueue.end())
236  {
237  if(name == (*iter)->name())
238  {
239  return iter;
240  }
241  ++iter;
242  }
243  return m_jobQueue.end();
244 }
245 
246 ossimJob::List::iterator ossimJobQueue::findByPointer(const std::shared_ptr<ossimJob> job)
247 {
248  return std::find(m_jobQueue.begin(),
249  m_jobQueue.end(),
250  job);
251 }
252 
253 ossimJob::List::iterator ossimJobQueue::findByNameOrPointer(const std::shared_ptr<ossimJob> job)
254 {
255  ossimString n = job->name();
256  ossimJob::List::iterator iter = std::find_if(m_jobQueue.begin(), m_jobQueue.end(), [n, job](const std::shared_ptr<ossimJob> jobIter){
257  bool result = (jobIter == job);
258  if(!result&&!n.empty()) result = jobIter->name() == n;
259  return result;
260  });
261  // ossimJob::List::iterator iter = m_jobQueue.begin();
262  // while(iter != m_jobQueue.end())
263  // {
264  // if((*iter) == job)
265  // {
266  // return iter;
267  // }
268  // else if((!n.empty())&&
269  // (job->name() == (*iter)->name()))
270  // {
271  // return iter;
272  // }
273  // ++iter;
274 // }
275 
276  return iter;
277 }
278 
279 bool ossimJobQueue::hasJob(std::shared_ptr<ossimJob> job)
280 {
281  ossimJob::List::const_iterator iter = m_jobQueue.begin();
282  while(iter != m_jobQueue.end())
283  {
284  if(job == (*iter))
285  {
286  return true;
287  }
288  ++iter;
289  }
290 
291  return false;
292 }
293 
294 void ossimJobQueue::setCallback(std::shared_ptr<Callback> c)
295 {
296  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
297  m_callback = c;
298 }
299 
300 std::shared_ptr<ossimJobQueue::Callback> ossimJobQueue::callback()
301 {
302  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
303  return m_callback;
304 }
std::shared_ptr< ossimJobQueue > getSharedFromThis()
This is the safe way to create a std::shared_ptr for &#39;this&#39;.
virtual void clear()
Will clear the queue.
ossim::Block m_block
void set(bool releaseFlag)
Will set the relase flag and wake up all threads to test the condition again.
Definition: Block.cpp:23
virtual void releaseBlock()
will release the block and have any blocked threads continue
ossimJob::List::iterator findByName(const ossimString &name)
Internal method that returns an iterator.
ossimJob::List::iterator findByPointer(const std::shared_ptr< ossimJob > job)
Internal method that returns an iterator.
std::shared_ptr< Callback > callback()
ossimJob::List m_jobQueue
std::shared_ptr< Callback > m_callback
void release()
Releases the threads and will not return until all threads are released.
Definition: Block.cpp:66
ossimJob::List::iterator findByNameOrPointer(const std::shared_ptr< ossimJob > job)
Internal method that returns an iterator.
virtual void remove(const std::shared_ptr< ossimJob > Job)
Allows one to pass in a job pointer to remove.
os2<< "> n<< " > nendobj n
ossimJob::List::iterator findById(const ossimString &id)
Internal method that returns an iterator.
virtual std::shared_ptr< ossimJob > removeById(const ossimString &id)
Allows one to remove a job passing in it&#39;s id.
unsigned int ossim_uint32
void setCallback(std::shared_ptr< Callback > c)
Allows one to set the callback to the list.
void block()
Will block the calling thread based on the internal condition.
Definition: Block.cpp:33
virtual std::shared_ptr< ossimJob > removeByName(const ossimString &name)
Allows one to remove a job passing in it&#39;s name.
ossim_uint32 size()
std::list< std::shared_ptr< ossimJob > > List
Definition: ossimJob.h:152
bool empty() const
Definition: ossimString.h:411
bool hasJob(std::shared_ptr< ossimJob > job)
Internal method that determines if we have the job.
std::mutex m_jobQueueMutex
ossimJobQueue()
Default constructor.
virtual std::shared_ptr< ossimJob > nextJob(bool blockIfEmptyFlag=true)
Will grab the next job on the list and will return the job or a null shared_ptr.
virtual void removeStoppedJobs()
Will remove any stopped jobs from the queue.
virtual void add(std::shared_ptr< ossimJob > job, bool guaranteeUniqueFlag=true)
Will add a job to the queue and if the guaranteeUniqueFlag is set it will scan and make sure the job ...
bool isEmpty() const