]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/mtasker.hh
Hack in compatibility for Boost <= 1.55
[thirdparty/pdns.git] / pdns / mtasker.hh
CommitLineData
86c152f2
BH
1/*
2 PowerDNS Versatile Database Driven Nameserver
5b0ddd18 3 Copyright (C) 2002 - 2009 PowerDNS.COM BV
86c152f2
BH
4
5 This program is free software; you can redistribute it and/or modify
22dc646a
BH
6 it under the terms of the GNU General Public License version 2
7 as published by the Free Software Foundation
f782fe38
MH
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.
86c152f2
BH
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
06bd9ccf 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
86c152f2
BH
21*/
22#ifndef MTASKER_HH
23#define MTASKER_HH
15a0aaef 24#include <stdint.h>
86c152f2 25#include <queue>
76473b92 26#include <vector>
86c152f2
BH
27#include <map>
28#include <time.h>
27af6ab1
BH
29#include <boost/multi_index_container.hpp>
30#include <boost/multi_index/ordered_index.hpp>
31#include <boost/multi_index/key_extractors.hpp>
61b26744 32#include "namespaces.hh"
b80f25df 33#include "misc.hh"
5cf909f3
AN
34#include "mtasker_context.hh"
35#include <memory>
27af6ab1 36using namespace ::boost::multi_index;
86c152f2 37
b80f25df 38// #define MTASKERTIMING 1
39
b786789d
BH
40struct KeyTag {};
41
86c152f2
BH
42//! The main MTasker class
43/** The main MTasker class. See the main page for more information.
16276aa8
RK
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.
86c152f2
BH
46 \note The EventKey needs to have an operator< defined because it is used as the key of an associative array
47*/
5cf909f3 48
86c152f2
BH
49template<class EventKey=int, class EventVal=int> class MTasker
50{
51private:
5cf909f3 52 pdns_ucontext_t d_kernel;
86c152f2
BH
53 std::queue<int> d_runQueue;
54 std::queue<int> d_zombiesQueue;
55
ec6eacbc
BH
56 struct ThreadInfo
57 {
5cf909f3
AN
58 std::shared_ptr<pdns_ucontext_t> context;
59 std::function<void(void)> start;
ec6eacbc
BH
60 char* startOfStack;
61 char* highestStackSeen;
b80f25df 62#ifdef MTASKERTIMING
63 CPUTime dt;
64 unsigned int totTime;
65#endif
ec6eacbc 66 };
35ce8576 67
ec6eacbc 68 typedef std::map<int, ThreadInfo> mthreads_t;
35ce8576
BH
69 mthreads_t d_threads;
70 int d_tid;
71 int d_maxtid;
72 size_t d_stacksize;
73
74 EventVal d_waitval;
7f1fa77d 75 enum waitstatusenum {Error=-1,TimeOut=0,Answer} d_waitstatus;
35ce8576
BH
76
77public:
86c152f2
BH
78 struct Waiter
79 {
27af6ab1 80 EventKey key;
5cf909f3 81 std::shared_ptr<pdns_ucontext_t> context;
5b0ddd18 82 struct timeval ttd;
ec6eacbc 83 int tid;
86c152f2
BH
84 };
85
27af6ab1
BH
86 typedef multi_index_container<
87 Waiter,
88 indexed_by <
89 ordered_unique<member<Waiter,EventKey,&Waiter::key> >,
5b0ddd18 90 ordered_non_unique<tag<KeyTag>, member<Waiter,struct timeval,&Waiter::ttd> >
27af6ab1
BH
91 >
92 > waiters_t;
93
86c152f2 94 waiters_t d_waiters;
27af6ab1 95
86c152f2
BH
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=8192) : d_stacksize(stacksize)
102 {
103 d_maxtid=0;
104 }
105
106 typedef void tfunc_t(void *); //!< type of the pointer that starts a thread
5b0ddd18 107 int waitEvent(EventKey &key, EventVal *val=0, unsigned int timeoutMsec=0, struct timeval* now=0);
86c152f2
BH
108 void yield();
109 int sendEvent(const EventKey& key, const EventVal* val=0);
110 void getEvents(std::vector<EventKey>& events);
d23a4bc7 111 void makeThread(tfunc_t *start, void* val);
5b0ddd18 112 bool schedule(struct timeval* now=0);
86c152f2 113 bool noProcesses();
e5d684f9 114 unsigned int numProcesses();
c836dc19 115 int getTid();
ec6eacbc 116 unsigned int getMaxStackUsage();
b80f25df 117 unsigned int getUsec();
9170fbaf 118
86c152f2 119private:
35ce8576 120 EventKey d_eventkey; // for waitEvent, contains exact key it was awoken for
86c152f2
BH
121};
122#include "mtasker.cc"
caa6eefa 123
caa6eefa
BH
124#endif // MTASKER_HH
125