OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ossimJobQueue Class Reference

This is the base implementation for the job queue. More...

#include <ossimJobQueue.h>

Inheritance diagram for ossimJobQueue:

Classes

class  Callback
 The callback allows one to attach and listen for certain things. More...
 

Public Member Functions

 ossimJobQueue ()
 Default constructor. More...
 
std::shared_ptr< ossimJobQueuegetSharedFromThis ()
 This is the safe way to create a std::shared_ptr for 'this'. More...
 
std::shared_ptr< const ossimJobQueuegetSharedFromThis () const
 This is the safe way to create a std::shared_ptr for 'this'. More...
 
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 is not on the queue before adding. More...
 
virtual std::shared_ptr< ossimJobremoveByName (const ossimString &name)
 Allows one to remove a job passing in it's name. More...
 
virtual std::shared_ptr< ossimJobremoveById (const ossimString &id)
 Allows one to remove a job passing in it's id. More...
 
virtual void remove (const std::shared_ptr< ossimJob > Job)
 Allows one to pass in a job pointer to remove. More...
 
virtual void removeStoppedJobs ()
 Will remove any stopped jobs from the queue. More...
 
virtual void clear ()
 Will clear the queue. More...
 
virtual std::shared_ptr< ossimJobnextJob (bool blockIfEmptyFlag=true)
 Will grab the next job on the list and will return the job or a null shared_ptr. More...
 
virtual void releaseBlock ()
 will release the block and have any blocked threads continue More...
 
bool isEmpty () const
 
ossim_uint32 size ()
 
void setCallback (std::shared_ptr< Callback > c)
 Allows one to set the callback to the list. More...
 
std::shared_ptr< Callbackcallback ()
 

Protected Member Functions

ossimJob::List::iterator findById (const ossimString &id)
 Internal method that returns an iterator. More...
 
ossimJob::List::iterator findByName (const ossimString &name)
 Internal method that returns an iterator. More...
 
ossimJob::List::iterator findByPointer (const std::shared_ptr< ossimJob > job)
 Internal method that returns an iterator. More...
 
ossimJob::List::iterator findByNameOrPointer (const std::shared_ptr< ossimJob > job)
 Internal method that returns an iterator. More...
 
bool hasJob (std::shared_ptr< ossimJob > job)
 Internal method that determines if we have the job. More...
 

Protected Attributes

std::mutex m_jobQueueMutex
 
ossim::Block m_block
 
ossimJob::List m_jobQueue
 
std::shared_ptr< Callbackm_callback
 

Detailed Description

This is the base implementation for the job queue.

It allows one to add and remove jobs and to call the nextJob and, if specified, block the call if no jobs are on the queue. we derived from std::enable_shared_from_this which allows us access to a safe shared 'this' pointer. This is used internal to our callbacks.

The job queue is thread safe and can be shared by multiple threads.

Here is a quick code example on how to create a shared queue and to attach a thread to it. In this example we do not block the calling thread for nextJob

#include <iostream>
class TestJobQueueThread : public ossim::Thread
{
public:
TestJobQueueThread(std::shared_ptr<ossimJobQueue> q):m_q(q){}
void run()
{
if(m_q)
{
while(true)
{
interrupt();
std::shared_ptr<ossimJob> job = m_q->nextJob(false);
if(job)
{
job->start();
}
yieldCurrentThread();
sleepInMilliSeconds(20);
}
}
}
private:
std::shared_ptr<ossimJobQueue> m_q;
};
class TestJob : public ossimJob
{
public:
virtual void run()
{
std:cout << "Running Job\n";
std::cout << "Finished Running Job\n";
}
};
int main(int argc, char *argv[])
{
std::shared_ptr<ossimJobQueue> q = std::make_shared<ossimJobQueue>();
std::shared_ptr<TestJobQueueThread> jobQueueThread = std::make_shared<TestJobQueueThread>(q);
jobQueueThread->start();
q->add(std::make_shared<TestJob>());
q->add(std::make_shared<TestJob>());
q->add(std::make_shared<TestJob>());
jobQueueThread->cancel();
jobQueueThread->waitForCompletion();
}

Definition at line 84 of file ossimJobQueue.h.

Constructor & Destructor Documentation

◆ ossimJobQueue()

ossimJobQueue::ossimJobQueue ( )

Default constructor.

Definition at line 5 of file ossimJobQueue.cpp.

6 {
7 }

Member Function Documentation

◆ add()

void ossimJobQueue::add ( std::shared_ptr< ossimJob job,
bool  guaranteeUniqueFlag = true 
)
virtual

Will add a job to the queue and if the guaranteeUniqueFlag is set it will scan and make sure the job is not on the queue before adding.

Parameters
jobThe job to add to the queue.
guaranteeUniqueFlagif set to true will force a find to make sure the job does not already exist

Definition at line 9 of file ossimJobQueue.cpp.

