You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2 KiB
C++

/*! \file
* \brief \ref Bellringer that manages and activates time-triggered activities.
*/
#pragma once
#include "../object/queue.h"
#include "../thread/thread.h"
#include "../types.h"
struct Vault;
/*! \brief Manages and activates time-triggered activities.
* \ingroup ipc
*
* The Bellringer is regularly activated and checks whether any of the bells
* should ring. The bells are stored in a Queue<Bell> that is managed by the
* Bellringer. A clever implementation avoids iterating through the whole list
* for every iteration by keeping the bells sorted and storing delta times. This
* approach leads to a complexity of O(1) for the method called by the timer
* interrupt in case no bells need to be rung.
*/
class Bellringer {
// Prevent copies and assignments
Bellringer(const Bellringer&) = delete;
Bellringer& operator=(const Bellringer&) = delete;
struct Bell {
// link pointer to the next bell in the bellringers bell list
Bell* queue_link = nullptr;
Thread* thread;
size_t counter;
};
/*! \brief List of bells currently managed.
*
* This list contains non-expired bells enqueued by job().
* These bells will be checked on every call to check().
*
* All elements that should be inserted into a Queue instance
* are required to be derived from Queue<Bell>::Node.
*/
Queue<Bell> bells;
public:
// constructor
Bellringer() : bells() {}
/*! \brief Checks whether there are bells to be rung.
*
* Every call to check elapses a tick. Once such a tick reduces a bells
* remaining time to zero, the bell will be rung.
*
* \param vault The vault containing the bellringer instance
*
* \todo(16) Implement Method
*/
void check(Vault& vault);
/*! \brief Passes a `bell` to the bellringer to be rung after `ms`
* milliseconds.
*
* \param vault The vault containing the bellringer instance
* \param ms Number of milliseconds that should be waited before
* ringing the bell
*
* \todo(16) Implement Method
*/
void sleep(Vault& vault, unsigned int ms);
/*! \brief Checks whether there are enqueued bells.
* \return true if there are enqueued bells, false otherwise
*
* \todo(16) Implement Method
*/
bool bellPending() const;
};