base: remove Cancelable_lock

- base/cancelable_lock.h becomes base/lock.h
- all members become private within base/lock.h
- solely Mutex and Blockade are friends to use base/lock.h

Fixes #3819
This commit is contained in:
Alexander Boettcher
2020-07-14 10:15:30 +02:00
committed by Christian Helmuth
parent 0ed7367c97
commit 41380ff769
23 changed files with 108 additions and 209 deletions

View File

@ -1,106 +0,0 @@
/*
* \brief Basic locking primitive
* \author Norman Feske
* \date 2006-07-26
*/
/*
* Copyright (C) 2006-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__CANCELABLE_LOCK_H_
#define _INCLUDE__BASE__CANCELABLE_LOCK_H_
#include <base/lock_guard.h>
#include <base/blocking.h>
namespace Genode {
class Thread;
class Cancelable_lock;
class Mutex;
}
class Genode::Cancelable_lock
{
friend class Mutex;
private:
class Applicant
{
private:
Thread *_thread_base;
Applicant *_to_wake_up;
public:
explicit Applicant(Thread *thread_base)
: _thread_base(thread_base), _to_wake_up(0) { }
void applicant_to_wake_up(Applicant *to_wake_up) {
_to_wake_up = to_wake_up; }
Applicant *applicant_to_wake_up() { return _to_wake_up; }
Thread *thread_base() { return _thread_base; }
/**
* Called from previous lock owner
*/
void wake_up();
bool operator == (Applicant &a) { return _thread_base == a.thread_base(); }
bool operator != (Applicant &a) { return _thread_base != a.thread_base(); }
};
/*
* Note that modifications of the applicants queue must be performed
* atomically. Hence, we use the additional spinlock here.
*/
volatile int _spinlock_state = 0;
volatile int _state = 0;
Applicant * volatile _last_applicant = nullptr;
Applicant _owner;
bool lock_owner(Applicant &myself) {
return (_state == LOCKED) && (_owner == myself); }
void lock(Applicant &);
public:
enum State { LOCKED, UNLOCKED };
/**
* Constructor
*/
explicit Cancelable_lock(State initial = UNLOCKED);
/**
* Try to aquire the lock and block while the lock is not free
*
* \throw Genode::Blocking_canceled
*/
void lock();
/**
* Release lock
*/
void unlock();
/**
* Lock guard
*/
typedef Genode::Lock_guard<Cancelable_lock> Guard;
};
#endif /* _INCLUDE__BASE__CANCELABLE_LOCK_H_ */

View File

@ -1,11 +1,11 @@
/*
* \brief Locking primitives
* \brief Basic locking primitive
* \author Norman Feske
* \date 2006-07-26
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
* Copyright (C) 2006-2020 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.
@ -14,31 +14,84 @@
#ifndef _INCLUDE__BASE__LOCK_H_
#define _INCLUDE__BASE__LOCK_H_
#include <base/cancelable_lock.h>
#include <base/lock_guard.h>
#include <base/blocking.h>
namespace Genode { class Lock; }
namespace Genode {
class Lock;
class Thread;
class Mutex;
}
struct Genode::Lock : Cancelable_lock
class Genode::Lock
{
/**
* Constructor
*/
explicit Lock(State initial = UNLOCKED) : Cancelable_lock(initial) { }
friend class Blockade;
friend class Mutex;
void lock()
{
while (1)
try {
Cancelable_lock::lock();
return;
} catch (Blocking_canceled) { }
}
private:
/**
* Lock guard
*/
typedef Lock_guard<Lock> Guard;
class Applicant
{
private:
Thread *_thread_base;
Applicant *_to_wake_up;
public:
explicit Applicant(Thread *thread_base)
: _thread_base(thread_base), _to_wake_up(0) { }
void applicant_to_wake_up(Applicant *to_wake_up) {
_to_wake_up = to_wake_up; }
Applicant *applicant_to_wake_up() { return _to_wake_up; }
Thread *thread_base() { return _thread_base; }
/**
* Called from previous lock owner
*/
void wake_up();
bool operator == (Applicant &a) { return _thread_base == a.thread_base(); }
bool operator != (Applicant &a) { return _thread_base != a.thread_base(); }
};
/*
* Note that modifications of the applicants queue must be performed
* atomically. Hence, we use the additional spinlock here.
*/
volatile int _spinlock_state = 0;
volatile int _state = 0;
Applicant * volatile _last_applicant = nullptr;
Applicant _owner;
bool lock_owner(Applicant &myself) {
return (_state == LOCKED) && (_owner == myself); }
void lock(Applicant &);
enum State { LOCKED, UNLOCKED };
/**
* Constructor
*/
explicit Lock(State);
/**
* Try to acquire the lock and block while the lock is not free.
*/
void lock();
/**
* Release lock
*/
void unlock();
};
#endif /* _INCLUDE__BASE__LOCK_H_ */

View File

@ -42,7 +42,7 @@ class Genode::Mutex : Noncopyable
{
private:
Lock _lock { };
Lock _lock { Lock::UNLOCKED };
public:

View File

@ -25,7 +25,6 @@
#include <pd_session/capability.h>
#include <base/allocator.h>
#include <base/snprintf.h>
#include <base/lock.h>
namespace Genode {