]> granicus.if.org Git - icinga2/commitdiff
Improved Event::Post performance.
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 13 Jul 2012 21:33:30 +0000 (23:33 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 13 Jul 2012 21:37:05 +0000 (23:37 +0200)
base/application.cpp
base/asynctask.h
base/event.cpp
base/event.h
base/logger.cpp
base/socket.cpp
base/tcpclient.cpp
base/tcpserver.cpp
base/tlsclient.cpp
cib/nagioschecktask.cpp

index 2d5d1b53f5cf1494b8c17d30b5cadf8fbf0ed600..253d9d3e4ca47c66aecba93979bb429990e0d7c7 100644 (file)
@@ -99,14 +99,7 @@ void Application::RunEventLoop(void)
                if (m_ShuttingDown)
                        break;
 
-               vector<Event::Ptr> events;
-               
-               Event::Wait(&events, boost::get_system_time() + boost::posix_time::seconds(sleep));
-
-               for (vector<Event::Ptr>::iterator it = events.begin(); it != events.end(); it++) {
-                       Event::Ptr ev = *it;
-                       ev->OnEventDelivered();
-               }
+               Event::ProcessEvents(boost::get_system_time() + boost::posix_time::seconds(sleep));
        }
 }
 
index 08237355ce950b29f158e5bb1cfe91b721644a71..75fe0283b0fd8ce287c0aa8f99c8a954b91fcfb7 100644 (file)
@@ -53,18 +53,10 @@ protected:
 
        void Finish(void)
        {
-               Event::Ptr ev = boost::make_shared<Event>();
-               ev->OnEventDelivered.connect(boost::bind(&T::FinishForwarder, static_cast<shared_ptr<T> >(GetSelf())));
-               Event::Post(ev);
+               Event::Post(boost::bind(boost::cref(OnTaskCompleted), static_cast<shared_ptr<T> >(GetSelf())));
        }
 
        bool m_Finished;
-
-private:
-       static void FinishForwarder(const shared_ptr<T>& task)
-       {
-               task->OnTaskCompleted(task);
-       }
 };
 
 }
index b83d70f60fb26e2af2dde86c96c5e0c25277b160..d3692f60eb16090c0005c847897300aae182130b 100644 (file)
 
 using namespace icinga;
 
-deque<Event::Ptr> Event::m_Events;
+vector<Event> Event::m_Events;
 condition_variable Event::m_EventAvailable;
 mutex Event::m_Mutex;
 
