2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Semaphore implementation with timeout facility
|
|
|
|
* \author Stefan Kalkowski
|
|
|
|
* \date 2010-03-05
|
|
|
|
*
|
|
|
|
* This semaphore implementation allows to block on a semaphore for a
|
|
|
|
* given time instead of blocking indefinetely.
|
|
|
|
*
|
|
|
|
* For the timeout functionality the alarm framework is used.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2010-2017 Genode Labs GmbH
|
2011-12-22 15:19: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.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
#ifndef _INCLUDE__RUMP__TIMED_SEMAPHORE_H_
|
|
|
|
#define _INCLUDE__RUMP__TIMED_SEMAPHORE_H_
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
#include <base/thread.h>
|
|
|
|
#include <base/semaphore.h>
|
2019-01-03 17:01:49 +00:00
|
|
|
#include <base/alarm.h>
|
2011-12-22 15:19:25 +00:00
|
|
|
#include <timer_session/connection.h>
|
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
using Genode::Exception;
|
|
|
|
using Genode::Entrypoint;
|
|
|
|
using Genode::Alarm;
|
|
|
|
using Genode::Alarm_scheduler;
|
|
|
|
using Genode::Semaphore;
|
|
|
|
using Genode::Signal_handler;
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
/**
|
|
|
|
* Exception types
|
|
|
|
*/
|
|
|
|
class Timeout_exception : public Exception { };
|
|
|
|
class Nonblocking_exception : public Exception { };
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2013-07-18 08:43:28 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/**
|
|
|
|
* Alarm thread, which counts jiffies and triggers timeout events.
|
|
|
|
*/
|
2019-01-18 10:40:23 +00:00
|
|
|
class Timeout_entrypoint : private Entrypoint
|
2015-03-04 20:12:14 +00:00
|
|
|
{
|
|
|
|
private:
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
enum { JIFFIES_STEP_MS = 10 };
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
Alarm_scheduler _alarm_scheduler { };
|
2017-05-23 13:10:20 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
Timer::Connection _timer;
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
Signal_handler<Timeout_entrypoint> _timer_handler;
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
void _handle_timer() { _alarm_scheduler.handle(_timer.elapsed_ms()); }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
static Genode::size_t constexpr STACK_SIZE = 2048*sizeof(long);
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 14:42:15 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
Timeout_entrypoint(Genode::Env &env)
|
|
|
|
:
|
|
|
|
Entrypoint(env, STACK_SIZE, "alarm-timer", Genode::Affinity::Location()),
|
|
|
|
_timer(env),
|
|
|
|
_timer_handler(*this, *this, &Timeout_entrypoint::_handle_timer)
|
2015-03-04 20:12:14 +00:00
|
|
|
{
|
2019-01-18 10:40:23 +00:00
|
|
|
_timer.sigh(_timer_handler);
|
2015-03-04 20:12:14 +00:00
|
|
|
_timer.trigger_periodic(JIFFIES_STEP_MS*1000);
|
|
|
|
}
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
Alarm::Time time(void) { return _timer.elapsed_ms(); }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
void schedule_absolute(Alarm &alarm, Alarm::Time timeout)
|
|
|
|
{
|
|
|
|
_alarm_scheduler.schedule_absolute(&alarm, timeout);
|
|
|
|
}
|
2017-05-23 13:10:20 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
void discard(Alarm &alarm) { _alarm_scheduler.discard(&alarm); }
|
2015-03-04 20:12:14 +00:00
|
|
|
};
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/**
|
|
|
|
* Semaphore with timeout on down operation.
|
|
|
|
*/
|
2019-01-18 10:40:23 +00:00
|
|
|
class Timed_semaphore : public Semaphore
|
2015-03-04 20:12:14 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2015-03-16 15:44:11 +00:00
|
|
|
typedef Semaphore::Element Element;
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
Timeout_entrypoint &_timeout_ep;
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/**
|
|
|
|
* Aborts blocking on the semaphore, raised when a timeout occured.
|
|
|
|
*
|
|
|
|
* \param element the waiting-queue element associated with a timeout.
|
2019-01-18 10:40:23 +00:00
|
|
|
* \return true if a thread was aborted/woken up
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 14:42:15 +00:00
|
|
|
bool _abort(Element &element)
|
2015-03-04 20:12:14 +00:00
|
|
|
{
|
2019-01-18 10:40:23 +00:00
|
|
|
Genode::Lock::Guard lock_guard(Semaphore::_meta_lock);
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/* potentially, the queue is empty */
|
|
|
|
if (++Semaphore::_cnt <= 0) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate through the queue and find the thread,
|
|
|
|
* with the corresponding timeout.
|
|
|
|
*/
|
2019-01-29 18:29:03 +00:00
|
|
|
Element *first = nullptr;
|
|
|
|
Semaphore::_queue.dequeue([&first] (Element &e) {
|
|
|
|
first = &e; });
|
2015-03-04 20:12:14 +00:00
|
|
|
Element *e = first;
|
|
|
|
|
2019-01-29 18:29:03 +00:00
|
|
|
while (e) {
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
/*
|
2015-03-04 20:12:14 +00:00
|
|
|
* Wakeup the thread.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 14:42:15 +00:00
|
|
|
if (&element == e) {
|
2015-03-04 20:12:14 +00:00
|
|
|
e->wake_up();
|
|
|
|
return true;
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-03-04 20:12:14 +00:00
|
|
|
* Noninvolved threads are enqueued again.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
2019-01-29 18:29:03 +00:00
|
|
|
Semaphore::_queue.enqueue(*e);
|
|
|
|
e = nullptr;
|
|
|
|
Semaphore::_queue.dequeue([&e] (Element &next) {
|
|
|
|
e = &next; });
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
/*
|
2015-03-04 20:12:14 +00:00
|
|
|
* Maybe, the alarm was triggered just after the corresponding
|
|
|
|
* thread was already dequeued, that's why we have to track
|
|
|
|
* whether we processed the whole queue.
|
|
|
|
*/
|
|
|
|
if (e == first)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/* The right element was not found, so decrease counter again */
|
|
|
|
--Semaphore::_cnt;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 14:42:15 +00:00
|
|
|
* Represents a timeout associated with the blocking
|
2015-03-04 20:12:14 +00:00
|
|
|
* operation on a semaphore.
|
|
|
|
*/
|
|
|
|
class Timeout : public Alarm
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
Timed_semaphore &_sem; /* semaphore we block on */
|
|
|
|
Element &_element; /* queue element timeout belongs to */
|
|
|
|
bool _triggered { false };
|
|
|
|
Time const _start;
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
Timeout(Time start, Timed_semaphore &s, Element &e)
|
|
|
|
: _sem(s), _element(e), _triggered(false), _start(start)
|
|
|
|
{ }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
bool triggered(void) { return _triggered; }
|
|
|
|
Time start() { return _start; }
|
2013-07-18 08:43:28 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
bool on_alarm(unsigned) override
|
|
|
|
{
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 14:42:15 +00:00
|
|
|
_triggered = _sem._abort(_element);
|
2015-03-04 20:12:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* \param n initial counter value of the semphore
|
|
|
|
*/
|
2019-01-18 10:40:23 +00:00
|
|
|
Timed_semaphore(Timeout_entrypoint &timeout_ep, int n = 0)
|
|
|
|
: Semaphore(n), _timeout_ep(timeout_ep) { }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrements semaphore and blocks when it's already zero.
|
|
|
|
*
|
|
|
|
* \param t after t milliseconds of blocking a Timeout_exception is thrown.
|
|
|
|
* if t is zero do not block, instead raise an
|
|
|
|
* Nonblocking_exception.
|
|
|
|
* \return milliseconds the caller was blocked
|
|
|
|
*/
|
|
|
|
Alarm::Time down(Alarm::Time t)
|
|
|
|
{
|
|
|
|
Semaphore::_meta_lock.lock();
|
|
|
|
|
|
|
|
if (--Semaphore::_cnt < 0) {
|
|
|
|
|
|
|
|
/* If t==0 we shall not block */
|
|
|
|
if (t == 0) {
|
|
|
|
++_cnt;
|
2011-12-22 15:19:25 +00:00
|
|
|
Semaphore::_meta_lock.unlock();
|
2019-01-18 10:40:23 +00:00
|
|
|
throw Nonblocking_exception();
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create semaphore queue element representing the thread
|
|
|
|
* in the wait queue.
|
|
|
|
*/
|
|
|
|
Element queue_element;
|
2019-01-29 18:29:03 +00:00
|
|
|
Semaphore::_queue.enqueue(queue_element);
|
2015-03-04 20:12:14 +00:00
|
|
|
Semaphore::_meta_lock.unlock();
|
|
|
|
|
|
|
|
/* Create the timeout */
|
2019-01-18 10:40:23 +00:00
|
|
|
Alarm::Time const curr_time = _timeout_ep.time();
|
|
|
|
Timeout timeout(curr_time, *this, queue_element);
|
|
|
|
_timeout_ep.schedule_absolute(timeout, curr_time + t);
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The thread is going to block on a local lock now,
|
|
|
|
* waiting for getting waked from another thread
|
|
|
|
* calling 'up()'
|
|
|
|
* */
|
|
|
|
queue_element.block();
|
|
|
|
|
|
|
|
/* Deactivate timeout */
|
2019-01-18 10:40:23 +00:00
|
|
|
_timeout_ep.discard(timeout);
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* When we were only woken up, because of a timeout,
|
|
|
|
* throw an exception.
|
|
|
|
*/
|
2019-01-18 10:40:23 +00:00
|
|
|
if (timeout.triggered())
|
|
|
|
throw Timeout_exception();
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/* return blocking time */
|
2019-01-18 10:40:23 +00:00
|
|
|
return _timeout_ep.time() - timeout.start();
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
} else {
|
|
|
|
Semaphore::_meta_lock.unlock();
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
2015-03-04 20:12:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/********************************
|
|
|
|
** Base class implementations **
|
|
|
|
********************************/
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
void down() { Semaphore::down(); }
|
|
|
|
void up() { Semaphore::up(); }
|
|
|
|
};
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2019-01-18 10:40:23 +00:00
|
|
|
#endif /* _INCLUDE__RUMP__TIMED_SEMAPHORE_H_ */
|