ticketlock foo

This commit is contained in:
Simon
2025-05-13 14:35:58 +02:00
parent 4a731be0ca
commit 9afe202078
4 changed files with 32 additions and 7 deletions

12
sync/ticketlock.cc Normal file
View File

@@ -0,0 +1,12 @@
#include "sync/ticketlock.h"
void Ticketlock::lock() {
uint64_t ticket = __atomic_fetch_add(&ticket_count, 1, __ATOMIC_SEQ_CST);
while (ticket != ticket_current) {
Core::pause();
}
}
void Ticketlock::unlock() {
__atomic_fetch_add(&ticket_current, 1, __ATOMIC_SEQ_CST);
}

View File

@@ -4,6 +4,8 @@
#pragma once
#include "arch/core.h"
#include "arch/cache.h"
/*! \brief Using Ticketlocks, it is possible to serialize blocks of code
* that might otherwise run in parallel on multiple CPU cores,
* or be interleaved due to interrupts or scheduling.
@@ -29,12 +31,17 @@ class Ticketlock {
Ticketlock(const Ticketlock& copy) = delete;
Ticketlock& operator=(const Ticketlock&) = delete;
private:
volatile uint64_t ticket_current;
volatile uint64_t ticket_count;
public:
/*! \brief Constructor
*
* \todo(12) Complete Constructor (for \MPStuBS)
*/
consteval Ticketlock() {}
consteval Ticketlock() : ticket_current(0), ticket_count(0) {}
/*! \brief Enters the critical area. In case the area is already locked,
* \ref lock() will actively wait until the area can be entered.
@@ -42,11 +49,11 @@ class Ticketlock {
* \see \ref Core::pause()
* \todo(12) Implement Method (for \MPStuBS)
*/
void lock() {}
void lock();
/*! \brief Unblocks the critical area.
*
* \todo(12) Implement Method (for \MPStuBS)
*/
void unlock() {}
void unlock();
};