Quiet annoying mutex debug logging messages
[clinton/bobotpp.git] / source / ServerQueue.C
index b525747..7db7fb9 100644 (file)
@@ -1,6 +1,6 @@
 // ServerQueue.C  -*- C++ -*-
 // Copyright (c) 1997, 1998 Etienne BERNARD
-// Copyright (C) 2002,2005 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
 #include "config.h"
 #endif
 
-//#include <limits>
 #include "ServerQueue.H"
+
+#include <limits>
+
+#include "Bot.H"
+#include "ServerQueueItem.H"
 #include "Utils.H"
 
+#ifdef USESCRIPTS
+#include "BotInterp.H"
+#include "Interp.H"
+#endif
+
+int ServerQueue::max_penalty = 20;
+
 ServerQueue::ServerQueue(Socket * s, bool d)
   : Queue(s,d), penalty(0)
 {
@@ -35,18 +46,19 @@ ServerQueue::ServerQueue(Socket * s, bool d)
 
 ServerQueue::~ServerQueue()
 {
-  penalty = INT_MIN;
+  penalty = std::numeric_limits<int>::min ();
   flush();
 }
 
 void
 ServerQueue::addItem(ServerQueueItem *sqi)
 {
+  BotLock queue_lock (queue_mutex);
   std::list<ServerQueueItem *>::iterator it, it2;
 
   for (it = serverQueue.begin(); it != serverQueue.end(); ++it)
     {
-      if ((*it)->priority > sqi->priority)
+      if (**it < *sqi)
        {
          break;
        }
@@ -78,19 +90,25 @@ ServerQueue::addLine(String l, int pr, int pen, int t)
 bool
 ServerQueue::flush()
 {
+  // Locking around the entire queue flush prevents another thread
+  // from spamming the queue and preventing lower priority messages
+  // from ever being sent
+  BotLock flush_lock (queue_mutex);
+
   // Called every second, we decrement the penalty
   if (penalty > 0)
     {
       penalty--;
     }
 
-  while (!serverQueue.empty() && (penalty < 5)) 
+  while (!serverQueue.empty() && (penalty < max_penalty)) 
     {
-      ServerQueueItem * sqi = (*serverQueue.begin());
-      penalty += sqi->penalty + sqi->getLine().length()/100 + 1;
+      ServerQueueItem * sqi = serverQueue.front ();
+      penalty += sqi->penalty + sqi->getLine().length()/100;
 
       bool res = sendLine(sqi->getLine());
-      serverQueue.erase(serverQueue.begin());
+
+      serverQueue.pop_front ();
       delete sqi;
 
       if (!res)
@@ -115,7 +133,7 @@ ServerQueue::sendCTCP(String to, String command,
       Interp::bot->botInterp->RunHooks (Hook::SEND_ACTION,
                                        MNICK + " " + to +
                                        " " + message,
-                                       scm_listify (Utils::
+                                       scm_list_n (Utils::
                                                  str2scm (MNICK),
                                                  Utils::
                                                  str2scm (to),
@@ -127,7 +145,7 @@ ServerQueue::sendCTCP(String to, String command,
     Interp::bot->botInterp->RunHooks (Hook::SEND_CTCP,
                                      MNICK + " " + to + " " +
                                      command + " " + message,
-                                     scm_listify (Utils::
+                                     scm_list_n (Utils::
                                                   str2scm (MNICK),
                                                   Utils::
                                                   str2scm (to),
@@ -242,7 +260,7 @@ ServerQueue::sendPrivmsg(String dest, String message)
       Interp::bot->botInterp->RunHooks (Hook::SEND_PUBLIC,
                                        Interp::bot->nickName + " " + dest +
                                        " " + message,
-                                       scm_listify 
+                                       scm_list_n 
                                        (Utils::str2scm 
                                         (Interp::bot->nickName),
                                         Utils::str2scm (dest),
@@ -253,7 +271,7 @@ ServerQueue::sendPrivmsg(String dest, String message)
        (Hook::SEND_MESSAGE,
         Interp::bot->nickName + " " + dest +
         message,
-        scm_listify (Utils::str2scm (Interp::bot->nickName),
+        scm_list_n (Utils::str2scm (Interp::bot->nickName),
                      Utils::str2scm (dest),
                      Utils::str2scm
                      (message), SCM_UNDEFINED));