genode/repos/base/src/core/include/signal_broker.h
Norman Feske 6b289a1423 base/core: use references instead of pointers
This patch replaces the former prominent use of pointers by references
wherever feasible. This has the following benefits:

* The contract between caller and callee becomes more obvious. When
  passing a reference, the contract says that the argument cannot be
  a null pointer. The caller is responsible to ensure that. Therefore,
  the use of reference eliminates the need to add defensive null-pointer
  checks at the callee site, which sometimes merely exist to be on the
  safe side. The bottom line is that the code becomes easier to follow.

* Reference members must be initialized via an object initializer,
  which promotes a programming style that avoids intermediate object-
  construction states. Within core, there are still a few pointers
  as member variables left though. E.g., caused by the late association
  of 'Platform_thread' objects with their 'Platform_pd' objects.

* If no pointers are present as member variables, we don't need to
  manually provide declarations of a private copy constructor and
  an assignment operator to avoid -Weffc++ errors "class ... has
  pointer data members [-Werror=effc++]".

This patch also changes a few system bindings on NOVA and Fiasco.OC,
e.g., the return value of the global 'cap_map' accessor has become a
reference. Hence, the patch touches a few places outside of core.

Fixes #3135
2019-02-12 10:33:13 +01:00

111 lines
2.7 KiB
C++

/*
* \brief Mechanism to deliver signals via core
* \author Norman Feske
* \date 2016-01-04
*/
/*
* 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 _CORE__INCLUDE__SIGNAL_BROKER_H_
#define _CORE__INCLUDE__SIGNAL_BROKER_H_
#include <signal_source_component.h>
#include <signal_source/capability.h>
#include <signal_context_slab.h>
#include <signal_delivery_proxy.h>
namespace Genode { class Signal_broker; }
class Genode::Signal_broker
{
private:
Allocator &_md_alloc;
Rpc_entrypoint &_source_ep;
Rpc_entrypoint &_context_ep;
Signal_source_component _source;
Signal_source_capability _source_cap;
Signal_context_slab _contexts_slab { _md_alloc };
Signal_delivery_proxy_component _delivery_proxy { _source_ep };
public:
class Invalid_signal_source : public Exception { };
Signal_broker(Allocator &md_alloc,
Rpc_entrypoint &source_ep,
Rpc_entrypoint &context_ep)
:
_md_alloc(md_alloc),
_source_ep(source_ep),
_context_ep(context_ep),
_source(_context_ep),
_source_cap(_source_ep.manage(&_source))
{ }
~Signal_broker()
{
/* remove source from entrypoint */
_source_ep.dissolve(&_source);
/* free all signal contexts */
while (Signal_context_component *r = _contexts_slab.any_signal_context())
free_context(r->cap());
}
Signal_source_capability alloc_signal_source() { return _source_cap; }
void free_signal_source(Signal_source_capability) { }
Signal_context_capability
alloc_context(Signal_source_capability, unsigned long imprint)
{
/*
* XXX For now, we ignore the signal-source argument as we
* create only a single receiver for each PD.
*/
Signal_context_component &context = *new (&_contexts_slab)
Signal_context_component(imprint, _source);
return _context_ep.manage(&context);
}
void free_context(Signal_context_capability context_cap)
{
Signal_context_component *context = nullptr;
_context_ep.apply(context_cap, [&] (Signal_context_component *c) {
if (!c) {
warning("specified signal-context capability has wrong type");
return;
}
context = c;
_context_ep.dissolve(context);
});
if (!context)
return;
/* release solely in context of context_ep thread */
if (context->enqueued() && !_context_ep.is_myself())
_delivery_proxy.release(*context);
destroy(&_contexts_slab, context);
}
void submit(Signal_context_capability const cap, unsigned const cnt)
{
_delivery_proxy.submit(cap, cnt);
}
};
#endif /* _CORE__INCLUDE__SIGNAL_BROKER_H_ */