2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
29 #include <boost/multi_index_container.hpp>
30 #include <boost/multi_index/ordered_index.hpp>
31 #include <boost/multi_index/key_extractors.hpp>
32 #include "namespaces.hh"
34 #include "mtasker_context.hh"
36 #include <boost/function.hpp>
37 using namespace ::boost::multi_index;
39 // #define MTASKERTIMING 1
43 //! The main MTasker class
44 /** The main MTasker class. See the main page for more information.
45 \tparam EventKey Type of the key with which events are to be identified. Defaults to int.
46 \tparam EventVal Type of the content or value of an event. Defaults to int. Cannot be set to void.
47 \note The EventKey needs to have an operator< defined because it is used as the key of an associative array
50 template<class EventKey=int, class EventVal=int> class MTasker
53 pdns_ucontext_t d_kernel;
54 std::queue<int> d_runQueue;
55 std::queue<int> d_zombiesQueue;
59 std::shared_ptr<pdns_ucontext_t> context;
60 boost::function<void(void)> start;
62 char* highestStackSeen;
69 typedef std::map<int, ThreadInfo> mthreads_t;
72 size_t d_threadsCount;
77 enum waitstatusenum {Error=-1,TimeOut=0,Answer} d_waitstatus;
83 std::shared_ptr<pdns_ucontext_t> context;
88 typedef multi_index_container<
91 ordered_unique<member<Waiter,EventKey,&Waiter::key> >,
92 ordered_non_unique<tag<KeyTag>, member<Waiter,struct timeval,&Waiter::ttd> >
98 void initMainStackBounds()
100 #ifdef HAVE_FIBER_SANITIZER
102 pthread_attr_init(&attr);
103 pthread_getattr_np(pthread_self(), &attr);
104 pthread_attr_getstack(&attr, &t_mainStack, &t_mainStackSize);
105 pthread_attr_destroy(&attr);
106 #endif /* HAVE_FIBER_SANITIZER */
110 /** Constructor with a small default stacksize. If any of your threads exceeds this stack, your application will crash.
111 This limit applies solely to the stack, the heap is not limited in any way. If threads need to allocate a lot of data,
112 the use of new/delete is suggested.
114 MTasker(size_t stacksize=16*8192) : d_stacksize(stacksize), d_threadsCount(0), d_tid(0), d_maxtid(0), d_waitstatus(Error)
116 initMainStackBounds();
118 // make sure our stack is 16-byte aligned to make all the architectures happy
119 d_stacksize = d_stacksize >> 4 << 4;
122 typedef void tfunc_t(void *); //!< type of the pointer that starts a thread
123 int waitEvent(EventKey &key, EventVal *val=0, unsigned int timeoutMsec=0, struct timeval* now=0);
125 int sendEvent(const EventKey& key, const EventVal* val=0);
126 void getEvents(std::vector<EventKey>& events);
127 void makeThread(tfunc_t *start, void* val);
128 bool schedule(struct timeval* now=0);
129 bool noProcesses() const;
130 unsigned int numProcesses() const;
132 unsigned int getMaxStackUsage();
133 unsigned int getUsec();
136 EventKey d_eventkey; // for waitEvent, contains exact key it was awoken for
138 #include "mtasker.cc"