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
106 lines
2.8 KiB
C++
106 lines
2.8 KiB
C++
/*
|
|
* \brief Nova specific user land "Spin lock" implementation
|
|
* \author Alexander Boettcher
|
|
* \date 2014-02-07
|
|
*/
|
|
|
|
/*
|
|
* Copyright (C) 2014-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__INTERNAL__SPIN_LOCK_H_
|
|
#define _INCLUDE__BASE__INTERNAL__SPIN_LOCK_H_
|
|
|
|
/* Genode includes */
|
|
#include <cpu/atomic.h>
|
|
#include <cpu/memory_barrier.h>
|
|
#include <base/thread.h>
|
|
|
|
/* base-internal includes */
|
|
#include <base/internal/lock_helper.h>
|
|
|
|
|
|
enum State {
|
|
SPINLOCK_LOCKED = 0, SPINLOCK_UNLOCKED = 1, SPINLOCK_CONTENDED = 2,
|
|
};
|
|
|
|
enum { RESERVED_BITS = 12, COUNTER_MASK = 0xFFC };
|
|
|
|
template <typename T>
|
|
static inline void spinlock_lock(volatile T *lock_variable)
|
|
{
|
|
using Genode::cmpxchg;
|
|
|
|
Genode::Thread * myself = Genode::Thread::myself();
|
|
T const tid = myself ? myself->native_thread().ec_sel
|
|
: (Genode::addr_t)Nova::PT_SEL_MAIN_EC;
|
|
|
|
unsigned help_counter = 0;
|
|
|
|
/* sanity check that ec_sel fits into the lock_variable */
|
|
if (tid >= (1 << (sizeof(*lock_variable) * 8 - RESERVED_BITS)))
|
|
nova_die();
|
|
|
|
if (myself) {
|
|
Nova::Utcb * utcb = reinterpret_cast<Nova::Utcb *>(myself->utcb());
|
|
help_counter = utcb->tls & COUNTER_MASK;
|
|
}
|
|
|
|
/* try to get lock */
|
|
do {
|
|
T raw = *lock_variable;
|
|
|
|
if (raw != SPINLOCK_UNLOCKED) {
|
|
if (!(raw & SPINLOCK_CONTENDED))
|
|
/* if it fails - just re-read and retry */
|
|
if (!Genode::cmpxchg(lock_variable, raw, raw | SPINLOCK_CONTENDED))
|
|
continue;
|
|
|
|
/*
|
|
* Donate remaining time slice to help the spinlock holder to
|
|
* pass the critical section.
|
|
*/
|
|
unsigned long const ec = raw >> RESERVED_BITS;
|
|
unsigned long const tls = raw & COUNTER_MASK;
|
|
Nova::ec_ctrl(Nova::EC_DONATE_SC, ec, tls);
|
|
continue;
|
|
}
|
|
} while (!cmpxchg(lock_variable, (T)SPINLOCK_UNLOCKED,
|
|
(tid << RESERVED_BITS) | help_counter | SPINLOCK_LOCKED));
|
|
}
|
|
|
|
|
|
template <typename T>
|
|
static inline void spinlock_unlock(volatile T *lock_variable)
|
|
{
|
|
using Nova::Utcb;
|
|
|
|
Genode::Thread * myself = Genode::Thread::myself();
|
|
Utcb * utcb = myself ? reinterpret_cast<Utcb *>(myself->utcb()) : 0;
|
|
|
|
/* unlock */
|
|
T old;
|
|
do {
|
|
old = *lock_variable;
|
|
} while (!Genode::cmpxchg(lock_variable, old, (T)SPINLOCK_UNLOCKED));
|
|
|
|
/* de-flag time donation help request and set new counter */
|
|
if (utcb) {
|
|
utcb->tls = (((utcb->tls & COUNTER_MASK) + 4) % 4096) & COUNTER_MASK;
|
|
/* take care that compiler generates code that writes tls to memory */
|
|
Genode::memory_barrier();
|
|
}
|
|
|
|
/*
|
|
* If anybody donated time, request kernel for a re-schedule in order that
|
|
* the helper can get its time donation (SC) back.
|
|
*/
|
|
if (old & SPINLOCK_CONTENDED)
|
|
Nova::ec_ctrl(Nova::EC_RESCHEDULE);
|
|
}
|
|
|
|
#endif /* _INCLUDE__BASE__INTERNAL__SPIN_LOCK_H_ */
|