You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

138 lines
4.3 KiB

  1. /*
  2. * This file is part of PowerDNS or dnsdist.
  3. * Copyright -- PowerDNS.COM B.V. and its contributors
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * In addition, for the avoidance of any doubt, permission is granted to
  10. * link this program with OpenSSL and to (re)distribute the binaries
  11. * produced as the result of such linking.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #pragma once
  23. #include <stdint.h>
  24. #include <queue>
  25. #include <vector>
  26. #include <map>
  27. #include <time.h>
  28. #include <boost/multi_index_container.hpp>
  29. #include <boost/multi_index/ordered_index.hpp>
  30. #include <boost/multi_index/key_extractors.hpp>
  31. #include "namespaces.hh"
  32. #include "misc.hh"
  33. #include "mtasker_context.hh"
  34. #include <memory>
  35. #include <boost/function.hpp>
  36. using namespace ::boost::multi_index;
  37. // #define MTASKERTIMING 1
  38. struct KeyTag {};
  39. //! The main MTasker class
  40. /** The main MTasker class. See the main page for more information.
  41. \tparam EventKey Type of the key with which events are to be identified. Defaults to int.
  42. \tparam EventVal Type of the content or value of an event. Defaults to int. Cannot be set to void.
  43. \note The EventKey needs to have an operator< defined because it is used as the key of an associative array
  44. */
  45. template<class EventKey=int, class EventVal=int> class MTasker
  46. {
  47. private:
  48. pdns_ucontext_t d_kernel;
  49. std::queue<int> d_runQueue;
  50. std::queue<int> d_zombiesQueue;
  51. struct ThreadInfo
  52. {
  53. std::shared_ptr<pdns_ucontext_t> context;
  54. boost::function<void(void)> start;
  55. char* startOfStack;
  56. char* highestStackSeen;
  57. #ifdef MTASKERTIMING
  58. CPUTime dt;
  59. unsigned int totTime;
  60. #endif
  61. };
  62. typedef std::map<int, ThreadInfo> mthreads_t;
  63. mthreads_t d_threads;
  64. size_t d_stacksize;
  65. size_t d_threadsCount;
  66. int d_tid;
  67. int d_maxtid;
  68. EventVal d_waitval;
  69. enum waitstatusenum {Error=-1,TimeOut=0,Answer} d_waitstatus;
  70. public:
  71. struct Waiter
  72. {
  73. EventKey key;
  74. std::shared_ptr<pdns_ucontext_t> context;
  75. struct timeval ttd;
  76. int tid;
  77. };
  78. typedef multi_index_container<
  79. Waiter,
  80. indexed_by <
  81. ordered_unique<member<Waiter,EventKey,&Waiter::key> >,
  82. ordered_non_unique<tag<KeyTag>, member<Waiter,struct timeval,&Waiter::ttd> >
  83. >
  84. > waiters_t;
  85. waiters_t d_waiters;
  86. void initMainStackBounds()
  87. {
  88. #ifdef HAVE_FIBER_SANITIZER
  89. pthread_attr_t attr;
  90. pthread_attr_init(&attr);
  91. pthread_getattr_np(pthread_self(), &attr);
  92. pthread_attr_getstack(&attr, &t_mainStack, &t_mainStackSize);
  93. pthread_attr_destroy(&attr);
  94. #endif /* HAVE_FIBER_SANITIZER */
  95. }
  96. //! Constructor
  97. /** Constructor with a small default stacksize. If any of your threads exceeds this stack, your application will crash.
  98. This limit applies solely to the stack, the heap is not limited in any way. If threads need to allocate a lot of data,
  99. the use of new/delete is suggested.
  100. */
  101. MTasker(size_t stacksize=16*8192) : d_stacksize(stacksize), d_threadsCount(0), d_tid(0), d_maxtid(0), d_waitstatus(Error)
  102. {
  103. initMainStackBounds();
  104. // make sure our stack is 16-byte aligned to make all the architectures happy
  105. d_stacksize = d_stacksize >> 4 << 4;
  106. }
  107. typedef void tfunc_t(void *); //!< type of the pointer that starts a thread
  108. int waitEvent(EventKey &key, EventVal *val=0, unsigned int timeoutMsec=0, struct timeval* now=0);
  109. void yield();
  110. int sendEvent(const EventKey& key, const EventVal* val=0);
  111. void getEvents(std::vector<EventKey>& events);
  112. void makeThread(tfunc_t *start, void* val);
  113. bool schedule(struct timeval* now=0);
  114. bool noProcesses() const;
  115. unsigned int numProcesses() const;
  116. int getTid() const;
  117. unsigned int getMaxStackUsage();
  118. unsigned int getUsec();
  119. private:
  120. EventKey d_eventkey; // for waitEvent, contains exact key it was awoken for
  121. };
  122. #include "mtasker.cc"