/usr/include/codeblocks/backgroundthread.h is in codeblocks-dev 16.01+dfsg-2+b1.
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 | /*
* This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
* http://www.gnu.org/licenses/lgpl-3.0.html
*/
#ifndef BACKGROUNDTHREAD_H
#define BACKGROUNDTHREAD_H
#include "safedelete.h"
#undef new
#include <deque>
#include <list>
#include <algorithm>
#include <wx/timer.h> // wxMilliSleep
#include "wx/thread.h"
#include "manager.h"
#include "blockallocated.h"
/*
* BackgroundThread is a lightweight single background worker thread implementation for situations in which
* you simply want to do one or several things in another thread, and using a thread pool is overkill.
* Also, several BackgroundThreads can be used in situations where a thread pool is unsuitable by design. For example,
* you can use two BackgroundThreads to asynchronously read a list of files from disk one at at time and download another list
* of files from the internet without hogging either the hard disk or the network layer with more than one concurrent access.
*
* BackgroundThread can be configured to own the job objects (will delete them after running) or not. It can also own
* the semaphore and queue, or use a shared context.
*
* BackgroundThreadPool is a low overhead thread pool implementation around BackgroundThread.
*/
class AbstractJob
{
public:
AbstractJob(){};
virtual ~AbstractJob(){};
virtual void operator()() = 0;
};
class JobQueue : public std::deque<AbstractJob*>
{
wxCriticalSection c;
public:
void Push(AbstractJob *j)
{
wxCriticalSectionLocker l(c);
push_back(j);
};
AbstractJob* Pop()
{
wxCriticalSectionLocker l(c);
AbstractJob* j = front();
pop_front();
return j;
};
};
class BackgroundThread : public wxThread
{
JobQueue *queue;
wxSemaphore *semaphore;
bool die;
const bool ownsQueue;
const bool ownsSemaphore;
const bool ownsJobs;
public:
BackgroundThread(JobQueue *q, wxSemaphore *s, const bool owns_jobs = true)
: queue(q), semaphore(s), die(false), ownsQueue(false), ownsSemaphore(false), ownsJobs(owns_jobs)
{
Create();
Run();
};
BackgroundThread(wxSemaphore *s, const bool owns_jobs = true)
: queue(new JobQueue), semaphore(s), die(false), ownsQueue(true), ownsSemaphore(false), ownsJobs(owns_jobs)
{
Create();
Run();
};
BackgroundThread(JobQueue *q, const bool owns_jobs = true)
: queue(q), semaphore(new wxSemaphore), die(false), ownsQueue(false), ownsSemaphore(true), ownsJobs(owns_jobs)
{
Create();
Run();
};
BackgroundThread(const bool owns_jobs = true)
: queue(new JobQueue), semaphore(new wxSemaphore), die(false), ownsQueue(true), ownsSemaphore(true), ownsJobs(owns_jobs)
{
Create();
Run();
};
~BackgroundThread()
{
if (ownsSemaphore)
::Delete(semaphore);
if (ownsQueue)
::Delete(queue);
};
void Queue(AbstractJob* j)
{
queue->Push(j);
semaphore->Post();
};
void Die()
{
die = true;
semaphore->Post();
wxMilliSleep(0);
};
void MarkDying() // Need this for threadpool. Die() alone does not work in shared context (for obvious reason).
{
die = true;
};
/*
* This function is inherently unsafe!
* Also, if used on a thread belonging to a pool, it will not do what you think it does!
* Do not use this function unless you are sure you really know what you are doing.
*/
void MurderDeathKill()
{
Die();
wxMilliSleep(0);
wxMilliSleep(0);
if (this && IsRunning())
Kill();
};
ExitCode Entry()
{
AbstractJob* job;
for (;;)
{
semaphore->Wait();
if (die)
break;
job = queue->Pop();
if ( job )
(*job)();
if (ownsJobs)
delete job;
}
return nullptr;
};
};
struct Agony { inline void operator()(BackgroundThread* t){t->MarkDying();}; };
struct Death { inline void operator()(BackgroundThread* t){t->Die();}; };
class BackgroundThreadPool
{
typedef std::list<BackgroundThread*> ThreadList;
JobQueue queue;
wxSemaphore semaphore;
ThreadList threadList;
public:
BackgroundThreadPool(size_t num_threads = 4)
{
for (unsigned int i = 0; i < num_threads; ++i)
AddThread(new BackgroundThread(&queue, &semaphore));
};
~BackgroundThreadPool()
{
for_each(threadList.begin(), threadList.end(), Agony());
for_each(threadList.begin(), threadList.end(), Death());
Delete(queue);
wxMilliSleep(0);
};
void AddThread(BackgroundThread * t)
{
threadList.push_back(t);
};
void Queue(AbstractJob* j)
{
queue.Push(j);
semaphore.Post();
};
};
#endif
|