summaryrefslogtreecommitdiffstats
path: root/src/threadengines/threadengine_pthread.cpp
diff options
context:
space:
mode:
authorGravatar Daniel De Graaf2010-08-25 00:16:06 -0400
committerGravatar Daniel De Graaf2010-08-25 00:16:06 -0400
commitfb4ad3e99e31547e29d10e53a45a2e3b5af08c96 (patch)
tree43f77a3dc094ce56eb94ed5e5e8222c1364f023d /src/threadengines/threadengine_pthread.cpp
parentUse mysql_real_escape_string by postponing substitution until query time (diff)
downloadinspircd++-fb4ad3e99e31547e29d10e53a45a2e3b5af08c96.tar.gz
inspircd++-fb4ad3e99e31547e29d10e53a45a2e3b5af08c96.tar.bz2
inspircd++-fb4ad3e99e31547e29d10e53a45a2e3b5af08c96.zip
Add thread terminator support
Diffstat (limited to 'src/threadengines/threadengine_pthread.cpp')
-rw-r--r--src/threadengines/threadengine_pthread.cpp38
1 files changed, 25 insertions, 13 deletions
diff --git a/src/threadengines/threadengine_pthread.cpp b/src/threadengines/threadengine_pthread.cpp
index a4d80b993..b7357680f 100644
--- a/src/threadengines/threadengine_pthread.cpp
+++ b/src/threadengines/threadengine_pthread.cpp
@@ -145,17 +145,24 @@ ThreadEngine::Runner::~Runner()
void ThreadEngine::Submit(Job* job)
{
Mutex::Lock lock(job_lock);
+ // TODO launch more threads if we have a queue and more are allowed
+ // TODO clean up threads at garbage collection time (all but one)
if (threads.empty())
- {
- Runner* thread = new Runner(this);
- threads.push_back(thread);
- }
+ threads.push_back(new Runner(this));
if (result_ss == NULL)
result_ss = new ThreadSignalSocket();
submit_q.push_back(job);
submit_s.signal_one();
}
+static class TerminateJob : public Job
+{
+ public:
+ TerminateJob() : Job(NULL) { cancel(); }
+ void run() {}
+ void finish() {}
+} terminator;
+
void ThreadEngine::Runner::main_loop()
{
te->job_lock.lock();
@@ -166,18 +173,22 @@ void ThreadEngine::Runner::main_loop()
current = te->submit_q.front();
te->submit_q.pop_front();
- te->job_lock.unlock();
-
- try
- {
- current->run();
- }
- catch (...)
+
+ if (!current->IsCancelled())
{
+ te->job_lock.unlock();
+ try
+ {
+ current->run();
+ }
+ catch (...)
+ {
+ }
+ te->job_lock.lock();
}
-
- te->job_lock.lock();
te->result_q.push_back(current);
+ if (current == &terminator)
+ break;
current = NULL;
te->result_sc.signal_one();
@@ -185,6 +196,7 @@ void ThreadEngine::Runner::main_loop()
te->result_ss->Notify();
}
te->job_lock.unlock();
+ // current == terminator, we die
}
void ThreadEngine::result_loop()