mirror of
https://github.com/genodelabs/genode.git
synced 2024-12-20 22:23:16 +00:00
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:
parent
0ed7367c97
commit
41380ff769
@ -15,7 +15,6 @@
|
||||
#define _CORE__INCLUDE__RPC_CAP_FACTORY_H_
|
||||
|
||||
#include <base/allocator.h>
|
||||
#include <base/lock.h>
|
||||
#include <base/capability.h>
|
||||
|
||||
namespace Genode { class Rpc_cap_factory; }
|
||||
|
@ -12,7 +12,6 @@
|
||||
*/
|
||||
|
||||
/* Genode includes */
|
||||
#include <base/cancelable_lock.h>
|
||||
#include <base/thread.h>
|
||||
#include <cpu/atomic.h>
|
||||
#include <cpu/memory_barrier.h>
|
||||
@ -25,7 +24,7 @@ namespace Fiasco {
|
||||
using namespace Genode;
|
||||
|
||||
|
||||
Cancelable_lock::Cancelable_lock(Cancelable_lock::State initial)
|
||||
Lock::Lock(Lock::State initial)
|
||||
: _state(UNLOCKED), _owner(nullptr)
|
||||
{
|
||||
if (initial == LOCKED)
|
||||
@ -33,28 +32,27 @@ Cancelable_lock::Cancelable_lock(Cancelable_lock::State initial)
|
||||
}
|
||||
|
||||
|
||||
void Cancelable_lock::lock()
|
||||
void Lock::lock()
|
||||
{
|
||||
Applicant myself(Thread::myself());
|
||||
lock(myself);
|
||||
}
|
||||
|
||||
|
||||
void Cancelable_lock::lock(Applicant &myself)
|
||||
void Lock::lock(Applicant &myself)
|
||||
{
|
||||
/*
|
||||
* XXX: How to notice cancel-blocking signals issued when being outside the
|
||||
* 'l4_ipc_sleep' system call?
|
||||
*/
|
||||
while (!Genode::cmpxchg(&_state, UNLOCKED, LOCKED))
|
||||
if (Fiasco::l4_ipc_sleep(Fiasco::l4_ipc_timeout(0, 0, 500, 0)) != L4_IPC_RETIMEOUT)
|
||||
throw Genode::Blocking_canceled();
|
||||
Fiasco::l4_ipc_sleep(Fiasco::l4_ipc_timeout(0, 0, 500, 0));
|
||||
|
||||
_owner = myself;
|
||||
}
|
||||
|
||||
|
||||
void Cancelable_lock::unlock()
|
||||
void Lock::unlock()
|
||||
{
|
||||
_owner = Applicant(nullptr);
|
||||
Genode::memory_barrier();
|
||||
|
@ -14,7 +14,6 @@
|
||||
|
||||
/* Genode includes */
|
||||
#include <base/sleep.h>
|
||||
#include <base/lock.h>
|
||||
|
||||
/* L4/Fiasco includes */
|
||||
namespace Fiasco {
|
||||
|
@ -15,18 +15,18 @@
|
||||
#include <base/mutex.h>
|
||||
#include <hw/assert.h>
|
||||
|
||||
Genode::Cancelable_lock::Cancelable_lock(Genode::Cancelable_lock::State state)
|
||||
Genode::Lock::Lock(Genode::Lock::State state)
|
||||
: _state(state), _owner(nullptr) { }
|
||||
|
||||
|
||||
void Genode::Cancelable_lock::unlock()
|
||||
void Genode::Lock::unlock()
|
||||
{
|
||||
assert(_state == LOCKED);
|
||||
_state = UNLOCKED;
|
||||
}
|
||||
|
||||
|
||||
void Genode::Cancelable_lock::lock()
|
||||
void Genode::Lock::lock()
|
||||
{
|
||||
assert(_state == UNLOCKED);
|
||||
_state = LOCKED;
|
||||
|
@ -352,6 +352,11 @@ inline int lx_sigaction(int signum, void (*handler)(int), bool altstack)
|
||||
struct kernel_sigaction act;
|
||||
act.handler = handler;
|
||||
|
||||
/*
|
||||
* System calls should be restarted on signal occurrence and not return
|
||||
* with EINTR. We therefore set the SA_RESTART flag in signal handlers.
|
||||
*/
|
||||
|
||||
#ifdef _LP64
|
||||
/*
|
||||
* The SA_RESTORER flag is not officially documented, but used internally
|
||||
@ -360,10 +365,10 @@ inline int lx_sigaction(int signum, void (*handler)(int), bool altstack)
|
||||
* when leaving the signal handler and it should call the rt_sigreturn syscall.
|
||||
*/
|
||||
enum { SA_RESTORER = 0x04000000 };
|
||||
act.flags = SA_RESTORER;
|
||||
act.flags = SA_RESTORER | SA_RESTART;
|
||||
act.restorer = lx_restore_rt;
|
||||
#else
|
||||
act.flags = 0;
|
||||
act.flags = SA_RESTART;
|
||||
act.restorer = 0;
|
||||
#endif
|
||||
|
||||
|
@ -14,7 +14,6 @@
|
||||
|
||||
/* Genode includes */
|
||||
#include <base/sleep.h>
|
||||
#include <base/lock.h>
|
||||
|
||||
/* base-internal includes */
|
||||
#include <base/internal/native_thread.h>
|
||||
|
@ -15,7 +15,6 @@
|
||||
#define _CORE__INCLUDE__RPC_CAP_FACTORY_H_
|
||||
|
||||
#include <base/allocator.h>
|
||||
#include <base/lock.h>
|
||||
#include <base/capability.h>
|
||||
|
||||
namespace Genode { class Rpc_cap_factory; }
|
||||
|
@ -15,7 +15,6 @@
|
||||
#define _CORE__INCLUDE__RPC_CAP_FACTORY_H_
|
||||
|
||||
#include <base/allocator.h>
|
||||
#include <base/lock.h>
|
||||
#include <base/capability.h>
|
||||
|
||||
namespace Genode { class Rpc_cap_factory; }
|
||||
|
@ -15,7 +15,6 @@
|
||||
#define _CORE__INCLUDE__RPC_CAP_FACTORY_H_
|
||||
|
||||
#include <base/allocator.h>
|
||||
#include <base/lock.h>
|
||||
#include <base/capability.h>
|
||||
|
||||
namespace Genode { class Rpc_cap_factory; }
|
||||
|
@ -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_ */
|
@ -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_ */
|
||||
|
@ -42,7 +42,7 @@ class Genode::Mutex : Noncopyable
|
||||
{
|
||||
private:
|
||||
|
||||
Lock _lock { };
|
||||
Lock _lock { Lock::UNLOCKED };
|
||||
|
||||
public:
|
||||
|
||||
|
@ -25,7 +25,6 @@
|
||||
#include <pd_session/capability.h>
|
||||
#include <base/allocator.h>
|
||||
#include <base/snprintf.h>
|
||||
#include <base/lock.h>
|
||||
|
||||
namespace Genode {
|
||||
|
||||
|
@ -139,11 +139,6 @@ _ZN6Genode15Alarm_scheduler7discardEPNS_5AlarmE T
|
||||
_ZN6Genode15Alarm_scheduler8scheduleEPNS_5AlarmEm T
|
||||
_ZN6Genode15Alarm_schedulerD1Ev T
|
||||
_ZN6Genode15Alarm_schedulerD2Ev T
|
||||
_ZN6Genode15Cancelable_lock4lockEv T
|
||||
_ZN6Genode15Cancelable_lock6unlockEv T
|
||||
_ZN6Genode15Cancelable_lock9Applicant7wake_upEv T
|
||||
_ZN6Genode15Cancelable_lockC1ENS0_5StateE T
|
||||
_ZN6Genode15Cancelable_lockC2ENS0_5StateE T
|
||||
_ZN6Genode15Connection_baseC1Ev T
|
||||
_ZN6Genode15Connection_baseC2Ev T
|
||||
_ZN6Genode15Signal_receiver12local_submitENS_6Signal4DataE T
|
||||
@ -225,6 +220,9 @@ _ZN6Genode4HeapC2EPNS_13Ram_allocatorEPNS_10Region_mapEmPvm T
|
||||
_ZN6Genode4HeapD0Ev T
|
||||
_ZN6Genode4HeapD1Ev T
|
||||
_ZN6Genode4HeapD2Ev T
|
||||
_ZN6Genode4Lock4lockEv T
|
||||
_ZN6Genode4Lock6unlockEv T
|
||||
_ZN6Genode4LockC1ENS0_5StateE T
|
||||
_ZN6Genode4Slab13any_used_elemEv T
|
||||
_ZN6Genode4Slab5Block11_slab_entryEi T
|
||||
_ZN6Genode4Slab5Block14any_used_entryEv T
|
||||
|
@ -14,7 +14,6 @@
|
||||
#ifndef _CORE__INCLUDE__IRQ_SESSION_COMPONENT_H_
|
||||
#define _CORE__INCLUDE__IRQ_SESSION_COMPONENT_H_
|
||||
|
||||
#include <base/lock.h>
|
||||
#include <base/rpc_server.h>
|
||||
#include <base/rpc_client.h>
|
||||
#include <util/list.h>
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
#include <signal_source/rpc_object.h>
|
||||
#include <base/tslab.h>
|
||||
#include <base/lock.h>
|
||||
#include <base/rpc_client.h>
|
||||
#include <base/rpc_server.h>
|
||||
#include <util/fifo.h>
|
||||
|
@ -15,7 +15,6 @@
|
||||
#include <base/env.h>
|
||||
#include <base/log.h>
|
||||
#include <base/heap.h>
|
||||
#include <base/lock.h>
|
||||
|
||||
using namespace Genode;
|
||||
|
||||
|
@ -12,7 +12,7 @@
|
||||
*/
|
||||
|
||||
/* Genode includes */
|
||||
#include <base/cancelable_lock.h>
|
||||
#include <base/lock.h>
|
||||
#include <cpu/memory_barrier.h>
|
||||
|
||||
/* base-internal includes */
|
||||
@ -37,7 +37,7 @@ static inline bool thread_base_valid(Genode::Thread *thread_base)
|
||||
** Lock applicant **
|
||||
********************/
|
||||
|
||||
void Cancelable_lock::Applicant::wake_up()
|
||||
void Lock::Applicant::wake_up()
|
||||
{
|
||||
if (!thread_base_valid(_thread_base)) return;
|
||||
|
||||
@ -56,17 +56,17 @@ void Cancelable_lock::Applicant::wake_up()
|
||||
}
|
||||
|
||||
|
||||
/*********************
|
||||
** Cancelable lock **
|
||||
*********************/
|
||||
/***************
|
||||
** Lock lock **
|
||||
***************/
|
||||
|
||||
void Cancelable_lock::lock()
|
||||
void Lock::lock()
|
||||
{
|
||||
Applicant myself(Thread::myself());
|
||||
lock(myself);
|
||||
}
|
||||
|
||||
void Cancelable_lock::lock(Applicant &myself)
|
||||
void Lock::lock(Applicant &myself)
|
||||
{
|
||||
spinlock_lock(&_spinlock_state);
|
||||
|
||||
@ -126,38 +126,10 @@ void Cancelable_lock::lock(Applicant &myself)
|
||||
* ! thread_yield();
|
||||
*/
|
||||
thread_stop_myself(myself.thread_base());
|
||||
|
||||
/*
|
||||
* We expect to be the lock owner when woken up. If this is not
|
||||
* the case, the blocking was canceled via core's cancel-blocking
|
||||
* mechanism. We have to dequeue ourself from the list of applicants
|
||||
* and reflect this condition as a C++ exception.
|
||||
*/
|
||||
spinlock_lock(&_spinlock_state);
|
||||
if (_owner != myself) {
|
||||
/*
|
||||
* Check if we are the applicant to be waken up next,
|
||||
* otherwise, go through the list of remaining applicants
|
||||
*/
|
||||
for (Applicant *a = &_owner; a; a = a->applicant_to_wake_up()) {
|
||||
/* remove reference to ourself from the applicants list */
|
||||
if (a->applicant_to_wake_up() == &myself) {
|
||||
a->applicant_to_wake_up(myself.applicant_to_wake_up());
|
||||
if (_last_applicant == &myself)
|
||||
_last_applicant = a;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
spinlock_unlock(&_spinlock_state);
|
||||
|
||||
throw Blocking_canceled();
|
||||
}
|
||||
spinlock_unlock(&_spinlock_state);
|
||||
}
|
||||
|
||||
|
||||
void Cancelable_lock::unlock()
|
||||
void Lock::unlock()
|
||||
{
|
||||
spinlock_lock(&_spinlock_state);
|
||||
|
||||
@ -190,7 +162,7 @@ void Cancelable_lock::unlock()
|
||||
}
|
||||
|
||||
|
||||
Cancelable_lock::Cancelable_lock(Cancelable_lock::State initial)
|
||||
Lock::Lock(Lock::State initial)
|
||||
:
|
||||
_spinlock_state(SPINLOCK_UNLOCKED),
|
||||
_state(UNLOCKED),
|
||||
|
@ -17,17 +17,12 @@
|
||||
|
||||
void Genode::Mutex::acquire()
|
||||
{
|
||||
while (1) {
|
||||
try {
|
||||
Lock::Applicant myself(Thread::myself());
|
||||
if (_lock.lock_owner(myself))
|
||||
Genode::error("deadlock ahead, mutex=", this, ", return ip=",
|
||||
__builtin_return_address(0));
|
||||
Lock::Applicant myself(Thread::myself());
|
||||
if (_lock.lock_owner(myself))
|
||||
Genode::error("deadlock ahead, mutex=", this, ", return ip=",
|
||||
__builtin_return_address(0));
|
||||
|
||||
_lock.Cancelable_lock::lock(myself);
|
||||
return;
|
||||
} catch (Blocking_canceled) { }
|
||||
}
|
||||
_lock.lock(myself);
|
||||
}
|
||||
|
||||
void Genode::Mutex::release()
|
||||
@ -35,8 +30,7 @@ void Genode::Mutex::release()
|
||||
Lock::Applicant myself(Thread::myself());
|
||||
if (!_lock.lock_owner(myself)) {
|
||||
Genode::error("denied non mutex owner the release, mutex=",
|
||||
this, ", return ip=",
|
||||
__builtin_return_address(0));
|
||||
this, ", return ip=", __builtin_return_address(0));
|
||||
return;
|
||||
}
|
||||
_lock.unlock();
|
||||
|
@ -17,7 +17,6 @@
|
||||
/* Genode includes */
|
||||
#include <file_system/listener.h>
|
||||
#include <util/list.h>
|
||||
#include <base/lock.h>
|
||||
|
||||
|
||||
namespace File_system {
|
||||
|
@ -13,7 +13,6 @@
|
||||
#ifndef _INCLUDE__USB__PACKET_HANDLER_
|
||||
#define _INCLUDE__USB__PACKET_HANDLER_
|
||||
|
||||
#include <base/lock.h>
|
||||
#include <usb_session/connection.h>
|
||||
|
||||
namespace Usb { class Packet_handler; }
|
||||
|
@ -18,8 +18,6 @@ using Vmm::Cpu_base;
|
||||
using Vmm::Cpu;
|
||||
using Vmm::Gic;
|
||||
|
||||
Genode::Lock & Vmm::lock() { static Genode::Lock l {}; return l; }
|
||||
|
||||
|
||||
Cpu_base::System_register::System_register(unsigned op0,
|
||||
unsigned crn,
|
||||
|
@ -26,7 +26,6 @@
|
||||
namespace Vmm {
|
||||
class Vm;
|
||||
class Cpu_base;
|
||||
Genode::Lock & lock();
|
||||
}
|
||||
|
||||
class Vmm::Cpu_base
|
||||
|
Loading…
Reference in New Issue
Block a user