From 001b0695092a800d68a4c9078c051f4fd2ff0d3d Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Mon, 16 Mar 2015 16:44:11 +0100 Subject: [PATCH] Simplify base/semaphore.h, fix #1453 --- repos/base/include/base/semaphore.h | 110 ++++---------------------- repos/os/include/os/timed_semaphore.h | 2 +- 2 files changed, 17 insertions(+), 95 deletions(-) diff --git a/repos/base/include/base/semaphore.h b/repos/base/include/base/semaphore.h index 7adeb09435..f0908f0b01 100644 --- a/repos/base/include/base/semaphore.h +++ b/repos/base/include/base/semaphore.h @@ -16,96 +16,27 @@ #define _INCLUDE__BASE__SEMAPHORE_H_ #include -#include #include -namespace Genode { - - struct Semaphore_queue; - class Fifo_semaphore_queue; - - template 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 { }; - - private: - - Fifo _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 -class Genode::Semaphore_template +class Genode::Semaphore { protected: int _cnt; Lock _meta_lock; - QT _queue; + + struct Element : Fifo::Element + { + Lock lock { Lock::LOCKED }; + + void block() { lock.lock(); } + void wake_up() { lock.unlock(); } + }; + + Fifo _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 Semaphore; -} - #endif /* _INCLUDE__BASE__SEMAPHORE_H_ */ diff --git a/repos/os/include/os/timed_semaphore.h b/repos/os/include/os/timed_semaphore.h index b46cd7ce7f..b01b297c77 100644 --- a/repos/os/include/os/timed_semaphore.h +++ b/repos/os/include/os/timed_semaphore.h @@ -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.