4 * - Semaphore syncronisation primitive
12 * \brief Semaphore typedef
14 typedef struct sSemaphore tSemaphore;
17 * \brief Semaphore structure
20 tShortSpinlock Protector; //!< Protector for the lock strucure
21 const char *ModName; //!< Human-readable module name
22 const char *Name; //!< Human-readable name
23 volatile int Value; //!< Current value
24 volatile int MaxValue; //!< Maximum value (signal will wait if it will go over this)
26 struct sThread *Waiting; //!< Waiting threads
27 struct sThread *LastWaiting; //!< Waiting threads
28 struct sThread *Signaling; //!< Waiting threads (from Semaphore_Signal)
29 struct sThread *LastSignaling; //!< Last waiting thread (from Semaphore_Signal)
33 * \brief Initialise the semaphore
34 * \param Sem Semaphore structure to initialsie
35 * \param InitValue Initial value of the semaphore
36 * \param MaxValue Maximum value for the semaphore
37 * \param Module Module name
38 * \param Name Symbolic name
39 * \note Not always needed, as initialising to 0 is valid, but it is preferred
40 * if all semaphores have \a Name set
42 * \note \a Module and \a Name must be avaliable for as long as the semaphore is used
44 extern void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Module, const char *Name);
46 * \brief Acquire items from the semaphore
47 * \param Sem Semaphore structure to use
48 * \param MaxToTake Maximum number of items to take off the list (if zero, as much as possible is taken)
49 * \return Number of items fetched
50 * \retval 0 Semaphore interrupted (signal/message)
51 * \retval -1 Unspecified error
53 extern int Semaphore_Wait(tSemaphore *Sem, int MaxToTake);
55 * \brief Add an "item" to the semaphore
56 * \param Sem Semaphore to use
57 * \param AmmountToAdd Number of items to add
58 * \return Actual number of items added
59 * \retval 0 Semaphore interrupted
60 * \retval -1 Unspecified error
62 extern int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd);
64 * \brief Get the number of items waiting in a semaphore
65 * \param Sem Semaphore to use
66 * \return Number of waiting items
68 extern int Semaphore_GetValue(tSemaphore *Sem);