Files
genode/repos/base/include/base/duration.h
Norman Feske bf62d6b896 Move timer from os to base repository
Since the timer and timeout handling is part of the base library (the
dynamic linker), it belongs to the base repository.

Besides moving the timer and its related infrastructure (alarm, timeout
libs, tests) to the base repository, this patch also moves the timer
from the 'drivers' subdirectory directly to 'src' and disamibuates the
timer's build locations for the various kernels. Otherwise the different
timer implementations could interfere with each other when using one
build directory with multiple kernels.

Note that this patch changes the include paths for the former os/timer,
os/alarm.h, os/duration.h, and os/timed_semaphore.h to base/.

Issue #3101
2019-01-14 12:33:57 +01:00

119 lines
2.4 KiB
C++

/*
* \brief A duration type for both highly precise and long durations
* \author Martin Stein
* \date 2017-03-21
*/
/*
* Copyright (C) 2016-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _INCLUDE__BASE__DURATION_H_
#define _INCLUDE__BASE__DURATION_H_
/* Genode includes */
#include <base/stdint.h>
#include <base/exception.h>
#include <base/output.h>
namespace Genode {
class Microseconds;
class Milliseconds;
class Duration;
}
/**
* Makes it clear that a given integer value stands for microseconds
*/
struct Genode::Microseconds
{
unsigned long value;
explicit Microseconds(unsigned long value) : value(value) { }
void print(Output &out) const
{
Genode::print(out, value);
out.out_string(" us");
}
};
/**
* Makes it clear that a given integer value stands for milliseconds
*/
struct Genode::Milliseconds
{
unsigned long value;
explicit Milliseconds(unsigned long value) : value(value) { }
void print(Output &out) const
{
Genode::print(out, value);
out.out_string(" ms");
}
};
/**
* A duration type that combines high precision and large intervals
*/
struct Genode::Duration
{
public:
struct Overflow : Exception { };
private:
enum { US_PER_MS = 1000UL };
enum { MS_PER_HOUR = 1000UL * 60 * 60 };
enum { US_PER_HOUR = 1000UL * 1000 * 60 * 60 };
unsigned long _microseconds { 0 };
unsigned long _hours { 0 };
void _add_us_less_than_an_hour(unsigned long us);
void _raise_hours(unsigned long hours);
public:
void add(Microseconds us);
void add(Milliseconds ms);
bool less_than(Duration const &other) const;
explicit Duration(Milliseconds ms) { add(ms); }
explicit Duration(Microseconds us) { add(us); }
Microseconds trunc_to_plain_us() const;
Milliseconds trunc_to_plain_ms() const;
};
namespace Genode
{
static inline
Microseconds min(Microseconds const x, Microseconds const y) {
return (x.value < y.value) ? x : y; }
static inline
Microseconds max(Microseconds const x, Microseconds const y) {
return (x.value > y.value) ? x : y; }
static inline
Milliseconds min(Milliseconds const x, Milliseconds const y) {
return (x.value < y.value) ? x : y; }
static inline
Milliseconds max(Milliseconds const x, Milliseconds const y) {
return (x.value > y.value) ? x : y; }
};
#endif /* _INCLUDE__BASE__DURATION_H_ */