#pragma once #include "../object/queue.h" #include "../types.h" /*! \file * \brief \ref Semaphore for synchronization of threads. */ /*! * \defgroup ipc Inter-Process Communication * \brief Communication between threads */ // Forward declarations to break cyclic includes struct Vault; class Thread; /*! \brief Semaphore used for synchronization of threads. * \ingroup ipc * * The class Semaphore implements the concept of counting semaphores. * The waiting list is provided by the base class Waitingroom. */ class Semaphore { // Prevent copies and assignments Semaphore(const Semaphore&) = delete; Semaphore& operator=(const Semaphore&) = delete; static Thread** thread_link(Thread& obj, unsigned link_index); public: /*! \brief Constructor; initialized the counter with provided value `c` * \param c Initial counter value * * \todo(16) Implement Constructor */ explicit Semaphore(unsigned c = 0); /*! \brief Wait for access to the critical area. * * Enter/Wait operation: If the counter is greater than 0, then it is * decremented by one. Otherwise the calling thread will be enqueued * into the Waitingroom and marked as blocked. * * \todo(16) Implement Method */ void p(Vault& vault); /*! \brief Leave the critical area. * * Leave operation: If there are threads in the Waitingroom, wake the * first one; otherwise increment the counter by one. * * \todo(16) Implement Method */ void v(Vault& vault); };