genode/repos/base-hw/src/core/platform_thread.h
Norman Feske eba9c15746 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
2018-01-17 12:14:35 +01:00

221 lines
5.2 KiB
C++

/*
* \brief Userland interface for the management of kernel thread-objects
* \author Martin Stein
* \author Stefan Kalkowski
* \date 2012-02-02
*/
/*
* Copyright (C) 2012-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__PLATFORM_THREAD_H_
#define _CORE__PLATFORM_THREAD_H_
/* Genode includes */
#include <ram_session/ram_session.h>
#include <base/thread.h>
/* base-internal includes */
#include <base/internal/native_utcb.h>
/* core includes */
#include <address_space.h>
#include <object.h>
/* kernel includes */
#include <kernel/core_interface.h>
#include <kernel/thread.h>
namespace Genode {
class Pager_object;
class Thread_state;
class Rm_client;
class Platform_thread;
class Platform_pd;
/**
* Userland interface for the management of kernel thread-objects
*/
class Platform_thread : public Kernel_object<Kernel::Thread>
{
/*
* Noncopyable
*/
Platform_thread(Platform_thread const &);
Platform_thread &operator = (Platform_thread const &);
enum { LABEL_MAX_LEN = 32 };
Platform_pd * _pd;
Weak_ptr<Address_space> _address_space { };
Pager_object * _pager;
Native_utcb * _utcb_core_addr { }; /* UTCB addr in core */
Native_utcb * _utcb_pd_addr; /* UTCB addr in pd */
Ram_dataspace_capability _utcb { }; /* UTCB dataspace */
char _label[LABEL_MAX_LEN];
/*
* Wether this thread is the main thread of a program.
* This should be used only after 'join_pd' was called
* or if this is a core thread. For core threads its save
* also without 'join_pd' because '_main_thread' is initialized
* with 0 wich is always true as cores main thread has no
* 'Platform_thread'.
*/
bool _main_thread;
Affinity::Location _location { };
/**
* Common construction part
*/
void _init();
/*
* Check if this thread will attach its UTCB by itself
*/
bool _attaches_utcb_by_itself();
unsigned _priority(unsigned virt_prio)
{
return Cpu_session::scale_priority(Kernel::Cpu_priority::MAX,
virt_prio);
}
public:
/**
* Constructor for core threads
*
* \param label debugging label
* \param utcb virtual address of UTCB within core
*/
Platform_thread(const char * const label, Native_utcb * utcb);
/**
* Constructor for threads outside of core
*
* \param quota CPU quota that shall be granted to the thread
* \param label debugging label
* \param virt_prio unscaled processor-scheduling priority
* \param utcb core local pointer to userland stack
*/
Platform_thread(size_t const quota, const char * const label,
unsigned const virt_prio, Affinity::Location,
addr_t const utcb);
/**
* Destructor
*/
~Platform_thread();
/**
* Join a protection domain
*
* \param pd platform pd object pointer
* \param main_thread wether thread is the first in protection domain
* \param address_space corresponding Genode address space
*
* This function has no effect when called more twice for a
* given thread.
*/
void join_pd(Platform_pd * const pd, bool const main_thread,
Weak_ptr<Address_space> address_space);
/**
* Run this thread
*
* \param ip initial instruction pointer
* \param sp initial stack pointer
*/
int start(void * const ip, void * const sp);
void restart();
/**
* Pause this thread
*/
void pause() { Kernel::pause_thread(kernel_object()); }
/**
* Enable/disable single stepping
*/
void single_step(bool) { }
/**
* Resume this thread
*/
void resume() { Kernel::resume_thread(kernel_object()); }
/**
* Cancel currently blocking operation
*/
void cancel_blocking() {
Kernel::cancel_thread_blocking(kernel_object()); }
/**
* Set CPU quota of the thread to 'quota'
*/
void quota(size_t const quota);
/**
* Get raw thread state
*/
Thread_state state();
/**
* Override raw thread state
*/
void state(Thread_state s);
/**
* Return unique identification of this thread as faulter
*/
unsigned long pager_object_badge() { return (unsigned long)this; }
/**
* Set the executing CPU for this thread
*
* \param location targeted location in affinity space
*/
void affinity(Affinity::Location const & location);
/**
* Get the executing CPU for this thread
*/
Affinity::Location affinity() const;
/**
* Return the address space to which the thread is bound
*/
Weak_ptr<Address_space>& address_space();
/**
* Return execution time consumed by the thread
*/
unsigned long long execution_time() const { return 0; }
/***************
** Accessors **
***************/
char const * label() const { return _label; };
void pager(Pager_object * const pager);
Pager_object * pager();
Platform_pd * pd() const { return _pd; }
Ram_dataspace_capability utcb() const { return _utcb; }
};
}
#endif /* _CORE__PLATFORM_THREAD_H_ */