OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimJobThreadQueue.cpp
Go to the documentation of this file.
2 #include <cstddef> // for std::nullptr
3 ossimJobThreadQueue::ossimJobThreadQueue(std::shared_ptr<ossimJobQueue> jqueue)
4 :m_doneFlag(false)
5 {
6  setJobQueue(jqueue);
7 }
8 void ossimJobThreadQueue::setJobQueue(std::shared_ptr<ossimJobQueue> jqueue)
9 {
10  std::lock_guard<std::mutex> lock(m_threadMutex);
11 
12  if (m_jobQueue == jqueue) return;
13 
14  pause();
15  while(isRunning()&&!isPaused())
16  {
17  m_jobQueue->releaseBlock();
18  }
19  m_jobQueue = jqueue;
20  resume();
21 
23 }
24 
25 std::shared_ptr<ossimJobQueue> ossimJobThreadQueue::getJobQueue()
26 {
27  std::lock_guard<std::mutex> lock(m_threadMutex);
28  return m_jobQueue;
29 }
30 
31 const std::shared_ptr<ossimJobQueue> ossimJobThreadQueue::getJobQueue() const
32 {
33  std::lock_guard<std::mutex> lock(m_threadMutex);
34  return m_jobQueue;
35 }
36 
37 std::shared_ptr<ossimJob> ossimJobThreadQueue::currentJob()
38 {
39  std::lock_guard<std::mutex> lock(m_threadMutex);
40  return m_currentJob;
41 }
42 
44 {
45  std::lock_guard<std::mutex> lock(m_threadMutex);
46  if(m_currentJob)
47  {
48  m_currentJob->cancel();
49  }
50 }
52 {
53  std::lock_guard<std::mutex> lock(m_threadMutex);
54  return (m_jobQueue!=nullptr);
55 }
56 
58 {
59  bool firstTime = true;
60  bool validQueue = true;
61  std::shared_ptr<ossimJob> job;
62  do
63  {
64  interrupt();
65  // osg::notify(osg::NOTICE)<<"In thread loop "<<this<<std::endl;
66  validQueue = isValidQueue();
67  job = nextJob();
68  if (job&&!m_doneFlag)
69  {
70  {
71  std::lock_guard<std::mutex> lock(m_threadMutex);
72  m_currentJob = job;
73  }
74 
75  // if the job is ready to execute
76  if(job->isReady())
77  {
78  job->start();
79  }
80  {
81  std::lock_guard<std::mutex> lock(m_threadMutex);
82  m_currentJob = 0;
83  }
84  job.reset();
85  }
86 
87  if (firstTime)
88  {
90  firstTime = false;
91  }
92  } while (!m_doneFlag&&validQueue);
93 
94  {
95  std::lock_guard<std::mutex> lock(m_threadMutex);
96  m_currentJob = 0;
97  }
98  if(job&&m_doneFlag&&job->isReady())
99  {
100  job->cancel();
101  }
102  job = 0;
103 }
104 
106 {
107  m_threadMutex.lock();
108  if (m_doneFlag==done)
109  {
110  m_threadMutex.unlock();
111  return;
112  }
113  m_doneFlag = done;
114  m_threadMutex.unlock();
115  if(done)
116  {
117  {
118  std::lock_guard<std::mutex> lock(m_threadMutex);
119  if (m_currentJob)
120  m_currentJob->release();
121  }
122 
123  if (m_jobQueue)
124  m_jobQueue->releaseBlock();
125  }
126 }
127 
129 {
130  std::lock_guard<std::mutex> lock(m_threadMutex);
131  return m_doneFlag;
132 }
133 
135 {
136  std::lock_guard<std::mutex> lock(m_threadMutex);
137  return (m_currentJob!=nullptr);
138 }
139 
141 {
142 
143  if( isRunning() )
144  {
145  {
146  std::lock_guard<std::mutex> lock(m_threadMutex);
147  m_doneFlag = true;
148  if (m_currentJob)
149  {
150  m_currentJob->cancel();
151  }
152 
153  if (m_jobQueue)
154  {
155  m_jobQueue->releaseBlock();
156  }
157  }
158 
159  // then wait for the the thread to stop running.
160  while(isRunning())
161  {
162 #if 1
163  {
164  std::lock_guard<std::mutex> lock(m_threadMutex);
165 
166  if (m_jobQueue)
167  {
168  m_jobQueue->releaseBlock();
169  }
170  }
171 #endif
173  }
174  }
175 }
176 
178 {
179  std::lock_guard<std::mutex> lock(m_threadMutex);
180  return m_jobQueue->isEmpty();
181 }
182 
184 {
185  cancel();
186 }
187 
189 {
190  if(m_jobQueue)
191  {
192  if(!isRunning())
193  {
194  start();
195  }
196  }
197 }
198 
200 {
201  bool result = false;
202  {
203  std::lock_guard<std::mutex> lock(m_threadMutex);
204  result = (!m_jobQueue->isEmpty()||m_currentJob);
205  }
206 
207  return result;
208 }
209 
210 std::shared_ptr<ossimJob> ossimJobThreadQueue::nextJob()
211 {
212  std::shared_ptr<ossimJob> job;
213  m_threadMutex.lock();
214  std::shared_ptr<ossimJobQueue> jobQueue = m_jobQueue;
215  bool checkIfValid = !m_doneFlag&&jobQueue;
216  m_threadMutex.unlock();
217  if(checkIfValid)
218  {
219  job = jobQueue->nextJob(true);
220  }
221  return job;
222 }
void cancelCurrentJob()
Will cancel the current job.
std::shared_ptr< ossimJobQueue > m_jobQueue
void setJobQueue(std::shared_ptr< ossimJobQueue > jqueue)
Sets the shared queue that this thread will be pulling jobs from.
virtual void interrupt()
This is the interrupt interface and will cause an internal exception that is caught by...
Definition: Thread.cpp:103
void start()
Will actually start the thread and will call the.
Definition: Thread.cpp:22
virtual std::shared_ptr< ossimJob > nextJob()
Will return the next job on the queue.
std::shared_ptr< ossimJobQueue > getJobQueue()
virtual void run()
This is method is overriden from the base thread class and is the main entry point of the thread...
std::shared_ptr< ossimJob > currentJob()
virtual void cancel()
Cancels the thread.
bool isRunning() const
Definition: Thread.h:100
std::shared_ptr< ossimJob > m_currentJob
ossimJobThreadQueue(std::shared_ptr< ossimJobQueue > jqueue=0)
constructor that allows one to instantiat the thread with a shared job queue.
void setDone(bool done)
Sets the done flag.
void resume()
This will resume a blocked thread.
Definition: Thread.cpp:63
void startThreadForQueue()
Internal method.
static void yieldCurrentThread()
Will yield the current thread.
Definition: Thread.cpp:98
bool isPaused() const
Definition: Thread.cpp:68
virtual ~ossimJobThreadQueue()
destructor.
void pause()
Enables the thread to be paused.
Definition: Thread.cpp:58