]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/mtasker.hh
Merge pull request #5523 from rubenk/fix-typos-in-logmessage
[thirdparty/pdns.git] / pdns / mtasker.hh
index d79691e3f186f82456be358d9974af414f6ba114..2014d5bcae6566109a492bab28ae9404253a7237 100644 (file)
@@ -1,31 +1,27 @@
 /*
-    PowerDNS Versatile Database Driven Nameserver
-    Copyright (C) 2002 - 2009  PowerDNS.COM BV
-
   This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License version 2
-    as published by the Free Software Foundation
-
-    Additionally, the license of this program contains a special
-    exception which allows to distribute the program in binary form when
   it is linked against OpenSSL.
-
   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 St, Fifth Floor, Boston, MA  02110-1301  USA
-*/
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
* This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
+ *
* 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.
+ */
 #ifndef MTASKER_HH
 #define MTASKER_HH
 #include <stdint.h>
-#include <signal.h>
-#include <ucontext.h>
-// don't pollute the namespace with the DS register (i386 only)
-#undef DS
 #include <queue>
 #include <vector>
 #include <map>
@@ -35,6 +31,9 @@
 #include <boost/multi_index/key_extractors.hpp>
 #include "namespaces.hh"
 #include "misc.hh"
+#include "mtasker_context.hh"
+#include <memory>
+#include <boost/function.hpp>
 using namespace ::boost::multi_index;
 
 // #define MTASKERTIMING 1
@@ -47,16 +46,18 @@ struct KeyTag {};
     \tparam EventVal Type of the content or value of an event. Defaults to int. Cannot be set to void.
     \note The EventKey needs to have an operator< defined because it is used as the key of an associative array
 */
+
 template<class EventKey=int, class EventVal=int> class MTasker
 {
 private:
-  ucontext_t d_kernel;     
+  pdns_ucontext_t d_kernel;
   std::queue<int> d_runQueue;
   std::queue<int> d_zombiesQueue;
 
   struct ThreadInfo
   {
-       ucontext_t* context;
+       std::shared_ptr<pdns_ucontext_t> context;
+       boost::function<void(void)> start;
        char* startOfStack;
        char* highestStackSeen;
 #ifdef MTASKERTIMING
@@ -78,7 +79,7 @@ public:
   struct Waiter
   {
     EventKey key;
-    ucontext_t *context;
+    std::shared_ptr<pdns_ucontext_t> context;
     struct timeval ttd;
     int tid;    
   };
@@ -93,14 +94,25 @@ public:
 
   waiters_t d_waiters;
 
+  void initMainStackBounds()
+  {
+#ifdef HAVE_FIBER_SANITIZER
+    pthread_attr_t attr;
+    pthread_attr_init(&attr);
+    pthread_getattr_np(pthread_self(), &attr);
+    pthread_attr_getstack(&attr, &t_mainStack, &t_mainStackSize);
+    pthread_attr_destroy(&attr);
+#endif /* HAVE_FIBER_SANITIZER */
+  }
+
   //! Constructor
   /** Constructor with a small default stacksize. If any of your threads exceeds this stack, your application will crash. 
       This limit applies solely to the stack, the heap is not limited in any way. If threads need to allocate a lot of data,
       the use of new/delete is suggested. 
    */
-  MTasker(size_t stacksize=8192) : d_stacksize(stacksize)
+  MTasker(size_t stacksize=8192) : d_tid(0), d_maxtid(0), d_stacksize(stacksize), d_waitstatus(Error)
   {
-    d_maxtid=0;
+    initMainStackBounds();
   }
 
   typedef void tfunc_t(void *); //!< type of the pointer that starts a thread 
@@ -110,14 +122,13 @@ public:
   void getEvents(std::vector<EventKey>& events);
   void makeThread(tfunc_t *start, void* val);
   bool schedule(struct timeval* now=0);
-  bool noProcesses();
-  unsigned int numProcesses();
-  int getTid()
+  bool noProcesses() const;
+  unsigned int numProcesses() const;
+  int getTid() const;
   unsigned int getMaxStackUsage();
   unsigned int getUsec();
 
 private:
-  static void threadWrapper(uint32_t self1, uint32_t self2, tfunc_t *tf, int tid, uint32_t val1, uint32_t val2);
   EventKey d_eventkey;   // for waitEvent, contains exact key it was awoken for
 };
 #include "mtasker.cc"