2012-07-13 21:07:39 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* 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 the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-07-13 21:00:54 +02:00
|
|
|
#ifndef ASYNCTASK_H
|
2013-02-17 19:14:34 +01:00
|
|
|
#define ASYNCTASK_H
|
2012-07-13 21:00:54 +02:00
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/function.hpp>
|
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
#include <boost/thread/condition_variable.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2012-07-13 21:00:54 +02:00
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2012-07-15 10:58:03 +02:00
|
|
|
/**
|
|
|
|
* An asynchronous task.
|
|
|
|
*
|
|
|
|
* @ingroup base
|
|
|
|
*/
|
|
|
|
template<typename TClass, typename TResult>
|
2012-07-15 17:15:49 +02:00
|
|
|
class AsyncTask : public Object
|
2012-07-15 10:58:03 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef shared_ptr<AsyncTask<TClass, TResult> > Ptr;
|
|
|
|
typedef weak_ptr<AsyncTask<TClass, TResult> > WeakPtr;
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* A completion callback for an AsyncTask.
|
|
|
|
*/
|
2013-03-15 18:21:29 +01:00
|
|
|
typedef boost::function<void (const shared_ptr<TClass>&)> CompletionCallback;
|
2012-07-15 10:58:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for the AsyncTask class.
|
|
|
|
*/
|
2012-07-15 17:15:49 +02:00
|
|
|
AsyncTask(void)
|
|
|
|
: m_Finished(false), m_ResultRetrieved(false)
|
2012-07-15 10:58:03 +02:00
|
|
|
{ }
|
|
|
|
|
2012-07-15 17:15:49 +02:00
|
|
|
/**
|
|
|
|
* Destructor for the AsyncTask class.
|
|
|
|
*/
|
|
|
|
~AsyncTask(void)
|
|
|
|
{
|
2013-03-12 18:53:44 +01:00
|
|
|
ASSERT(m_Finished);
|
|
|
|
ASSERT(m_ResultRetrieved);
|
2012-07-15 17:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the async task. The caller must hold a reference to the AsyncTask
|
|
|
|
* object until the completion callback is invoked.
|
2013-03-01 12:07:52 +01:00
|
|
|
*
|
|
|
|
* @threadsafety Always.
|
2012-07-15 17:15:49 +02:00
|
|
|
*/
|
2013-02-06 10:53:28 +01:00
|
|
|
void Start(const CompletionCallback& completionCallback = CompletionCallback())
|
2012-07-15 17:15:49 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-01 12:07:52 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
2012-07-15 17:15:49 +02:00
|
|
|
m_CompletionCallback = completionCallback;
|
2013-02-24 01:10:34 +01:00
|
|
|
Utility::QueueAsyncCallback(boost::bind(&AsyncTask<TClass, TResult>::RunInternal, this));
|
2012-07-15 17:15:49 +02:00
|
|
|
}
|
|
|
|
|
2013-01-21 13:47:36 +01:00
|
|
|
/**
|
|
|
|
* Checks whether the task is finished.
|
2013-03-01 12:07:52 +01:00
|
|
|
*
|
|
|
|
* @threadsafety Always.
|
2013-01-21 13:47:36 +01:00
|
|
|
*/
|
|
|
|
bool IsFinished(void) const
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-02-17 19:14:34 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-01-21 13:47:36 +01:00
|
|
|
return m_Finished;
|
|
|
|
}
|
|
|
|
|
2012-07-15 10:58:03 +02:00
|
|
|
/**
|
2013-03-01 12:07:52 +01:00
|
|
|
* Blocks until the task is completed and retrieves the result. Throws an exception if one is stored in
|
2012-07-15 10:58:03 +02:00
|
|
|
* the AsyncTask object.
|
|
|
|
*
|
|
|
|
* @returns The task's result.
|
2013-03-01 12:07:52 +01:00
|
|
|
* @threadsafety Always.
|
2012-07-15 10:58:03 +02:00
|
|
|
*/
|
|
|
|
TResult GetResult(void)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-01 12:07:52 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
while (!m_Finished)
|
|
|
|
m_CV.wait(lock);
|
2012-07-15 10:58:03 +02:00
|
|
|
|
|
|
|
if (m_ResultRetrieved)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("GetResult called on an AsyncTask whose result was already retrieved."));
|
2012-07-15 10:58:03 +02:00
|
|
|
|
2012-08-14 15:06:28 +02:00
|
|
|
m_ResultRetrieved = true;
|
|
|
|
|
2012-07-15 10:58:03 +02:00
|
|
|
if (m_Exception)
|
2012-07-17 20:41:06 +02:00
|
|
|
rethrow_exception(m_Exception);
|
2012-07-15 10:58:03 +02:00
|
|
|
|
2012-07-15 17:15:49 +02:00
|
|
|
TResult result;
|
|
|
|
std::swap(m_Result, result);
|
|
|
|
return result;
|
2012-07-15 10:58:03 +02:00
|
|
|
}
|
|
|
|
|
2012-07-15 17:15:49 +02:00
|
|
|
/**
|
|
|
|
* Finishes the task using an exception.
|
|
|
|
*
|
|
|
|
* @param ex The exception.
|
2013-03-01 12:07:52 +01:00
|
|
|
* @threadsafety Always.
|
2012-07-15 17:15:49 +02:00
|
|
|
*/
|
2012-07-15 17:29:59 +02:00
|
|
|
void FinishException(const boost::exception_ptr& ex)
|
2012-07-15 17:15:49 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-01 12:07:52 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
2012-07-15 17:15:49 +02:00
|
|
|
m_Exception = ex;
|
|
|
|
FinishInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finishes the task using an ordinary result.
|
|
|
|
*
|
|
|
|
* @param result The result.
|
2013-03-01 12:07:52 +01:00
|
|
|
* @threadsafety Always.
|
2012-07-15 17:15:49 +02:00
|
|
|
*/
|
2012-07-15 17:29:59 +02:00
|
|
|
void FinishResult(const TResult& result)
|
2012-07-15 10:58:03 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-01 12:07:52 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
2012-07-15 10:58:03 +02:00
|
|
|
m_Result = result;
|
|
|
|
FinishInternal();
|
|
|
|
}
|
|
|
|
|
2012-07-15 17:15:49 +02:00
|
|
|
protected:
|
2012-09-17 14:47:43 +02:00
|
|
|
/**
|
|
|
|
* Begins executing the task. The Run method must ensure
|
|
|
|
* that one of the Finish*() functions is executed on the task
|
|
|
|
* object (possibly after the Run method has returned).
|
|
|
|
*/
|
2012-07-15 17:15:49 +02:00
|
|
|
virtual void Run(void) = 0;
|
|
|
|
|
2012-07-14 12:44:37 +02:00
|
|
|
private:
|
2012-07-15 17:15:49 +02:00
|
|
|
/**
|
|
|
|
* Finishes the task and causes the completion callback to be invoked. This
|
|
|
|
* function must be called before the object is destroyed.
|
2013-03-01 12:07:52 +01:00
|
|
|
*
|
|
|
|
* @threadsafety Caller must hold m_Mutex.
|
2012-07-15 17:15:49 +02:00
|
|
|
*/
|
|
|
|
void FinishInternal(void)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!m_Finished);
|
2013-03-01 12:07:52 +01:00
|
|
|
m_Finished = true;
|
|
|
|
m_CV.notify_all();
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-02-06 10:53:28 +01:00
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
if (!m_CompletionCallback.empty()) {
|
|
|
|
CompletionCallback callback;
|
2013-02-19 23:02:08 +01:00
|
|
|
m_CompletionCallback.swap(callback);
|
|
|
|
|
|
|
|
Utility::QueueAsyncCallback(boost::bind(callback, GetSelf()));
|
2013-03-01 12:07:52 +01:00
|
|
|
}
|
2012-07-15 17:15:49 +02:00
|
|
|
}
|
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
/**
|
|
|
|
* Calls the Run() method and catches exceptions.
|
|
|
|
*/
|
|
|
|
void RunInternal(void)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Run();
|
2013-03-12 19:49:24 +01:00
|
|
|
} catch (const exception&) {
|
2013-02-24 01:10:34 +01:00
|
|
|
FinishException(boost::current_exception());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
mutable boost::mutex m_Mutex;
|
|
|
|
boost::condition_variable m_CV;
|
2012-07-15 10:58:03 +02:00
|
|
|
CompletionCallback m_CompletionCallback; /**< The completion callback. */
|
|
|
|
TResult m_Result; /**< The task's result. */
|
2012-07-15 17:15:49 +02:00
|
|
|
boost::exception_ptr m_Exception; /**< The task's exception. */
|
|
|
|
|
|
|
|
bool m_Finished; /**< Whether the task is finished. */
|
|
|
|
bool m_ResultRetrieved; /**< Whether the result was retrieved. */
|
2012-07-13 21:00:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ASYNCTASK_H */
|