2015-05-19 11:57:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Utility to execute a function repeatedly
|
|
|
|
* \author Norman Feske
|
|
|
|
* \author Stefan Kalkowski
|
|
|
|
* \date 2015-04-29
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2015-2017 Genode Labs GmbH
|
2015-05-19 11:57:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2015-05-19 11:57:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__UTIL__RETRY_H_
|
|
|
|
#define _INCLUDE__UTIL__RETRY_H_
|
|
|
|
|
|
|
|
namespace Genode {
|
|
|
|
|
2024-05-22 14:35:38 +00:00
|
|
|
template <typename EXC>
|
|
|
|
auto retry(auto const &fn, auto const &,
|
|
|
|
unsigned attempts = ~0U) -> decltype(fn());
|
2015-05-19 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Repeatedly try to execute a function 'func'
|
|
|
|
*
|
|
|
|
* If the function 'func' throws an exception of type 'EXC', the 'handler'
|
|
|
|
* is called and the function call is retried.
|
|
|
|
*
|
|
|
|
* \param EXC exception type to handle
|
2024-05-22 14:35:38 +00:00
|
|
|
* \param fn functor to execute
|
|
|
|
* \param exc_fn exception handler executed if 'fn' raised an exception
|
2015-05-19 11:57:25 +00:00
|
|
|
* of type 'EXC'
|
|
|
|
* \param attempts number of attempts to execute 'func' before giving up
|
|
|
|
* and reflecting the exception 'EXC' to the caller. If not
|
|
|
|
* specified, attempt infinitely.
|
|
|
|
*/
|
2024-05-22 14:35:38 +00:00
|
|
|
template <typename EXC>
|
|
|
|
auto Genode::retry(auto const &fn, auto const &exc_fn,
|
|
|
|
unsigned attempts) -> decltype(fn())
|
2015-05-19 11:57:25 +00:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; attempts == ~0U || i < attempts; i++)
|
2024-05-22 14:35:38 +00:00
|
|
|
try { return fn(); }
|
|
|
|
catch (EXC) { exc_fn(); }
|
2015-05-19 11:57:25 +00:00
|
|
|
|
|
|
|
throw EXC();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__UTIL__RETRY_H_ */
|