From: clinton Date: Thu, 13 Nov 2008 08:09:34 +0000 (+0000) Subject: Basic multithreading support X-Git-Tag: release-2.3.1~49 X-Git-Url: http://git.hcoop.net/clinton/bobotpp.git/commitdiff_plain/eb42e7278a8db3d34f4dd792cd3a95a82a72d684 Basic multithreading support * BotMutex class provides a mutex that turns into a noop when threading is disabled * BotLock provides an automatic lock/unlock pairing within a block * Once everything has been made threadsafe using multiple Guile threads that access the bot will not corrupt any internal data structures --- diff --git a/source/BotThreading.C b/source/BotThreading.C new file mode 100644 index 0000000..d4e6ea0 --- /dev/null +++ b/source/BotThreading.C @@ -0,0 +1,103 @@ +// BotThreading.C -*- C++ -*- +// Copyright (c) 2008 Clinton Ebadi + +// 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 +// any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +// 02110-1301, USA. + +#include "BotThreading.H" + +#ifdef MULTITHREAD + +#include +#include + +BotMutex::BotMutex () +{ + pthread_t self = pthread_self (); + std::cerr << "Mutex Init..." + << " Mutex: " << &mutex + << " Thread: " << &self + << std::endl; + pthread_mutex_init (&mutex, 0); +} + +BotMutex::~BotMutex () +{ + pthread_t self = pthread_self (); + std::cerr << "Mutex Destroy..." + << " Mutex: " << &mutex + << " Thread: " << &self + << std::endl; + pthread_mutex_destroy (&mutex); +} + +void BotMutex::lock () +{ + pthread_t self = pthread_self (); + std::cerr << "< Mutex Lock..." + << " Mutex: " << &mutex + << " Thread: " << &self + << std::endl; + pthread_mutex_lock (&mutex); +} + +void BotMutex::unlock () +{ + pthread_t self = pthread_self (); + std::cerr << "> Mutex Unlock..." + << " Mutex: " << &mutex + << " Thread: " << &self + << std::endl; + pthread_mutex_unlock (&mutex); +} + +#else // non threaded noops + +BotMutex::BotMutex () +{ } + +BotMutex::~BotMutex () +{ } + +void BotMutex::lock () +{ } + +void BotMutex::unlock () +{ } + +#endif // MULTITHREAD + +BotLock::BotLock (BotMutex & m) + : mutex(m) +{ + pthread_t self = pthread_self (); + std::cerr << "Lock Init..." + << " Lock: " << this + << " Mutex: " << &mutex + << " Thread: " << &self + << std::endl; + mutex.lock (); +} + +BotLock::~BotLock () +{ + pthread_t self = pthread_self (); + std::cerr << "Lock Destroy..." + << " Lock: " << this + << " Mutex: " << &mutex + << " Thread: " << &self + << std::endl; + mutex.unlock (); +} diff --git a/source/BotThreading.H b/source/BotThreading.H new file mode 100644 index 0000000..088542a --- /dev/null +++ b/source/BotThreading.H @@ -0,0 +1,57 @@ +// BotThreading.H -*- C++ -*- +// Copyright (c) 2008 Clinton Ebadi + +// 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 +// any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +// 02110-1301, USA. + +// Basic Mutex and automatic lock interface that falls back to noops +// (wasting one byte of space for each fake mutex...oh well) in the +// absence of threading. This is intended to be used to ensure that +// multiple Guile threads can call the bot without corrupting its data +// structures without forcing pthreads or locking overhead on the +// usual single threaded standalone bot + +#include "config.h" + +#ifdef MULTITHREAD +#include +#endif + +class BotMutex +{ +#ifdef MULTITHREAD + pthread_mutex_t mutex; +#endif + +public: + BotMutex (); + ~BotMutex (); + + void lock (); + void unlock (); +}; + +// Interface to automatically acquire and release a BotMutex within a +// block +class BotLock +{ + BotMutex& mutex; + +public: + BotLock (BotMutex & m); + ~BotLock (); +}; + + diff --git a/source/Makefile.am b/source/Makefile.am index f0df95a..420a225 100644 --- a/source/Makefile.am +++ b/source/Makefile.am @@ -4,6 +4,7 @@ bobotpp_SOURCES = BanEntry.C \ Bot.C \ BotConfig.C \ BotInterp.C \ + BotThreading.C \ Channel.C \ ChannelList.C \ Commands.C \ @@ -40,6 +41,7 @@ bobotpp_SOURCES = BanEntry.C \ Bot.H \ BotConfig.H \ BotInterp.H \ + BotThreading.H \ Channel.H \ ChannelList.H \ Commands.H \