Simplify base/semaphore.h, fix #1453

This commit is contained in:
Norman Feske 2015-03-16 16:44:11 +01:00 committed by Christian Helmuth
parent d841fbb82e
commit 001b069509
2 changed files with 17 additions and 95 deletions

View File

@ -16,96 +16,27 @@
#define _INCLUDE__BASE__SEMAPHORE_H_
#include <base/lock.h>
#include <util/list.h>
#include <util/fifo.h>
namespace Genode {
struct Semaphore_queue;
class Fifo_semaphore_queue;
template <typename, typename> class Semaphore_template;
}
namespace Genode { class Semaphore; }
/**
* Semaphore queue interface
*/
struct Genode::Semaphore_queue
{
/**
* Semaphore-queue elements
*
* A queue element represents a thread blocking on the
* semaphore.
*/
class Element : Lock
{
public:
/**
* Constructor
*/
Element() : Lock(LOCKED) { }
void block() { lock(); }
void wake_up() { unlock(); }
};
/**
* Add new queue member that is going to block
*/
void enqueue(Element *e);
/**
* Dequeue queue member to wake up next
*/
Element *dequeue();
};
/**
* First-in-first-out variant of the semaphore-queue interface
*/
class Genode::Fifo_semaphore_queue : public Semaphore_queue
{
public:
class Element : public Semaphore_queue::Element,
public Fifo<Element>::Element { };
private:
Fifo<Element> _fifo;
public:
void enqueue(Element *e) { _fifo.enqueue(e); }
Element *dequeue() { return _fifo.dequeue(); }
};
/**
* Semaphore base template
*
* \param QT semaphore wait queue type implementing the
* 'Semaphore_queue' interface
* \param QTE wait-queue element type implementing the
* 'Semaphore_queue::Element' interface
*
* The queuing policy is defined via the QT and QTE types.
* This way, the platform-specific semaphore-queueing policies
* such as priority-sorted queueing can be easily supported.
*/
template <typename QT, typename QTE>
class Genode::Semaphore_template
class Genode::Semaphore
{
protected:
int _cnt;
Lock _meta_lock;
QT _queue;
struct Element : Fifo<Element>::Element
{
Lock lock { Lock::LOCKED };
void block() { lock.lock(); }
void wake_up() { lock.unlock(); }
};
Fifo<Element> _queue;
public:
@ -114,9 +45,9 @@ class Genode::Semaphore_template
*
* \param n initial counter value of the semphore
*/
Semaphore_template(int n = 0) : _cnt(n) { }
Semaphore(int n = 0) : _cnt(n) { }
~Semaphore_template()
~Semaphore()
{
/* synchronize destruction with unfinished 'up()' */
try { _meta_lock.lock(); } catch (...) { }
@ -133,7 +64,7 @@ class Genode::Semaphore_template
* Remove element from queue and wake up the corresponding
* blocking thread
*/
Semaphore_queue::Element * element = _queue.dequeue();
Element * element = _queue.dequeue();
if (element)
element->wake_up();
}
@ -148,7 +79,7 @@ class Genode::Semaphore_template
* Create semaphore queue element representing the thread
* in the wait queue.
*/
QTE queue_element;
Element queue_element;
_queue.enqueue(&queue_element);
_meta_lock.unlock();
@ -170,13 +101,4 @@ class Genode::Semaphore_template
int cnt() { return _cnt; }
};
namespace Genode {
/**
* Semaphore with default behaviour
*/
typedef Semaphore_template<Fifo_semaphore_queue, Fifo_semaphore_queue::Element> Semaphore;
}
#endif /* _INCLUDE__BASE__SEMAPHORE_H_ */

View File

@ -82,7 +82,7 @@ class Genode::Timed_semaphore : public Semaphore
{
private:
typedef Fifo_semaphore_queue::Element Element;
typedef Semaphore::Element Element;
/**
* Aborts blocking on the semaphore, raised when a timeout occured.