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.
 
 
 
 
 
 

413 lines
14 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. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include "mtasker.hh"
  26. #include "misc.hh"
  27. #include <stdio.h>
  28. #include <iostream>
  29. #ifdef PDNS_USE_VALGRIND
  30. #include <valgrind/valgrind.h>
  31. #endif /* PDNS_USE_VALGRIND */
  32. /** \page MTasker
  33. Simple system for implementing cooperative multitasking of functions, with
  34. support for waiting on events which can return values.
  35. \section copyright Copyright and License
  36. MTasker is (c) 2002 - 2009 by bert hubert. It is licensed to you under the terms of the GPL version 2.
  37. \section overview High level overview
  38. MTasker is designed to support very simple cooperative multitasking to facilitate writing
  39. code that would ordinarily require a statemachine, for which the author does not consider
  40. himself smart enough.
  41. This class does not perform any magic it only makes calls to makecontext() and swapcontext().
  42. Getting the details right however is complicated and MTasker does that for you.
  43. If preemptive multitasking or more advanced concepts such as semaphores, locks or mutexes
  44. are required, the use of POSIX threads is advised.
  45. MTasker is designed to offer the performance of statemachines while maintaining simple thread semantics. It is not
  46. a replacement for a full threading system.
  47. \section compatibility Compatibility
  48. MTasker is only guaranteed to work on Linux with glibc 2.2.5 and higher. It does not work on FreeBSD and notably,
  49. not on Red Hat 6.0. It may work on Solaris, please test.
  50. \section concepts Concepts
  51. There are two important concepts, the 'kernel' and the 'thread'. Each thread starts out as a function,
  52. which is passed to MTasker::makeThread(), together with a possible argument.
  53. This function is now free to do whatever it wants, but realise that MTasker implements cooperative
  54. multitasking, which means that the coder has the responsibility of not taking the CPU overly long.
  55. Other threads can only get the CPU if MTasker::yield() is called or if a thread sleeps to wait for an event,
  56. using the MTasker::waitEvent() method.
  57. \section kernel The Kernel
  58. The Kernel consists of functions that do housekeeping, but also of code that the client coder
  59. can call to report events. A minimal kernel loop looks like this:
  60. \code
  61. for(;;) {
  62. MT.schedule();
  63. if(MT.noProcesses()) // exit if no processes are left
  64. break;
  65. }
  66. \endcode
  67. The kernel typically starts from the main() function of your program. New threads are also
  68. created from the kernel. This can also happen before entering the main loop. To start a thread,
  69. the method MTasker::makeThread is provided.
  70. \section events Events
  71. By default, Events are recognized by an int and their value is also an int.
  72. This can be overridden by specifying the EventKey and EventVal template parameters.
  73. An event can be a keypress, but also a UDP packet, or a bit of data from a TCP socket. The
  74. sample code provided works with keypresses, but that is just a not very useful example.
  75. A thread can also wait for an event only for a limited time, and receive a timeout of that
  76. event did not occur within the specified timeframe.
  77. \section example A simple menu system
  78. \code
  79. MTasker<> MT;
  80. void menuHandler(void *p)
  81. {
  82. int num=(int)p;
  83. cout<<"Key handler for key "<<num<<" launched"<<endl;
  84. MT.waitEvent(num);
  85. cout<<"Key "<<num<<" was pressed!"<<endl;
  86. }
  87. int main()
  88. {
  89. char line[10];
  90. for(int i=0;i<10;++i)
  91. MT.makeThread(menuHandler,(void *)i);
  92. for(;;) {
  93. while(MT.schedule()); // do everything we can do
  94. if(MT.noProcesses()) // exit if no processes are left
  95. break;
  96. if(!fgets(line,sizeof(line),stdin))
  97. break;
  98. MT.sendEvent(*line-'0');
  99. }
  100. }
  101. \endcode
  102. \section example2 Canonical multitasking example
  103. This implements the canonical multitasking example, alternately printing an 'A' and a 'B'. The Linux kernel
  104. started this way too.
  105. \code
  106. void printer(void *p)
  107. {
  108. char c=(char)p;
  109. for(;;) {
  110. cout<<c<<endl;
  111. MT.yield();
  112. }
  113. }
  114. int main()
  115. {
  116. MT.makeThread(printer,(void*)'a');
  117. MT.makeThread(printer,(void*)'b');
  118. for(;;) {
  119. while(MT.schedule()); // do everything we can do
  120. if(MT.noProcesses()) // exit if no processes are left
  121. break;
  122. }
  123. }
  124. \endcode
  125. */
  126. //! puts a thread to sleep waiting until a specified event arrives
  127. /** Threads can call waitEvent to register that they are waiting on an event with a certain key.
  128. If so desired, the event can carry data which is returned in val in case that is non-zero.
  129. Furthermore, a timeout can be specified in seconds.
  130. Only one thread can be waiting on a key, results of trying to have more threads
  131. waiting on the same key are undefined.
  132. \param key Event key to wait for. Needs to match up to a key reported to sendEvent
  133. \param val If non-zero, the value of the event will be stored in *val
  134. \param timeout If non-zero, number of seconds to wait for an event.
  135. \return returns -1 in case of error, 0 in case of timeout, 1 in case of an answer
  136. */
  137. template<class EventKey, class EventVal>int MTasker<EventKey,EventVal>::waitEvent(EventKey &key, EventVal *val, unsigned int timeoutMsec, struct timeval* now)
  138. {
  139. if(d_waiters.count(key)) { // there was already an exact same waiter
  140. return -1;
  141. }
  142. Waiter w;
  143. w.context=std::make_shared<pdns_ucontext_t>();
  144. w.ttd.tv_sec = 0; w.ttd.tv_usec = 0;
  145. if(timeoutMsec) {
  146. struct timeval increment;
  147. increment.tv_sec = timeoutMsec / 1000;
  148. increment.tv_usec = 1000 * (timeoutMsec % 1000);
  149. if(now)
  150. w.ttd = increment + *now;
  151. else {
  152. struct timeval realnow;
  153. gettimeofday(&realnow, 0);
  154. w.ttd = increment + realnow;
  155. }
  156. }
  157. w.tid=d_tid;
  158. w.key=key;
  159. d_waiters.insert(w);
  160. #ifdef MTASKERTIMING
  161. unsigned int diff=d_threads[d_tid].dt.ndiff()/1000;
  162. d_threads[d_tid].totTime+=diff;
  163. #endif
  164. notifyStackSwitchToKernel();
  165. pdns_swapcontext(*d_waiters.find(key)->context,d_kernel); // 'A' will return here when 'key' has arrived, hands over control to kernel first
  166. notifyStackSwitchDone();
  167. #ifdef MTASKERTIMING
  168. d_threads[d_tid].dt.start();
  169. #endif
  170. if(val && d_waitstatus==Answer)
  171. *val=d_waitval;
  172. d_tid=w.tid;
  173. if((char*)&w < d_threads[d_tid].highestStackSeen) {
  174. d_threads[d_tid].highestStackSeen = (char*)&w;
  175. }
  176. key=d_eventkey;
  177. return d_waitstatus;
  178. }
  179. //! yields control to the kernel or other threads
  180. /** Hands over control to the kernel, allowing other processes to run, or events to arrive */
  181. template<class Key, class Val>void MTasker<Key,Val>::yield()
  182. {
  183. d_runQueue.push(d_tid);
  184. notifyStackSwitchToKernel();
  185. pdns_swapcontext(*d_threads[d_tid].context ,d_kernel); // give control to the kernel
  186. notifyStackSwitchDone();
  187. }
  188. //! reports that an event took place for which threads may be waiting
  189. /** From the kernel loop, sendEvent can be called to report that something occurred for which there may be waiters.
  190. \param key Key of the event for which threads may be waiting
  191. \param val If non-zero, pointer to the content of the event
  192. \return Returns -1 in case of error, 0 if there were no waiters, 1 if a thread was woken up.
  193. WARNING: when passing val as zero, d_waitval is undefined, and hence waitEvent will return undefined!
  194. */
  195. template<class EventKey, class EventVal>int MTasker<EventKey,EventVal>::sendEvent(const EventKey& key, const EventVal* val)
  196. {
  197. typename waiters_t::iterator waiter=d_waiters.find(key);
  198. if(waiter == d_waiters.end()) {
  199. // cout<<"Event sent nobody was waiting for!"<<endl;
  200. return 0;
  201. }
  202. d_waitstatus=Answer;
  203. if(val)
  204. d_waitval=*val;
  205. d_tid=waiter->tid; // set tid
  206. d_eventkey=waiter->key; // pass waitEvent the exact key it was woken for
  207. auto userspace=std::move(waiter->context);
  208. d_waiters.erase(waiter); // removes the waitpoint
  209. notifyStackSwitch(d_threads[d_tid].startOfStack, d_stacksize);
  210. pdns_swapcontext(d_kernel,*userspace); // swaps back to the above point 'A'
  211. notifyStackSwitchDone();
  212. return 1;
  213. }
  214. //! launches a new thread
  215. /** The kernel can call this to make a new thread, which starts at the function start and gets passed the val void pointer.
  216. \param start Pointer to the function which will form the start of the thread
  217. \param val A void pointer that can be used to pass data to the thread
  218. */
  219. template<class Key, class Val>void MTasker<Key,Val>::makeThread(tfunc_t *start, void* val)
  220. {
  221. auto uc=std::make_shared<pdns_ucontext_t>();
  222. uc->uc_link = &d_kernel; // come back to kernel after dying
  223. uc->uc_stack.resize (d_stacksize+1);
  224. #ifdef PDNS_USE_VALGRIND
  225. uc->valgrind_id = VALGRIND_STACK_REGISTER(&uc->uc_stack[0],
  226. &uc->uc_stack[uc->uc_stack.size()-1]);
  227. #endif /* PDNS_USE_VALGRIND */
  228. ++d_threadsCount;
  229. auto& thread = d_threads[d_maxtid];
  230. auto mt = this;
  231. thread.start = [start, val, mt]() {
  232. char dummy;
  233. mt->d_threads[mt->d_tid].startOfStack = mt->d_threads[mt->d_tid].highestStackSeen = &dummy;
  234. auto const tid = mt->d_tid;
  235. start (val);
  236. mt->d_zombiesQueue.push(tid);
  237. };
  238. pdns_makecontext (*uc, thread.start);
  239. thread.context = std::move(uc);
  240. d_runQueue.push(d_maxtid++); // will run at next schedule invocation
  241. }
  242. //! needs to be called periodically so threads can run and housekeeping can be performed
  243. /** The kernel should call this function every once in a while. It makes sense
  244. to call this function if you:
  245. - reported an event
  246. - called makeThread
  247. - want to have threads running waitEvent() to get a timeout if enough time passed
  248. \return Returns if there is more work scheduled and recalling schedule now would be useful
  249. */
  250. template<class Key, class Val>bool MTasker<Key,Val>::schedule(struct timeval* now)
  251. {
  252. if(!d_runQueue.empty()) {
  253. d_tid=d_runQueue.front();
  254. #ifdef MTASKERTIMING
  255. d_threads[d_tid].dt.start();
  256. #endif
  257. notifyStackSwitch(d_threads[d_tid].startOfStack, d_stacksize);
  258. pdns_swapcontext(d_kernel, *d_threads[d_tid].context);
  259. notifyStackSwitchDone();
  260. d_runQueue.pop();
  261. return true;
  262. }
  263. if(!d_zombiesQueue.empty()) {
  264. d_threads.erase(d_zombiesQueue.front());
  265. --d_threadsCount;
  266. d_zombiesQueue.pop();
  267. return true;
  268. }
  269. if(!d_waiters.empty()) {
  270. struct timeval rnow;
  271. if(!now)
  272. gettimeofday(&rnow, 0);
  273. else
  274. rnow = *now;
  275. typedef typename waiters_t::template index<KeyTag>::type waiters_by_ttd_index_t;
  276. // waiters_by_ttd_index_t& ttdindex=d_waiters.template get<KeyTag>();
  277. waiters_by_ttd_index_t& ttdindex=boost::multi_index::get<KeyTag>(d_waiters);
  278. for(typename waiters_by_ttd_index_t::iterator i=ttdindex.begin(); i != ttdindex.end(); ) {
  279. if(i->ttd.tv_sec && i->ttd < rnow) {
  280. d_waitstatus=TimeOut;
  281. d_eventkey=i->key; // pass waitEvent the exact key it was woken for
  282. auto uc = i->context;
  283. d_tid = i->tid;
  284. ttdindex.erase(i++); // removes the waitpoint
  285. notifyStackSwitch(d_threads[d_tid].startOfStack, d_stacksize);
  286. pdns_swapcontext(d_kernel, *uc); // swaps back to the above point 'A'
  287. notifyStackSwitchDone();
  288. }
  289. else if(i->ttd.tv_sec)
  290. break;
  291. else
  292. ++i;
  293. }
  294. }
  295. return false;
  296. }
  297. //! returns true if there are no processes
  298. /** Call this to check if no processes are running anymore
  299. \return true if no processes are left
  300. */
  301. template<class Key, class Val>bool MTasker<Key,Val>::noProcesses() const
  302. {
  303. return d_threadsCount == 0;
  304. }
  305. //! returns the number of processes running
  306. /** Call this to perhaps limit activities if too many threads are running
  307. \return number of processes running
  308. */
  309. template<class Key, class Val>unsigned int MTasker<Key,Val>::numProcesses() const
  310. {
  311. return d_threadsCount;
  312. }
  313. //! gives access to the list of Events threads are waiting for
  314. /** The kernel can call this to get a list of Events threads are waiting for. This is very useful
  315. to setup 'select' or 'poll' or 'aio' events needed to satisfy these requests.
  316. getEvents clears the events parameter before filling it.
  317. \param events Vector which is to be filled with keys threads are waiting for
  318. */
  319. template<class Key, class Val>void MTasker<Key,Val>::getEvents(std::vector<Key>& events)
  320. {
  321. events.clear();
  322. for(typename waiters_t::const_iterator i=d_waiters.begin();i!=d_waiters.end();++i) {
  323. events.push_back(i->first);
  324. }
  325. }
  326. //! Returns the current Thread ID (tid)
  327. /** Processes can call this to get a numerical representation of their current thread ID.
  328. This can be useful for logging purposes.
  329. */
  330. template<class Key, class Val>int MTasker<Key,Val>::getTid() const
  331. {
  332. return d_tid;
  333. }
  334. //! Returns the maximum stack usage so far of this MThread
  335. template<class Key, class Val>unsigned int MTasker<Key,Val>::getMaxStackUsage()
  336. {
  337. return d_threads[d_tid].startOfStack - d_threads[d_tid].highestStackSeen;
  338. }
  339. //! Returns the maximum stack usage so far of this MThread
  340. template<class Key, class Val>unsigned int MTasker<Key,Val>::getUsec()
  341. {
  342. #ifdef MTASKERTIMING
  343. return d_threads[d_tid].totTime + d_threads[d_tid].dt.ndiff()/1000;
  344. #else
  345. return 0;
  346. #endif
  347. }