Basic multithreading support
authorclinton <clinton@unknownlamer.org>
Thu, 13 Nov 2008 08:09:34 +0000 (08:09 +0000)
committerclinton <clinton@unknownlamer.org>
Thu, 13 Nov 2008 08:09:34 +0000 (08:09 +0000)
* 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

source/BotThreading.C [new file with mode: 0644]
source/BotThreading.H [new file with mode: 0644]
source/Makefile.am

diff --git a/source/BotThreading.C b/source/BotThreading.C
new file mode 100644 (file)
index 0000000..d4e6ea0
--- /dev/null
@@ -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 <pthread.h>
+#include <iostream>
+
+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 (file)
index 0000000..088542a
--- /dev/null
@@ -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 <pthread.h>
+#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 ();
+};
+
+
index f0df95a..420a225 100644 (file)
@@ -4,6 +4,7 @@ bobotpp_SOURCES = BanEntry.C \
        Bot.C \
        BotConfig.C \
        BotInterp.C \
        Bot.C \
        BotConfig.C \
        BotInterp.C \
+       BotThreading.C \
        Channel.C \
        ChannelList.C \
        Commands.C \
        Channel.C \
        ChannelList.C \
        Commands.C \
@@ -40,6 +41,7 @@ bobotpp_SOURCES = BanEntry.C \
        Bot.H \
        BotConfig.H \
        BotInterp.H \
        Bot.H \
        BotConfig.H \
        BotInterp.H \
+       BotThreading.H \
        Channel.H \
        ChannelList.H \
        Commands.H \
        Channel.H \
        ChannelList.H \
        Commands.H \