genode/repos/base/include/base/cancelable_lock.h

98 lines
1.9 KiB
C
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Basic locking primitive
* \author Norman Feske
* \date 2006-07-26
*/
/*
2013-01-10 21:44:47 +01:00
* Copyright (C) 2006-2013 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#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_base;
class Cancelable_lock;
}
2011-12-22 16:19:25 +01:00
class Genode::Cancelable_lock
{
private:
2011-12-22 16:19:25 +01:00
class Applicant
{
private:
2011-12-22 16:19:25 +01:00
Thread_base *_thread_base;
Applicant *_to_wake_up;
2011-12-22 16:19:25 +01:00
public:
2011-12-22 16:19:25 +01:00
explicit Applicant(Thread_base *thread_base)
: _thread_base(thread_base), _to_wake_up(0) { }
2011-12-22 16:19:25 +01:00
void applicant_to_wake_up(Applicant *to_wake_up) {
_to_wake_up = to_wake_up; }
2011-12-22 16:19:25 +01:00
Applicant *applicant_to_wake_up() { return _to_wake_up; }
2011-12-22 16:19:25 +01:00
Thread_base *thread_base() { return _thread_base; }
2011-12-22 16:19:25 +01:00
/**
* Called from previous lock owner
*/
void wake_up();
2011-12-22 16:19:25 +01:00
bool operator == (Applicant &a) { return _thread_base == a.thread_base(); }
bool operator != (Applicant &a) { return _thread_base != a.thread_base(); }
};
2011-12-22 16:19:25 +01:00
/*
* Note that modifications of the applicants queue must be performed
* atomically. Hence, we use the additional spinlock here.
*/
2011-12-22 16:19:25 +01:00
volatile int _spinlock_state;
volatile int _state;
2011-12-22 16:19:25 +01:00
Applicant* volatile _last_applicant;
Applicant _owner;
2011-12-22 16:19:25 +01:00
public:
2011-12-22 16:19:25 +01:00
enum State { LOCKED, UNLOCKED };
2011-12-22 16:19:25 +01:00
/**
* Constructor
*/
explicit Cancelable_lock(State initial = UNLOCKED);
2011-12-22 16:19:25 +01:00
/**
* Try to aquire the lock and block while the lock is not free
*
* \throw Genode::Blocking_canceled
*/
void lock();
2011-12-22 16:19:25 +01:00
/**
* Release lock
*/
void unlock();
/**
* Lock guard
*/
typedef Genode::Lock_guard<Cancelable_lock> Guard;
};
2011-12-22 16:19:25 +01:00
#endif /* _INCLUDE__BASE__CANCELABLE_LOCK_H_ */