2013-02-18 13:29:48 +01:00
|
|
|
/*
|
|
|
|
* \brief Platform-independent part of signal framework
|
|
|
|
* \author Norman Feske
|
|
|
|
* \author Christian Prochaska
|
|
|
|
* \author Martin Stein
|
|
|
|
* \date 2013-02-21
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2013-2017 Genode Labs GmbH
|
2013-02-18 13:29:48 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2013-02-18 13:29:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Genode includes */
|
2016-01-14 13:22:00 +01:00
|
|
|
#include <base/env.h>
|
2013-02-18 13:29:48 +01:00
|
|
|
#include <base/signal.h>
|
2016-01-14 13:22:00 +01:00
|
|
|
#include <base/trace/events.h>
|
2019-01-30 17:53:16 +01:00
|
|
|
#include <deprecated/env.h>
|
2013-02-18 13:29:48 +01:00
|
|
|
|
|
|
|
using namespace Genode;
|
|
|
|
|
|
|
|
|
|
|
|
/************
|
|
|
|
** Signal **
|
|
|
|
************/
|
|
|
|
|
2013-10-15 11:12:29 +02:00
|
|
|
Signal::Signal(Signal const &other)
|
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 15:42:15 +01:00
|
|
|
:
|
|
|
|
_data(other._data.context, other._data.num)
|
2013-10-15 11:12:29 +02:00
|
|
|
{
|
|
|
|
_inc_ref();
|
|
|
|
}
|
2013-02-18 13:29:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
Signal & Signal::operator=(Signal const &other)
|
|
|
|
{
|
2017-11-30 09:16:08 +01:00
|
|
|
bool const same_context = (_data.context == other._data.context);
|
2013-02-18 13:29:48 +01:00
|
|
|
|
2017-11-30 09:16:08 +01:00
|
|
|
/* don't change ref cnt if it's the same context */
|
|
|
|
if (!same_context)
|
|
|
|
_dec_ref_and_unlock();
|
2013-02-18 13:29:48 +01:00
|
|
|
|
|
|
|
_data.context = other._data.context;
|
|
|
|
_data.num = other._data.num;
|
|
|
|
|
2017-11-30 09:16:08 +01:00
|
|
|
if (!same_context)
|
|
|
|
_inc_ref();
|
2013-02-18 13:29:48 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-23 21:58:29 +01:00
|
|
|
Signal::~Signal() { _dec_ref_and_unlock(); }
|
|
|
|
|
|
|
|
|
2015-11-13 15:49:11 +01:00
|
|
|
void Signal::_dec_ref_and_unlock()
|
|
|
|
{
|
|
|
|
if (_data.context) {
|
|
|
|
Lock::Guard lock_guard(_data.context->_lock);
|
|
|
|
_data.context->_ref_cnt--;
|
|
|
|
if (_data.context->_ref_cnt == 0)
|
|
|
|
_data.context->_destroy_lock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Signal::_inc_ref()
|
|
|
|
{
|
|
|
|
if (_data.context) {
|
|
|
|
Lock::Guard lock_guard(_data.context->_lock);
|
|
|
|
_data.context->_ref_cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Signal::Signal(Signal::Data data) : _data(data)
|
|
|
|
{
|
|
|
|
if (_data.context) {
|
2017-02-15 11:24:43 +01:00
|
|
|
_data.context->_ref_cnt++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Defer the destruction of the context until the handling of the
|
|
|
|
* 'Signal' has completed.
|
|
|
|
*
|
|
|
|
* Normally, the context can only have one 'Signal' in flight, which is
|
|
|
|
* destroyed before 'pending_signal' is called the next time. However,
|
|
|
|
* one exception is a signal handler that unexpectedly calls
|
2017-04-03 10:45:51 +02:00
|
|
|
* 'pending_signal' itself (i.e., via 'wait_and_dispatch_one_io_signal').
|
2017-02-15 11:24:43 +01:00
|
|
|
* As this is dangerous programming pattern (that should be fixed), we
|
|
|
|
* print a warning.
|
|
|
|
*
|
|
|
|
* In this situation, the context-destroy lock is already taken by the
|
|
|
|
* outer scope. To avoid a deadlock during the attempt to create
|
|
|
|
* a second 'Signal' object for the same context, we take the lock
|
|
|
|
* only in the outer scope (where the context's reference counter
|
|
|
|
* is in its clear state).
|
|
|
|
*/
|
|
|
|
if (_data.context->_ref_cnt == 1) {
|
|
|
|
_data.context->_destroy_lock.lock();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* print warning only once to avoid flooding the log */
|
|
|
|
static bool printed;
|
|
|
|
if (!printed) {
|
|
|
|
warning("attempt to handle the same signal context twice (nested)");
|
|
|
|
printed = true;
|
|
|
|
}
|
|
|
|
}
|
2015-11-13 15:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-23 21:58:29 +01:00
|
|
|
/********************
|
|
|
|
** Signal_context **
|
|
|
|
********************/
|
|
|
|
|
|
|
|
Signal_context::~Signal_context()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Detect bug in an application where a signal context is destroyed prior
|
|
|
|
* dissolving it from the signal receiver.
|
|
|
|
*/
|
|
|
|
if (_receiver)
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
error("Destructing undissolved signal context");
|
2013-11-23 21:58:29 +01:00
|
|
|
}
|
2013-02-18 13:29:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
/************************
|
|
|
|
** Signal_transmitter **
|
|
|
|
************************/
|
|
|
|
|
|
|
|
Signal_transmitter::Signal_transmitter(Signal_context_capability context)
|
|
|
|
: _context(context) { }
|
|
|
|
|
|
|
|
|
|
|
|
void Signal_transmitter::context(Signal_context_capability context) {
|
|
|
|
_context = context; }
|
|
|
|
|
2014-06-18 15:04:30 +02:00
|
|
|
Signal_context_capability Signal_transmitter::context() { return _context; }
|
|
|
|
|
2013-02-18 13:29:48 +01:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
** Signal_receiver **
|
|
|
|
*********************/
|
|
|
|
|
2015-11-13 15:49:11 +01:00
|
|
|
Signal Signal_receiver::wait_for_signal()
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
return pending_signal();
|
|
|
|
} catch (Signal_not_pending) { }
|
2017-01-31 14:08:00 +01:00
|
|
|
|
|
|
|
/* block until the receiver has received a signal */
|
|
|
|
block_for_signal();
|
2015-11-13 15:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Signal Signal_receiver::pending_signal()
|
|
|
|
{
|
2017-11-07 14:04:46 +01:00
|
|
|
Lock::Guard contexts_lock_guard(_contexts_lock);
|
|
|
|
Signal::Data result;
|
|
|
|
_contexts.for_each_locked([&] (Signal_context &context) {
|
2015-11-13 15:49:11 +01:00
|
|
|
|
2017-11-07 14:04:46 +01:00
|
|
|
if (!context._pending) return;
|
2015-11-13 15:49:11 +01:00
|
|
|
|
2017-11-07 14:04:46 +01:00
|
|
|
_contexts.head(context._next);
|
|
|
|
context._pending = false;
|
|
|
|
result = context._curr_signal;
|
|
|
|
context._curr_signal = Signal::Data(0, 0);
|
2015-11-13 15:49:11 +01:00
|
|
|
|
2017-11-07 14:04:46 +01:00
|
|
|
Trace::Signal_received trace_event(context, result.num);
|
|
|
|
throw Context_ring::Break_for_each();
|
|
|
|
});
|
|
|
|
if (result.context) {
|
2017-12-05 11:46:26 +01:00
|
|
|
Lock::Guard lock_guard(result.context->_lock);
|
2015-11-13 15:49:11 +01:00
|
|
|
if (result.num == 0)
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
warning("returning signal with num == 0");
|
2015-11-13 15:49:11 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normally, we should never arrive at this point because that would
|
|
|
|
* mean, the '_signal_available' semaphore was increased without
|
|
|
|
* registering the signal in any context associated to the receiver.
|
|
|
|
*
|
|
|
|
* However, if a context gets dissolved right after submitting a
|
|
|
|
* signal, we may have increased the semaphore already. In this case
|
|
|
|
* the signal-causing context is absent from the list.
|
|
|
|
*/
|
|
|
|
throw Signal_not_pending();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-18 13:29:48 +01:00
|
|
|
Signal_receiver::~Signal_receiver()
|
|
|
|
{
|
2017-11-07 14:04:46 +01:00
|
|
|
Lock::Guard contexts_lock_guard(_contexts_lock);
|
2013-02-18 13:29:48 +01:00
|
|
|
|
|
|
|
/* disassociate contexts from the receiver */
|
2019-01-29 15:26:17 +01:00
|
|
|
while (Signal_context *context = _contexts.head()) {
|
|
|
|
_platform_begin_dissolve(context);
|
|
|
|
_unsynchronized_dissolve(context);
|
|
|
|
_platform_finish_dissolve(context);
|
|
|
|
}
|
2013-02-18 13:29:48 +01:00
|
|
|
|
2013-09-12 01:25:53 +02:00
|
|
|
_platform_destructor();
|
|
|
|
}
|
2015-11-13 15:49:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
void Signal_receiver::_unsynchronized_dissolve(Signal_context * const context)
|
|
|
|
{
|
|
|
|
/* tell core to stop sending signals referring to the context */
|
2017-01-09 15:18:49 +01:00
|
|
|
env_deprecated()->pd_session()->free_context(context->_cap);
|
2015-11-13 15:49:11 +01:00
|
|
|
|
|
|
|
/* restore default initialization of signal context */
|
2017-04-03 10:45:51 +02:00
|
|
|
context->_receiver = nullptr;
|
2015-11-13 15:49:11 +01:00
|
|
|
context->_cap = Signal_context_capability();
|
|
|
|
|
|
|
|
/* remove context from context list */
|
2017-09-17 12:30:20 +02:00
|
|
|
_contexts.remove(context);
|
2015-11-13 15:49:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Signal_receiver::dissolve(Signal_context *context)
|
|
|
|
{
|
|
|
|
if (context->_receiver != this)
|
|
|
|
throw Context_not_associated();
|
|
|
|
|
2017-11-29 14:43:56 +01:00
|
|
|
{
|
2019-01-29 15:26:17 +01:00
|
|
|
/*
|
|
|
|
* We must adhere to the following lock-taking order:
|
|
|
|
*
|
|
|
|
* 1. Taking the lock for the list of contexts ('_contexts_lock')
|
|
|
|
* 2. Taking the context-registry lock (this happens inside
|
|
|
|
* '_platform_begin_dissolve' on platforms that use such a
|
|
|
|
* registry)
|
|
|
|
* 3. Taking the lock for an individual signal context
|
|
|
|
*/
|
2017-11-29 14:43:56 +01:00
|
|
|
Lock::Guard contexts_lock_guard(_contexts_lock);
|
|
|
|
|
2019-01-29 15:26:17 +01:00
|
|
|
_platform_begin_dissolve(context);
|
|
|
|
|
2017-11-29 14:43:56 +01:00
|
|
|
Lock::Guard context_lock_guard(context->_lock);
|
2015-11-13 15:49:11 +01:00
|
|
|
|
2017-11-29 14:43:56 +01:00
|
|
|
_unsynchronized_dissolve(context);
|
|
|
|
}
|
2015-11-13 15:49:11 +01:00
|
|
|
|
2019-01-29 15:26:17 +01:00
|
|
|
_platform_finish_dissolve(context);
|
|
|
|
|
2015-11-13 15:49:11 +01:00
|
|
|
Lock::Guard context_destroy_lock_guard(context->_destroy_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-07 14:04:46 +01:00
|
|
|
void Signal_receiver::Context_ring::insert_as_tail(Signal_context *re)
|
2017-09-17 12:30:20 +02:00
|
|
|
{
|
|
|
|
if (_head) {
|
2017-11-07 14:04:46 +01:00
|
|
|
re->_prev = _head->_prev;
|
|
|
|
re->_next = _head;
|
|
|
|
_head->_prev = _head->_prev->_next = re;
|
2017-09-17 12:30:20 +02:00
|
|
|
} else {
|
2017-11-07 14:04:46 +01:00
|
|
|
_head = re->_prev = re->_next = re;
|
2017-09-17 12:30:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-07 14:04:46 +01:00
|
|
|
void Signal_receiver::Context_ring::remove(Signal_context const *re)
|
2017-09-17 12:30:20 +02:00
|
|
|
{
|
2017-11-07 14:04:46 +01:00
|
|
|
if (re->_next != re) {
|
|
|
|
if (re == _head) { _head = re->_next; }
|
2017-09-17 12:30:20 +02:00
|
|
|
re->_prev->_next = re->_next;
|
2017-11-07 14:04:46 +01:00
|
|
|
re->_next->_prev = re->_prev;
|
2017-09-17 12:30:20 +02:00
|
|
|
}
|
2017-11-07 14:04:46 +01:00
|
|
|
else { _head = nullptr; }
|
2017-09-17 12:30:20 +02:00
|
|
|
}
|