Menu

[r7]: / Plugin / jobqueue.h  Maximize  Restore  History

Download this file

173 lines (152 with data), 4.9 kB

  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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : jobqueue.h
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#ifndef __jobqueue__
#define __jobqueue__
#include <wx/thread.h>
#include <deque>
#include <vector>
class Job;
/**
* @class JobQueueWorker
* @author Eran
* @date 05/09/08
* @file jobqueue.h
* @brief a thread class used internally by JobQueue
* @sa JobQueue
*/
class JobQueueWorker : public wxThread
{
wxCriticalSection *m_cs;
std::deque<Job*> *m_queue;
public:
/// default ctor/dtor
JobQueueWorker(wxCriticalSection *cs, std::deque<Job*> *queue);
virtual ~JobQueueWorker();
private:
//declare this class as non copyable
JobQueueWorker(const JobQueueWorker& rhs);
JobQueueWorker& operator=(const JobQueueWorker& rhs);
public:
/**
* @brief stop the running thread. usually called before calling JobQueue::Release()
*/
void Stop();
/**
* @brief start the thread with given priority
* @param priority thread priority (defaulted to WXTHREAD_DEFAULT_PRIORITY)
*/
void Start(int priority = WXTHREAD_DEFAULT_PRIORITY);
/**
* @brief implementation of the wxThread pure virtual function Entry()
* @sa wxThread
*/
virtual void* Entry();
/**
* @brief return a job to process from the queue.
* @return Jon to process or NULL if queue is empty.
*/
Job *GetJob();
/**
* @brief the main code that processes the job
* @param job job to process
* @sa Job
*/
virtual void ProcessJob(Job *job);
};
/**
* @class JobQueue
* @author Eran
* @date 05/09/08
* @file jobqueue.h
* @brief this class provides a convenient way of handling background tasks using Job objects
*
* @code
* // somewhere in your application initialization
* // start the job queue with 5 workers and default thread prioritization
* m_jobQueue = new JobQueue();
* m_jobQueue->Start(5);
*
* // whenever you need to add new job to be processed
* // just add it
* m_jobQueue->PushJob( new MyJob() );
*
* // Shutdown code:
* m_jobQueue->Stop();
* // free the resources allocated by the JobQueue and empty the queue
* delete m_jobQueue;
* @endcode
*
*/
class JobQueue
{
wxCriticalSection m_cs;
std::deque<Job*> m_queue;
std::vector<JobQueueWorker*> m_threads;
public:
JobQueue();
virtual ~JobQueue();
/**
* @brief add job to the queue. all jobs must be constructed on the heap.
* note that the job will be freed when processing is done by the job queue
* @param job job to process allocated on the heap
*/
virtual void PushJob(Job *job);
/**
* @brief
* @param poolSize
* @param priority
*/
virtual void Start(size_t poolSize = 1, int priority = WXTHREAD_DEFAULT_PRIORITY);
/**
* @brief stop all workers threads
*/
virtual void Stop();
};
/**
* @class JobQueueSingleton
* @author Eran
* @date 05/09/08
* @file jobqueue.h
* @brief a wrapper around the JobQueue class that provides the functionality of singleton
*/
class JobQueueSingleton
{
private:
static JobQueue *ms_instance;
private:
JobQueueSingleton();
virtual ~JobQueueSingleton();
public:
/**
* @brief
* @return
*/
static JobQueue *Instance();
/**
* @brief
*/
static void Release();
};
#endif // __jobqueue__
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.