1

add vorgabe10

This commit is contained in:
2022-07-11 13:29:54 +02:00
parent 2d2eec6934
commit bb0ff5104c
5 changed files with 180 additions and 1 deletions

39
c_os/lib/Semaphore.h Executable file
View File

@ -0,0 +1,39 @@
/*****************************************************************************
* *
* S E M A P H O R E *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Implementierung von Sempahor-Objekten. *
* *
* Autor: Michael Schoettner, 2.9.2016 *
*****************************************************************************/
#ifndef __Semaphore_include__
#define __Semaphore_include__
#include "lib/Queue.h"
#include "lib/SpinLock.h"
class Semaphore {
private:
Semaphore(const Semaphore& copy) = delete; // Verhindere Kopieren
// Queue fuer wartende Threads.
Queue waitQueue;
SpinLock lock;
int counter;
public:
// Konstruktor: Initialisieren des Semaphorzaehlers
Semaphore(int c) : counter(c) {}
// 'Passieren': Warten auf das Freiwerden eines kritischen Abschnitts.
void p();
// 'Vreigeben': Freigeben des kritischen Abschnitts.
void v();
};
#endif

63
c_os/lib/SpinLock.cc Normal file
View File

@ -0,0 +1,63 @@
/*****************************************************************************
* *
* S P I N L O C K *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Implementierung eines Spinlocks mithilfe der cmpxchg *
* Instruktion. *
* *
* Autor: Michael Schoettner, 2.2.2018 *
*****************************************************************************/
#include "lib/SpinLock.h"
#include "kernel/Globals.h"
/*****************************************************************************
* Methode: CAS *
*---------------------------------------------------------------------------*
* Parameter: *ptr Adresse der Variable des Locks *
* old Wert gegen den verglichen wird *
* _new Wert der gesetzt werden soll *
* *
* Beschreibung: Semantik der Funktion CAS = Cmompare & Swap: *
* if old == *ptr then *
* *ptr := _new *
* return prev *
*****************************************************************************/
static inline unsigned long CAS(unsigned long* ptr, unsigned long old, unsigned long _new) {
unsigned long prev;
/*
AT&T/UNIX assembly syntax
The 'volatile' keyword after 'asm' indicates that the instruction
has important side-effects. GCC will not delete a volatile asm if
sit is reachable.
*/
asm volatile("lock;" // prevent race conditions with other cores
"cmpxchg %1, %2;" // %1 = _new; %2 = *ptr
// constraints
: "=a"(prev) // output: =a: RAX -> prev (%0))
: "r"(_new), "m"(*ptr), "a"(old) // input = %1, %2, %3 (r=register, m=memory, a=accumlator = eax
: "memory"); // ensures assembly block will not be moved by gcc
return prev; // return pointer instead of prev to prevent unnecessary second call
}
/*****************************************************************************
* Methode: SpinLock::acquire *
*---------------------------------------------------------------------------*
* Beschreibung: Lock belegen. *
*****************************************************************************/
void SpinLock::acquire() {
while (CAS(ptr, 0, 1) != 0) {}
}
/*****************************************************************************
* Methode: SpinLock::release *
*---------------------------------------------------------------------------*
* Beschreibung: Lock freigeben. *
*****************************************************************************/
void SpinLock::release() {
lock = 0;
}

31
c_os/lib/SpinLock.h Normal file
View File

@ -0,0 +1,31 @@
/*****************************************************************************
* *
* S P I N L O C K *
* *
*---------------------------------------------------------------------------*
* Beschreibung: Implementierung eines Spinlocks mithilfe der cmpxchg *
* Instruktion. *
* *
* Autor: Michael Schoettner, 2.2.2018 *
*****************************************************************************/
#ifndef __SpinLock_include__
#define __SpinLock_include__
class SpinLock {
private:
SpinLock(const SpinLock& copy) = delete; // Verhindere Kopieren
unsigned long lock;
unsigned long* ptr;
public:
SpinLock() : lock(0), ptr(&lock) {}
void acquire();
void release();
};
#endif