Make Scheme hooks and timers threadsafe
[clinton/bobotpp.git] / source / BotInterp.H
index 959a172..4929058 100644 (file)
@@ -1,6 +1,6 @@
 // BotInterp.H  -*- C++ -*-
 // Copyright (c) 1998 Etienne BERNARD
-// Copyright (C) 2002 Clinton Ebadi
+// Copyright (C) 2002,2005,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
@@ -14,7 +14,8 @@
 
 // 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.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+// 02110-1301, USA.
 
 #ifndef BOTINTERP_H
 #define BOTINTERP_H
 #ifdef USESCRIPTS
 
 #include <ctime>
+#include <map>
+#include <list>
+#include <functional>
+
 #include <libguile.h>
+
+#include "BotThreading.H"
 #include "String.H"
+#include "Utils.H"
 
 class Bot;
 
@@ -81,6 +89,7 @@ struct Hook {
     PUBLIC_NOTICE, RAW, TIMER, TOPIC,
     // send hooks
     SEND_ACTION, SEND_CTCP, SEND_PUBLIC, SEND_MESSAGE,
+    SEND_WHO, SEND_WHOIS,
     // DCC hooks
     DCC_CHAT_BEGIN, DCC_CHAT_END, DCC_CHAT_MESSAGE
   };  
@@ -93,14 +102,22 @@ struct Timer {
 
   Timer(int c, std::time_t t, SCM f)
     : count(c), when(t), function(f) { }
+
+  bool operator< (const Timer & other) const
+  { return when < other.when; }
 };
 
 class BotInterp {
+  typedef std::list<Timer *> TimerList;
+  
   Bot * bot;
   SCM logPort;
   std::map<int, std::list<Hook *>, std::less<int> > hooksMap;
-  std::list<Timer *> timersList;
+  TimerList timers;
+  Utils::IndirectPred<Timer, std::less<Timer> > timer_sort_p;
   int counter;
+  BotMutex hook_mutex;
+  BotMutex timer_mutex; // NOTE: recursive lock
 
 public:
   BotInterp(Bot *, String);