[Basic multithreading support clinton@unknownlamer.org**20081113080934 * 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 ] { addfile ./source/BotThreading.C hunk ./source/BotThreading.C 1 +// 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 (); +} addfile ./source/BotThreading.H hunk ./source/BotThreading.H 1 +// 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 (); +}; + + hunk ./source/Makefile.am 7 + BotThreading.C \ hunk ./source/Makefile.am 44 + BotThreading.H \ }