/usr/include/ossim/parallel/ossimJobQueue.h is in libossim-dev 2.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | //**************************************************************************************************
// OSSIM -- Open Source Software Image Map
//
// LICENSE: See top level LICENSE.txt file.
//
//**************************************************************************************************
// $Id$
#ifndef ossimJobQueue_HEADER
#define ossimJobQueue_HEADER
#include <ossim/parallel/ossimJob.h>
#include <ossim/base/Block.h>
#include <mutex>
#include <memory>
/**
* 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
* @code
* #include <ossim/base/Thread.h>
* #include <ossim/parallel/ossimJob.h>
* #include <ossim/parallel/ossimJobQueue.h>
* #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";
* ossim::Thread::sleepInSeconds(2);
* 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>());
*
* ossim::Thread::sleepInSeconds(10);
* jobQueueThread->cancel();
* jobQueueThread->waitForCompletion();
* }
* @endcode
*/
class OSSIM_DLL ossimJobQueue : public std::enable_shared_from_this<ossimJobQueue>
{
public:
/**
* The callback allows one to attach and listen for certain things. In
* the ossimJobQueue it will notify just before adding a job, after adding a job
* and if a job is removed.
*/
class OSSIM_DLL Callback
{
public:
Callback(){}
/**
* Called just before a job is added
*
* @param q Is a shared_ptr to 'this' job queue
* @param job Is a shared ptr to the job we are adding
*/
virtual void adding(std::shared_ptr<ossimJobQueue> /*q*/,
std::shared_ptr<ossimJob> /*job*/){};
/**
* Called after a job is added to the queue
* @param q Is a shared_ptr to 'this' job queue
* @param job Is a shared ptr to the job we are added
*/
virtual void added(std::shared_ptr<ossimJobQueue> /*q*/,
std::shared_ptr<ossimJob> /*job*/){}
/**
* Called after a job is removed from the queue
* @param q Is a shared_ptr to 'this' job queue
* @param job Is a shared ptr to the job we have removed
*/
virtual void removed(std::shared_ptr<ossimJobQueue> /*q*/,
std::shared_ptr<ossimJob>/*job*/){}
};
/**
* Default constructor
*/
ossimJobQueue();
/**
* This is the safe way to create a std::shared_ptr for 'this'. Calls the derived
* method shared_from_this
*/
std::shared_ptr<ossimJobQueue> getSharedFromThis(){
return shared_from_this();
}
/**
* This is the safe way to create a std::shared_ptr for 'this'. Calls the derived
* method shared_from_this
*/
std::shared_ptr<const ossimJobQueue> getSharedFromThis()const{
return shared_from_this();
}
/**
* 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
*
* @param job The job to add to the queue.
* @param guaranteeUniqueFlag if set to true will force a find to make sure the job
* does not already exist
*/
virtual void add(std::shared_ptr<ossimJob> job, bool guaranteeUniqueFlag=true);
/**
* Allows one to remove a job passing in it's name.
*
* @param name The job name
* @return a shared_ptr to the job. This will be nullptr if not found.
*/
virtual std::shared_ptr<ossimJob> removeByName(const ossimString& name);
/**
* Allows one to remove a job passing in it's id.
*
* @param id The job id
* @return a shared_ptr to the job. This will be nullptr if not found.
*/
virtual std::shared_ptr<ossimJob> removeById(const ossimString& id);
/**
* Allows one to pass in a job pointer to remove
*
* @param job the job you wish to remove from the list
*/
virtual void remove(const std::shared_ptr<ossimJob> Job);
/**
* Will remove any stopped jobs from the queue
*/
virtual void removeStoppedJobs();
/**
* Will clear the queue
*/
virtual void clear();
/**
* 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
*
* @param blockIfEmptyFlag If true it will block the calling thread until more jobs appear
* on the queue. If false, it will return without blocking
* @return a shared pointer to a job
*/
virtual std::shared_ptr<ossimJob> nextJob(bool blockIfEmptyFlag=true);
/**
* will release the block and have any blocked threads continue
*/
virtual void releaseBlock();
/**
* @return true if the queue is empty false otherwise
*/
bool isEmpty()const;
/**
* @return the number of jobs on the queue
*/
ossim_uint32 size();
/**
* Allows one to set the callback to the list
*
* @param c shared_ptr to a callback
*/
void setCallback(std::shared_ptr<Callback> c);
/**
* @return the callback
*/
std::shared_ptr<Callback> callback();
protected:
/**
* Internal method that returns an iterator
*
* @param the id of the job to search for
* @return the iterator
*/
ossimJob::List::iterator findById(const ossimString& id);
/**
* Internal method that returns an iterator
*
* @param name the name of the job to search for
* @return the iterator
*/
ossimJob::List::iterator findByName(const ossimString& name);
/**
* Internal method that returns an iterator
*
* @param job the job to search for
* @return the iterator
*/
ossimJob::List::iterator findByPointer(const std::shared_ptr<ossimJob> job);
/**
* Internal method that returns an iterator
*
* @param job it will find by the name or by the pointer
* @return the iterator
*/
ossimJob::List::iterator findByNameOrPointer(const std::shared_ptr<ossimJob> job);
/**
* Internal method that determines if we have the job
*
* @param job the job you wish to search for
*/
bool hasJob(std::shared_ptr<ossimJob> job);
mutable std::mutex m_jobQueueMutex;
ossim::Block m_block;
ossimJob::List m_jobQueue;
std::shared_ptr<Callback> m_callback;
};
#endif
|