-bool Event::Wait(vector<Event::Ptr> *events, const system_time& wait_until)
+Event::Event(const function<void ()>& callback)
+       : m_Callback(callback)
+{ }
+
+void Event::ProcessEvents(const system_time& wait_until)
 {
-       mutex::scoped_lock lock(m_Mutex);
+       vector<Event> events;
+
+       {
+               mutex::scoped_lock lock(m_Mutex);
+
+               while (m_Events.empty()) {
+                       if (!m_EventAvailable.timed_wait(lock, wait_until))
+                               return;
+               }
 
-       while (m_Events.empty()) {
-               if (!m_EventAvailable.timed_wait(lock, wait_until))
-                       return false;
+               events.swap(m_Events);
        }
-       
-       vector<Event::Ptr> result;
-       std::copy(m_Events.begin(), m_Events.end(), back_inserter(*events));
-       m_Events.clear();
 
-       return true;
+       vector<Event>::iterator it;
+       for (it = events.begin(); it != events.end(); it++)
+               it->m_Callback();
 }
 
-void Event::Post(const Event::Ptr& ev)
+void Event::Post(const function<void ()>& callback)
 {
        if (Application::IsMainThread()) {
-               ev->OnEventDelivered();
+               callback();
                return;
        }
 
+       Event ev(callback);
+
        {
                mutex::scoped_lock lock(m_Mutex);
                m_Events.push_back(ev);
index 5919f4e6592d95a28a07861ebe368e5290deeee3..4f1b16e7b78e985cab0f9a349354e6dd3ce2fb57 100644 (file)
 namespace icinga
 {
 
-class I2_BASE_API Event : public Object
+class I2_BASE_API Event
 {
 public:
-       typedef shared_ptr<Event> Ptr;
-       typedef weak_ptr<Event> WeakPtr;
+       static void ProcessEvents(const system_time& wait_until);
+       static void Post(const function<void ()>& callback);
 
-       static bool Wait(vector<Event::Ptr> *events, const system_time& wait_until);
-       static void Post(const Event::Ptr& ev);
+private:
+       Event(const function<void ()>& callback);
 
-       boost::signal<void ()> OnEventDelivered;
+       function<void ()> m_Callback;
 
-private:
-       static deque<Event::Ptr> m_Events;
+       static vector<Event> m_Events;
        static condition_variable m_EventAvailable;
        static mutex m_Mutex;
 };
index 5699b12d399b73f8931ad398e13bcceea4c21cb7..591fe79528d6f4f91e05b1518186a1e0b3a92b8b 100644 (file)
@@ -49,9 +49,7 @@ void Logger::Write(LogSeverity severity, const string& facility,
        entry.Facility = facility;
        entry.Message = message;
 
-       Event::Ptr ev = boost::make_shared<Event>();
-       ev->OnEventDelivered.connect(boost::bind(&Logger::ForwardLogEntry, entry));
-       Event::Post(ev);
+       Event::Post(boost::bind(&Logger::ForwardLogEntry, entry));
 }
 
 /**
index 32a2c45efd1612768e6f54b0a11ab5604cac87f8..fb25499351badd00115c09cc8e513278b1684d5d 100644 (file)
@@ -111,11 +111,8 @@ void Socket::CloseInternal(bool from_dtor)
 
        /* nobody can possibly have a valid event subscription when the
                destructor has been called */
-       if (!from_dtor) {
-               Event::Ptr ev = boost::make_shared<Event>();
-               ev->OnEventDelivered.connect(boost::bind(boost::ref(OnClosed), GetSelf()));
-               Event::Post(ev);
-       }
+       if (!from_dtor)
+               Event::Post(boost::bind(boost::ref(OnClosed), GetSelf()));
 }
 
 /**
@@ -159,9 +156,7 @@ int Socket::GetLastSocketError(void)
 void Socket::HandleSocketError(const exception& ex)
 {
        if (!OnError.empty()) {
-               Event::Ptr ev = boost::make_shared<Event>();
-               ev->OnEventDelivered.connect(boost::bind(boost::ref(OnError), GetSelf(), runtime_error(ex.what())));
-               Event::Post(ev);
+               Event::Post(boost::bind(boost::ref(OnError), GetSelf(), runtime_error(ex.what())));
 
                CloseInternal(false);
        } else {
index 2b0d18f52b9ff1e4ad6a01e6e2d3e06b56bb2010..3f95783e3965e5e43b6005fa977ec635fb21cf73 100644 (file)
@@ -159,9 +159,7 @@ void TcpClient::HandleReadable(void)
                m_RecvQueue->Write(NULL, rc);
        }
 
-       Event::Ptr ev = boost::make_shared<Event>();
-       ev->OnEventDelivered.connect(boost::bind(boost::ref(OnDataAvailable), GetSelf()));
-       Event::Post(ev);
+       Event::Post(boost::bind(boost::ref(OnDataAvailable), GetSelf()));
 }
 
 /**
index c4b863e54163a2c8d7107b332e4e4ac1f3f7e178..2da1a73f0ca8051a7d481d27e06bb0b7d31f7fc7 100644 (file)
@@ -91,7 +91,5 @@ void TcpServer::HandleReadable(void)
 
        TcpClient::Ptr client = m_ClientFactory(fd);
 
-       Event::Ptr ev = boost::make_shared<Event>();
-       ev->OnEventDelivered.connect(boost::bind(boost::ref(OnNewClient), GetSelf(), client));
-       Event::Post(ev);
+       Event::Post(boost::bind(boost::ref(OnNewClient), GetSelf(), client));
 }
index 4c63a9f7ebd5fd485e32a259b633765cafe6f70b..7ddb76ce0dff7c1f3da2ab357a2b4c7454eddb2e 100644 (file)
@@ -142,9 +142,7 @@ void TlsClient::HandleReadable(void)
        }
 
 post_event:
-       Event::Ptr ev = boost::make_shared<Event>();
-       ev->OnEventDelivered.connect(boost::bind(boost::ref(OnDataAvailable), GetSelf()));
-       Event::Post(ev);
+       Event::Post(boost::bind(boost::ref(OnDataAvailable), GetSelf()));
 }
 
 /**
@@ -257,11 +255,8 @@ int TlsClient::ValidateCertificateInternal(int ok, X509_STORE_CTX *x509Context)
        shared_ptr<X509> x509Certificate = shared_ptr<X509>(x509Context->cert, &TlsClient::NullCertificateDeleter);
        bool valid = ValidateCertificate((ok != 0), x509Context, x509Certificate);
 
-       if (valid) {
-               Event::Ptr ev = boost::make_shared<Event>();
-               ev->OnEventDelivered.connect(boost::bind(boost::ref(OnCertificateValidated), GetSelf()));
-               Event::Post(ev);
-       }
+       if (valid)
+               Event::Post(boost::bind(boost::ref(OnCertificateValidated), GetSelf()));
 
        return valid ? 1 : 0;
 }
index b17147d62a0cd86d93fdb12fddc8aa52503d5eee..5b6dc23fca87007f336f471aa63e247e1fa8a9e4 100644 (file)
@@ -46,6 +46,10 @@ void NagiosCheckTask::Run(void)
 
 void NagiosCheckTask::ProcessFinishedHandler(void)
 {
+       time_t now;
+       time(&now);
+       GetResult().SetExecutionEnd(now);
+
        string output = m_Process->GetOutput();
        boost::algorithm::trim(output);
        ProcessCheckOutput(output);
@@ -71,9 +75,8 @@ void NagiosCheckTask::ProcessFinishedHandler(void)
 
        GetResult().SetState(state);
 
-       time_t now;
        time(&now);
-       GetResult().SetExecutionEnd(now);
+       GetResult().SetScheduleEnd(now);
 
        Finish();
 }