]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/mtasker.hh
Replace include guard ifdef/define with pragma once
[thirdparty/pdns.git] / pdns / mtasker.hh
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
38 // #define MTASKERTIMING 1
39
40 struct KeyTag {};
41
42 //! The main MTasker class
43 /** The main MTasker class. See the main page for more information.
44 \tparam EventKey Type of the key with which events are to be identified. Defaults to int.
45 \tparam EventVal Type of the content or value of an event. Defaults to int. Cannot be set to void.
46 \note The EventKey needs to have an operator< defined because it is used as the key of an associative array
47 */
48
49 template<class EventKey=int, class EventVal=int> class MTasker
50 {
51 private:
52 pdns_ucontext_t d_kernel;
53 std::queue<int> d_runQueue;
54 std::queue<int> d_zombiesQueue;
55
56 struct ThreadInfo
57 {
58 std::shared_ptr<pdns_ucontext_t> context;
59 boost::function<void(void)> start;
60 char* startOfStack;
61 char* highestStackSeen;
62 #ifdef MTASKERTIMING
63 CPUTime dt;
64 unsigned int totTime;
65 #endif
66 };
67
68 typedef std::map<int, ThreadInfo> mthreads_t;
69 mthreads_t d_threads;
70 size_t d_stacksize;
71 size_t d_threadsCount;
72 int d_tid;
73 int d_maxtid;
74
75 EventVal d_waitval;
76 enum waitstatusenum {Error=-1,TimeOut=0,Answer} d_waitstatus;
77
78 public:
79 struct Waiter
80 {
81 EventKey key;
82 std::shared_ptr<pdns_ucontext_t> context;
83 struct timeval ttd;
84 int tid;
85 };
86
87 typedef multi_index_container<
88 Waiter,
89 indexed_by <
90 ordered_unique<member<Waiter,EventKey,&Waiter::key> >,
91 ordered_non_unique<tag<KeyTag>, member<Waiter,struct timeval,&Waiter::ttd> >
92 >
93 > waiters_t;
94
95 waiters_t d_waiters;
96
97 void initMainStackBounds()
98 {
99 #ifdef HAVE_FIBER_SANITIZER
100 pthread_attr_t attr;
101 pthread_attr_init(&attr);
102 pthread_getattr_np(pthread_self(), &attr);
103 pthread_attr_getstack(&attr, &t_mainStack, &t_mainStackSize);
104 pthread_attr_destroy(&attr);
105 #endif /* HAVE_FIBER_SANITIZER */
106 }
107
108 //! Constructor
109 /** Constructor with a small default stacksize. If any of your threads exceeds this stack, your application will crash.
110 This limit applies solely to the stack, the heap is not limited in any way. If threads need to allocate a lot of data,
111 the use of new/delete is suggested.
112 */
113 MTasker(size_t stacksize=16*8192) : d_stacksize(stacksize), d_threadsCount(0), d_tid(0), d_maxtid(0), d_waitstatus(Error)
114 {
115 initMainStackBounds();
116
117 // make sure our stack is 16-byte aligned to make all the architectures happy
118 d_stacksize = d_stacksize >> 4 << 4;
119 }
120
121 typedef void tfunc_t(void *); //!< type of the pointer that starts a thread
122 int waitEvent(EventKey &key, EventVal *val=0, unsigned int timeoutMsec=0, struct timeval* now=0);
123 void yield();
124 int sendEvent(const EventKey& key, const EventVal* val=0);
125 void getEvents(std::vector<EventKey>& events);
126 void makeThread(tfunc_t *start, void* val);
127 bool schedule(struct timeval* now=0);
128 bool noProcesses() const;
129 unsigned int numProcesses() const;
130 int getTid() const;
131 unsigned int getMaxStackUsage();
132 unsigned int getUsec();
133
134 private:
135 EventKey d_eventkey; // for waitEvent, contains exact key it was awoken for
136 };
137 #include "mtasker.cc"