2 * (c) 2012 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3 * economic rights: Technische Universität Dresden (Germany)
4 * This file is part of TUD:OS and distributed under the terms of the
5 * GNU Lesser General Public License 2.1.
6 * Please see the COPYING-LGPL-2.1 file for details.
11 #include <l4/sys/kip.h>
12 #include <l4/re/env.h>
17 * \brief A polling timeout based on the L4Re clock.
19 * This class allows to conveniently add a timeout to a polling loop.
23 * while (device.read(State) & Busy)
30 * Poll_timeout_kipclock timeout(10000);
31 * while (timeout.test(device.read(State) & Busy))
33 * if (timeout.timed_out())
34 * printf("ERROR: Device does not respond.\n");
37 class Poll_timeout_kipclock
41 * \brief Initialise relative timeout in microseconds
42 * \param poll_time_us Polling timeout in microseconds.
44 Poll_timeout_kipclock(unsigned poll_time_us)
50 * \brief (Re-)Set relative timeout in microseconds
51 * \param poll_time_us Polling timeout in microseconds.
53 void set(unsigned poll_time_us)
55 _timeout = l4_kip_clock(l4re_kip()) + poll_time_us;
59 /** \brief Test whether timeout has expired
60 * \param expression Optional expression.
62 * \retval false The timeout has expired or the given expression returned
64 * \retval true The timeout has not expired and the optionally given
65 * expression returns true.
67 bool test(bool expression = true)
72 return _last_check = l4_kip_clock(l4re_kip()) < _timeout;
76 * \brief Query whether timeout has expired
77 * \return Expiry state of timeout
79 bool timed_out() const { return !_last_check; }
82 l4_cpu_time_t _timeout;