summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2011-09-05 18:44:43 -0400
committerAdam <Adam@anope.org>2011-09-10 02:06:31 -0400
commit63cb8ca24c0d4003343340bb8413b7f84de1a6a3 (patch)
treed71f0cead730065dd8509e284d66b6767e7ebd25 /src
parentdc5d1fa21c20000b77bf713585333c79121a8df0 (diff)
Moved signal/thread/mode checking to use signal pipes
Diffstat (limited to 'src')
-rw-r--r--src/init.cpp13
-rw-r--r--src/mail.cpp6
-rw-r--r--src/main.cpp26
-rw-r--r--src/modes.cpp16
-rw-r--r--src/threadengine.cpp33
-rw-r--r--src/threadengines/threadengine_pthread.cpp47
-rw-r--r--src/threadengines/threadengine_win32.cpp25
7 files changed, 63 insertions, 103 deletions
diff --git a/src/init.cpp b/src/init.cpp
index 4cff3fc91..beb34aafb 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -182,7 +182,7 @@ class SignalReload : public Signal
public:
SignalReload(int sig) : Signal(sig) { }
- void OnSignal()
+ void OnNotify()
{
Log() << "Received SIGHUP: Saving databases & rehashing configuration";
@@ -208,7 +208,7 @@ class SignalExit : public Signal
public:
SignalExit(int sig) : Signal(sig) { }
- void OnSignal()
+ void OnNotify()
{
#ifndef _WIN32
Log() << "Received " << strsignal(this->signal) << " signal (" << this->signal << "), exiting.";
@@ -228,7 +228,7 @@ class SignalNothing : public Signal
public:
SignalNothing(int sig) : Signal(sig) { }
- void OnSignal() { }
+ void OnNotify() { }
};
void Init(int ac, char **av)
@@ -397,9 +397,10 @@ void Init(int ac, char **av)
/* Announce ourselves to the logfile. */
Log() << "Anope " << Anope::Version() << " starting up" << (debug || readonly ? " (options:" : "") << (debug ? " debug" : "") << (readonly ? " readonly" : "") << (debug || readonly ? ")" : "");
- static SignalReload sig_hup(SIGHUP);
- static SignalExit sig_term(SIGTERM), sig_int(SIGINT);
- static SignalNothing sig_pipe(SIGPIPE);
+ new SignalReload(SIGHUP);
+ new SignalExit(SIGTERM);
+ new SignalExit(SIGINT);
+ new SignalNothing(SIGPIPE);
/* Initialize multi-language support */
Log(LOG_DEBUG) << "Loading Languages...";
diff --git a/src/mail.cpp b/src/mail.cpp
index edd892bd0..dcf9dea5e 100644
--- a/src/mail.cpp
+++ b/src/mail.cpp
@@ -51,7 +51,8 @@ bool Mail(User *u, NickCore *nc, BotInfo *service, const Anope::string &subject,
else
{
u->lastmail = nc->lastmail = Anope::CurTime;
- threadEngine.Start(new MailThread(nc->display, nc->email, subject, message));
+ Thread *t = new MailThread(nc->display, nc->email, subject, message);
+ t->Start();
return true;
}
@@ -64,7 +65,8 @@ bool Mail(NickCore *nc, const Anope::string &subject, const Anope::string &messa
return false;
nc->lastmail = Anope::CurTime;
- threadEngine.Start(new MailThread(nc->display, nc->email, subject, message));
+ Thread *t = new MailThread(nc->display, nc->email, subject, message);
+ t->Start();
return true;
}
diff --git a/src/main.cpp b/src/main.cpp
index 69a5fe38f..aab9edce5 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -207,21 +207,10 @@ void Signal::SignalHandler(int signal)
{
for (unsigned i = 0, j = SignalHandlers.size(); i < j; ++i)
if (SignalHandlers[i]->signal == signal)
- SignalHandlers[i]->called = true;
+ SignalHandlers[i]->Notify();
}
-void Signal::Process()
-{
- for (unsigned i = 0, j = SignalHandlers.size(); i < j; ++i)
- if (SignalHandlers[i]->called == true)
- {
- Signal *s = SignalHandlers[i];
- s->called = false;
- s->OnSignal();
- }
-}
-
-Signal::Signal(int s) : called(false), signal(s)
+Signal::Signal(int s) : Pipe(), signal(s)
{
memset(&this->old, 0, sizeof(this->old));
@@ -244,7 +233,6 @@ Signal::~Signal()
sigaction(this->signal, &this->old, NULL);
}
-
/*************************************************************************/
/** The following comes from InspIRCd to get the full path of the Anope executable
@@ -355,9 +343,6 @@ int main(int ac, char **av, char **envp)
{
Log(LOG_DEBUG_2) << "Top of main loop";
- /* Process signals */
- Signal::Process();
-
/* Process timers */
if (Anope::CurTime - last_check >= Config->TimeoutCheck)
{
@@ -365,13 +350,6 @@ int main(int ac, char **av, char **envp)
last_check = Anope::CurTime;
}
- /* Free up any finished threads */
- threadEngine.Process();
-
- /* Process any modes that need to be (un)set */
- if (Me->IsSynced())
- ModeManager::ProcessModes();
-
/* Process the socket engine */
SocketEngine::Process();
}
diff --git a/src/modes.cpp b/src/modes.cpp
index d51622e22..c3e52e036 100644
--- a/src/modes.cpp
+++ b/src/modes.cpp
@@ -9,6 +9,7 @@
#include "services.h"
#include "modules.h"
+ModeManager::ModePipe *ModeManager::mpipe = NULL;
/* List of pairs of user/channels and their stacker info */
std::list<std::pair<Base *, StackerInfo *> > ModeManager::StackerObjects;
@@ -345,6 +346,16 @@ void StackerInfo::AddMode(Mode *mode, bool Set, const Anope::string &Param)
list->push_back(std::make_pair(mode, Param));
}
+/** Called when there are modes to be set
+ */
+void ModeManager::ModePipe::OnNotify()
+{
+ if (!Me || !Me->IsSynced())
+ return;
+
+ ModeManager::ProcessModes();
+}
+
/** Get the stacker info for an item, if one doesnt exist it is created
* @param Item The user/channel etc
* @return The stacker info
@@ -360,6 +371,11 @@ StackerInfo *ModeManager::GetInfo(Base *Item)
StackerInfo *s = new StackerInfo();
StackerObjects.push_back(std::make_pair(Item, s));
+
+ if (mpipe == NULL)
+ mpipe = new ModePipe();
+ mpipe->Notify();
+
return s;
}
diff --git a/src/threadengine.cpp b/src/threadengine.cpp
index c2aa3da20..e11b065ea 100644
--- a/src/threadengine.cpp
+++ b/src/threadengine.cpp
@@ -1,46 +1,22 @@
#include "services.h"
-ThreadEngine threadEngine;
-
-/** Check for finished threads
- */
-void ThreadEngine::Process()
-{
- for (unsigned i = this->threads.size(); i > 0; --i)
- {
- Thread *t = this->threads[i - 1];
-
- if (t->GetExitState())
- {
- t->Join();
- delete t;
- }
- }
-}
-
/** Threads constructor
*/
Thread::Thread() : exit(false)
{
- threadEngine.threads.push_back(this);
}
/** Threads destructor
*/
Thread::~Thread()
{
- std::vector<Thread *>::iterator it = std::find(threadEngine.threads.begin(), threadEngine.threads.end(), this);
-
- if (it != threadEngine.threads.end())
- {
- threadEngine.threads.erase(it);
- }
}
/** Sets the exit state as true informing the thread we want it to shut down
*/
void Thread::SetExitState()
{
+ this->Notify();
exit = true;
}
@@ -52,8 +28,11 @@ bool Thread::GetExitState() const
return exit;
}
-/** Called to run the thread, should be overloaded
+/** Called when this thread should be joined to
*/
-void Thread::Run()
+void Thread::OnNotify()
{
+ this->Join();
+ this->SetFlag(SF_DEAD);
}
+
diff --git a/src/threadengines/threadengine_pthread.cpp b/src/threadengines/threadengine_pthread.cpp
index f28c43846..987b8bc61 100644
--- a/src/threadengines/threadengine_pthread.cpp
+++ b/src/threadengines/threadengine_pthread.cpp
@@ -1,7 +1,22 @@
#include "services.h"
-/* Threadengine attributes used by this thread engine */
-static pthread_attr_t threadengine_attr;
+static inline pthread_attr_t *get_engine_attr()
+{
+ /* Threadengine attributes used by this thread engine */
+ static pthread_attr_t attr;
+ static bool inited = false;
+
+ if (inited == false)
+ {
+ if (pthread_attr_init(&attr))
+ throw CoreException("Error calling pthread_attr_init");
+ if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE))
+ throw CoreException("Unable to mark threads as joinable");
+ inited = true;
+ }
+
+ return &attr;
+}
/** Entry point used for the threads
* @param parameter A Thread* cast to a void*
@@ -14,23 +29,6 @@ static void *entry_point(void *parameter)
pthread_exit(0);
}
-/** Threadengines constructor
- */
-ThreadEngine::ThreadEngine()
-{
- if (pthread_attr_init(&threadengine_attr))
- throw CoreException("ThreadEngine: Error calling pthread_attr_init");
- if (pthread_attr_setdetachstate(&threadengine_attr, PTHREAD_CREATE_JOINABLE))
- throw CoreException("ThreadEngine: Unable to mark threads as joinable");
-}
-
-/** Threadengines destructor
- */
-ThreadEngine::~ThreadEngine()
-{
- pthread_attr_destroy(&threadengine_attr);
-}
-
/** Join to the thread, sets the exit state to true
*/
void Thread::Join()
@@ -47,15 +45,14 @@ void Thread::Exit()
pthread_exit(0);
}
-/** Start a new thread
- * @param thread A pointer to a newley allocated thread
+/** Launch the thread
*/
-void ThreadEngine::Start(Thread *thread)
+void Thread::Start()
{
- if (pthread_create(&thread->Handle, &threadengine_attr, entry_point, thread))
+ if (pthread_create(&this->Handle, get_engine_attr(), entry_point, this))
{
- delete thread;
- throw CoreException(Anope::string("Unable to create thread: ") + Anope::LastError());
+ this->SetFlag(SF_DEAD);
+ throw CoreException("Unable to create thread: " + Anope::LastError());
}
}
diff --git a/src/threadengines/threadengine_win32.cpp b/src/threadengines/threadengine_win32.cpp
index adefab800..5b6739c52 100644
--- a/src/threadengines/threadengine_win32.cpp
+++ b/src/threadengines/threadengine_win32.cpp
@@ -11,18 +11,6 @@ static DWORD WINAPI entry_point(void *parameter)
return 0;
}
-/** Threadengines constructor
- */
-ThreadEngine::ThreadEngine()
-{
-}
-
-/** Threadengines destructor
- */
-ThreadEngine::~ThreadEngine()
-{
-}
-
/** Join to the thread, sets the exit state to true
*/
void Thread::Join()
@@ -39,17 +27,16 @@ void Thread::Exit()
ExitThread(0);
}
-/** Start a new thread
- * @param thread A pointer to a newley allocated thread
+/** Launch the thread
*/
-void ThreadEngine::Start(Thread *thread)
+void Thread::Start()
{
- thread->Handle = CreateThread(NULL, 0, entry_point, thread, 0, NULL);
+ this->Handle = CreateThread(NULL, 0, entry_point, this, 0, NULL);
- if (!thread->Handle)
+ if (!this->Handle)
{
- delete thread;
- throw CoreException(Anope::string("Unable to create thread: ") + Anope::LastError());
+ this->SetFlag(SF_DEAD);
+ throw CoreException("Unable to create thread: " + Anope::LastError());
}
}