From 27af6ab1e0619ac8d9534848288522eb2ead96c3 Mon Sep 17 00:00:00 2001 From: Bert Hubert Date: Sat, 12 Nov 2005 15:13:00 +0000 Subject: [PATCH] move mtasker 'waiter' structure to boost::multi_index so schedule() is no longer the prime cpu user when waiting for 1000 packets git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@547 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- pdns/mtasker.cc | 33 +++++++++++++++++++-------------- pdns/mtasker.hh | 18 +++++++++++++++++- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/pdns/mtasker.cc b/pdns/mtasker.cc index 4b292f7ba..278540186 100644 --- a/pdns/mtasker.cc +++ b/pdns/mtasker.cc @@ -1,6 +1,6 @@ /* PowerDNS Versatile Database Driven Nameserver - Copyright (C) 2002 PowerDNS.COM BV + Copyright (C) 2002 - 2005 PowerDNS.COM BV This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -25,7 +25,7 @@ support for waiting on events which can return values. \section copyright Copyright and License - MTasker is (c) 2002 by bert hubert. It is licensed to you under the terms of the GPL version 2. + MTasker is (c) 2002 - 2005 by bert hubert. It is licensed to you under the terms of the GPL version 2. \section overview High level overview MTasker is designed to support very simple cooperative multitasking to facilitate writing @@ -169,9 +169,11 @@ templateint MTasker::waitEven if(d_waiters.count(key)) { // there was already an exact same waiter return -1; } - d_waiters[key]=w; + w.key=key; + + d_waiters.insert(w); - if(swapcontext(d_waiters[key].context,&d_kernel)) { // 'A' will return here when 'key' has arrived, hands over control to kernel first + if(swapcontext(d_waiters.find(key)->context,&d_kernel)) { // 'A' will return here when 'key' has arrived, hands over control to kernel first perror("swapcontext"); exit(EXIT_FAILURE); // no way we can deal with this } @@ -210,8 +212,8 @@ templateint MTasker::sendEven if(val) d_waitval=*val; - ucontext_t *userspace=d_waiters[key].context; - d_tid=d_waiters[key].tid; // set tid + ucontext_t *userspace=d_waiters.find(key)->context; + d_tid=d_waiters.find(key)->tid; // set tid d_waiters.erase(key); // removes the waitpoint if(swapcontext(&d_kernel,userspace)) { // swaps back to the above point 'A' @@ -259,7 +261,6 @@ templatevoid MTasker::makeThread(tfunc_t *start, */ templatebool MTasker::schedule() { - if(!d_runQueue.empty()) { d_tid=d_runQueue.front(); if(swapcontext(&d_kernel, d_threads[d_tid].first)) { @@ -279,18 +280,22 @@ templatebool MTasker::schedule() } if(!d_waiters.empty()) { time_t now=time(0); - for(typename waiters_t::iterator i=d_waiters.begin();i!=d_waiters.end(); ) { - if(i->second.ttd && i->second.ttd::type waiters_by_ttd_index_t; + waiters_by_ttd_index_t& ttdindex=d_waiters.get<1>(); + + for(typename waiters_by_ttd_index_t::iterator i=ttdindex.begin(); i != ttdindex.end(); ) { + if(i->ttd && i->ttd < now) { d_waitstatus=TimeOut; - if(swapcontext(&d_kernel,i->second.context)) { // swaps back to the above point 'A' + if(swapcontext(&d_kernel,i->context)) { // swaps back to the above point 'A' perror("swapcontext in schedule2"); exit(EXIT_FAILURE); } - delete i->second.context; - d_waiters.erase(i++); // removes the waitpoint + delete i->context; + ttdindex.erase(i++); // removes the waitpoint } - else - ++i; + else if(i->ttd) + break; } } return false; diff --git a/pdns/mtasker.hh b/pdns/mtasker.hh index 643f82725..dbc899acd 100644 --- a/pdns/mtasker.hh +++ b/pdns/mtasker.hh @@ -29,6 +29,11 @@ #include #include #include +#include +#include +#include +using namespace boost; +using namespace ::boost::multi_index; //! The main MTasker class /** The main MTasker class. See the main page for more information. @@ -45,13 +50,24 @@ private: struct Waiter { + EventKey key; ucontext_t *context; time_t ttd; int tid; }; - typedef std::map waiters_t; + // typedef std::map waiters_t; + + typedef multi_index_container< + Waiter, + indexed_by < + ordered_unique >, + ordered_non_unique > + > + > waiters_t; + waiters_t d_waiters; + typedef std::map > mthreads_t; mthreads_t d_threads; int d_tid; -- 2.50.0