References findByPointer(), getSharedFromThis(), m_block, m_callback, m_jobQueue, m_jobQueueMutex, and ossim::Block::set().

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 }
std::shared_ptr< ossimJobQueue > getSharedFromThis()
This is the safe way to create a std::shared_ptr for &#39;this&#39;.
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
ossimJob::List::iterator findByPointer(const std::shared_ptr< ossimJob > job)
Internal method that returns an iterator.
ossimJob::List m_jobQueue
std::shared_ptr< Callback > m_callback
std::mutex m_jobQueueMutex

◆ callback()

std::shared_ptr< ossimJobQueue::Callback > ossimJobQueue::callback ( )
Returns
the callback

Definition at line 300 of file ossimJobQueue.cpp.

References m_callback, and m_jobQueueMutex.

301 {
302  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
303  return m_callback;
304 }
std::shared_ptr< Callback > m_callback
std::mutex m_jobQueueMutex

◆ clear()

void ossimJobQueue::clear ( )
virtual

Will clear the queue.

Definition at line 143 of file ossimJobQueue.cpp.

References getSharedFromThis(), m_callback, m_jobQueue, and m_jobQueueMutex.

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 }
std::shared_ptr< ossimJobQueue > getSharedFromThis()
This is the safe way to create a std::shared_ptr for &#39;this&#39;.
ossimJob::List m_jobQueue
std::shared_ptr< Callback > m_callback
std::list< std::shared_ptr< ossimJob > > List
Definition: ossimJob.h:152
std::mutex m_jobQueueMutex

◆ findById()

ossimJob::List::iterator ossimJobQueue::findById ( const ossimString id)
protected

Internal method that returns an iterator.

Parameters
theid of the job to search for
Returns
the iterator

Definition at line 216 of file ossimJobQueue.cpp.

References m_jobQueue.

Referenced by removeById().

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 }
ossimJob::List m_jobQueue

◆ findByName()

ossimJob::List::iterator ossimJobQueue::findByName ( const ossimString name)
protected

Internal method that returns an iterator.

Parameters
namethe name of the job to search for
Returns
the iterator

Definition at line 231 of file ossimJobQueue.cpp.

References ossimString::empty(), and m_jobQueue.

Referenced by removeByName().

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 }
ossimJob::List m_jobQueue
bool empty() const
Definition: ossimString.h:411

◆ findByNameOrPointer()

ossimJob::List::iterator ossimJobQueue::findByNameOrPointer ( const std::shared_ptr< ossimJob job)
protected

Internal method that returns an iterator.

Parameters
jobit will find by the name or by the pointer
Returns
the iterator

Definition at line 253 of file ossimJobQueue.cpp.

References m_jobQueue, and n.

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 }
ossimJob::List m_jobQueue
os2<< "> n<< " > nendobj n

◆ findByPointer()

ossimJob::List::iterator ossimJobQueue::findByPointer ( const std::shared_ptr< ossimJob job)
protected

Internal method that returns an iterator.

Parameters
jobthe job to search for
Returns
the iterator

Definition at line 246 of file ossimJobQueue.cpp.

References m_jobQueue.

Referenced by add().

247 {
248  return std::find(m_jobQueue.begin(),
249  m_jobQueue.end(),
250  job);
251 }
ossimJob::List m_jobQueue

◆ getSharedFromThis() [1/2]

std::shared_ptr<ossimJobQueue> ossimJobQueue::getSharedFromThis ( )
inline

This is the safe way to create a std::shared_ptr for 'this'.

Calls the derived method shared_from_this

Definition at line 133 of file ossimJobQueue.h.

Referenced by add(), clear(), remove(), removeById(), removeByName(), and removeStoppedJobs().

133  {
134  return shared_from_this();
135  }

◆ getSharedFromThis() [2/2]

std::shared_ptr<const ossimJobQueue> ossimJobQueue::getSharedFromThis ( ) const
inline

This is the safe way to create a std::shared_ptr for 'this'.

Calls the derived method shared_from_this

Definition at line 141 of file ossimJobQueue.h.

141  {
142  return shared_from_this();
143  }

◆ hasJob()

bool ossimJobQueue::hasJob ( std::shared_ptr< ossimJob job)
protected

Internal method that determines if we have the job.

Parameters
jobthe job you wish to search for

Definition at line 279 of file ossimJobQueue.cpp.

References m_jobQueue.

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 }
ossimJob::List m_jobQueue

◆ isEmpty()

bool ossimJobQueue::isEmpty ( ) const
Returns
true if the queue is empty false otherwise

Definition at line 200 of file ossimJobQueue.cpp.

References m_jobQueue, and m_jobQueueMutex.

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 }
ossimJob::List m_jobQueue
std::mutex m_jobQueueMutex

◆ nextJob()

std::shared_ptr< ossimJob > ossimJobQueue::nextJob ( bool  blockIfEmptyFlag = true)
virtual

Will grab the next job on the list and will return the job or a null shared_ptr.

You can block the caller if the queueis empty forcing it to wait for more jobs to come onto the queue

Parameters
blockIfEmptyFlagIf true it will block the calling thread until more jobs appear on the queue. If false, it will return without blocking
Returns
a shared pointer to a job

Definition at line 161 of file ossimJobQueue.cpp.

