]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/mtasker.hh
6e0416fe01b05ae75a2d2eb917da52d5b2c41426
[thirdparty/pdns.git] / pdns / mtasker.hh
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 - 2009 PowerDNS.COM BV
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2
7 as published by the Free Software Foundation
8
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 #ifndef MTASKER_HH
23 #define MTASKER_HH
24 #include <stdint.h>
25 #include <queue>
26 #include <vector>
27 #include <map>
28 #include <time.h>
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"
33 #include "misc.hh"
34 #include "mtasker_context.hh"
35 #include <memory>
36 #include <boost/function.hpp>
37 using namespace ::boost::multi_index;
38
39 // #define MTASKERTIMING 1
40
41 struct KeyTag {};
42
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
48 */
49
50 template<class EventKey=int, class EventVal=int> class MTasker
51 {
52 private:
53 pdns_ucontext_t d_kernel;
54 std::queue<int> d_runQueue;
55 std::queue<int> d_zombiesQueue;
56
57 struct ThreadInfo
58 {
59 std::shared_ptr<pdns_ucontext_t> context;
60 boost::function<void(void)> start;
61 char* startOfStack;
62 char* highestStackSeen;
63 #ifdef MTASKERTIMING
64 CPUTime dt;
65 unsigned int totTime;
66 #endif
67 };
68
69 typedef std::map<int, ThreadInfo> mthreads_t;
70 mthreads_t d_threads;
71 int d_tid;
72 int d_maxtid;
73 size_t d_stacksize;
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 //! Constructor
98 /** Constructor with a small default stacksize. If any of your threads exceeds this stack, your application will crash.
99 This limit applies solely to the stack, the heap is not limited in any way. If threads need to allocate a lot of data,
100 the use of new/delete is suggested.
101 */
102 MTasker(size_t stacksize=8192) : d_tid(0), d_maxtid(0), d_stacksize(stacksize), d_waitstatus(Error)
103 {
104 }
105
106 typedef void tfunc_t(void *); //!< type of the pointer that starts a thread
107 int waitEvent(EventKey &key, EventVal *val=0, unsigned int timeoutMsec=0, struct timeval* now=0);
108 void yield();
109 int sendEvent(const EventKey& key, const EventVal* val=0);
110 void getEvents(std::vector<EventKey>& events);
111 void makeThread(tfunc_t *start, void* val);
112 bool schedule(struct timeval* now=0);
113 bool noProcesses();
114 unsigned int numProcesses();
115 int getTid();
116 unsigned int getMaxStackUsage();
117 unsigned int getUsec();
118
119 private:
120 EventKey d_eventkey; // for waitEvent, contains exact key it was awoken for
121 };
122 #include "mtasker.cc"
123
124 #endif // MTASKER_HH
125