References ossim::Block::block(), m_block, m_jobQueue, m_jobQueueMutex, and ossim::Block::set().

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 }
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
ossimJob::List m_jobQueue
void block()
Will block the calling thread based on the internal condition.
Definition: Block.cpp:33
std::mutex m_jobQueueMutex

◆ releaseBlock()

void ossimJobQueue::releaseBlock ( )
virtual

will release the block and have any blocked threads continue

Definition at line 196 of file ossimJobQueue.cpp.

References m_block, and ossim::Block::release().

197 {
198  m_block.release();
199 }
ossim::Block m_block
void release()
Releases the threads and will not return until all threads are released.
Definition: Block.cpp:66

◆ remove()

void ossimJobQueue::remove ( const std::shared_ptr< ossimJob Job)
virtual

Allows one to pass in a job pointer to remove.

Parameters
jobthe job you wish to remove from the list

Definition at line 87 of file ossimJobQueue.cpp.

References getSharedFromThis(), m_callback, m_jobQueue, and m_jobQueueMutex.

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 }
std::shared_ptr< ossimJobQueue > getSharedFromThis()
This is the safe way to create a std::shared_ptr for &#39;this&#39;.
ossimJob::List m_jobQueue
std::shared_ptr< Callback > m_callback
std::mutex m_jobQueueMutex

◆ removeById()

std::shared_ptr< ossimJob > ossimJobQueue::removeById ( const ossimString id)
virtual

Allows one to remove a job passing in it's id.

Parameters
idThe job id
Returns
a shared_ptr to the job. This will be nullptr if not found.

Definition at line 64 of file ossimJobQueue.cpp.

References findById(), getSharedFromThis(), m_block, m_callback, m_jobQueue, m_jobQueueMutex, and ossim::Block::set().

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 }
std::shared_ptr< ossimJobQueue > getSharedFromThis()
This is the safe way to create a std::shared_ptr for &#39;this&#39;.
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
ossimJob::List m_jobQueue
std::shared_ptr< Callback > m_callback
ossimJob::List::iterator findById(const ossimString &id)
Internal method that returns an iterator.
std::mutex m_jobQueueMutex

◆ removeByName()

std::shared_ptr< ossimJob > ossimJobQueue::removeByName ( const ossimString name)
virtual

Allows one to remove a job passing in it's name.

Parameters
nameThe job name
Returns
a shared_ptr to the job. This will be nullptr if not found.

Definition at line 41 of file ossimJobQueue.cpp.

References ossimString::empty(), findByName(), getSharedFromThis(), m_block, m_callback, m_jobQueue, m_jobQueueMutex, and ossim::Block::set().

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 }
std::shared_ptr< ossimJobQueue > getSharedFromThis()
This is the safe way to create a std::shared_ptr for &#39;this&#39;.
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
ossimJob::List::iterator findByName(const ossimString &name)
Internal method that returns an iterator.
ossimJob::List m_jobQueue
std::shared_ptr< Callback > m_callback
bool empty() const
Definition: ossimString.h:411
std::mutex m_jobQueueMutex

◆ removeStoppedJobs()

void ossimJobQueue::removeStoppedJobs ( )
virtual

Will remove any stopped jobs from the queue.

Definition at line 107 of file ossimJobQueue.cpp.

References getSharedFromThis(), m_callback, m_jobQueue, and m_jobQueueMutex.

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 }
std::shared_ptr< ossimJobQueue > getSharedFromThis()
This is the safe way to create a std::shared_ptr for &#39;this&#39;.
ossimJob::List m_jobQueue
std::shared_ptr< Callback > m_callback
std::list< std::shared_ptr< ossimJob > > List
Definition: ossimJob.h:152
std::mutex m_jobQueueMutex

◆ setCallback()

void ossimJobQueue::setCallback ( std::shared_ptr< Callback c)

Allows one to set the callback to the list.

Parameters
cshared_ptr to a callback

Definition at line 294 of file ossimJobQueue.cpp.

References m_callback, and m_jobQueueMutex.

295 {
296  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
297  m_callback = c;
298 }
std::shared_ptr< Callback > m_callback
std::mutex m_jobQueueMutex

◆ size()

ossim_uint32 ossimJobQueue::size ( )
Returns
the number of jobs on the queue

Definition at line 210 of file ossimJobQueue.cpp.

References m_jobQueue, and m_jobQueueMutex.

211 {
212  std::lock_guard<std::mutex> lock(m_jobQueueMutex);
213  return (ossim_uint32) m_jobQueue.size();
214 }
ossimJob::List m_jobQueue
unsigned int ossim_uint32
std::mutex m_jobQueueMutex

Member Data Documentation

◆ m_block

ossim::Block ossimJobQueue::m_block
protected

Definition at line 267 of file ossimJobQueue.h.

Referenced by add(), nextJob(), releaseBlock(), removeById(), and removeByName().

◆ m_callback

std::shared_ptr<Callback> ossimJobQueue::m_callback
protected

◆ m_jobQueue

ossimJob::List ossimJobQueue::m_jobQueue
protected

◆ m_jobQueueMutex

std::mutex ossimJobQueue::m_jobQueueMutex
mutableprotected

The documentation for this class was generated from the following files: