musst semaphore.counter public machen, hasse alles. init läuft nun

This commit is contained in:
user
2025-12-02 16:56:38 +01:00
parent 21fb133146
commit 3afa165ef3
5 changed files with 49 additions and 10 deletions

View File

@@ -20,7 +20,9 @@ struct Vault {
Bellringer bellringer; Bellringer bellringer;
BBuffer<Key, 16> keys; BBuffer<Key, 16> keys;
Semaphore keys_sem; Semaphore keys_sem;
static constexpr int MAX_SEMS =32;
Semaphore sems[MAX_SEMS];
// Ignore this for now, this is for a bonus task // Ignore this for now, this is for a bonus task
Graphics graphics; Graphics graphics;

View File

@@ -25,7 +25,6 @@ class Semaphore {
// Prevent copies and assignments // Prevent copies and assignments
Semaphore(const Semaphore&) = delete; Semaphore(const Semaphore&) = delete;
Semaphore& operator=(const Semaphore&) = delete; Semaphore& operator=(const Semaphore&) = delete;
unsigned counter;
Queue<Thread> waiting; Queue<Thread> waiting;
public: public:
@@ -33,6 +32,7 @@ class Semaphore {
* \param c Initial counter value * \param c Initial counter value
*/ */
explicit Semaphore(unsigned c = 0) : counter(c) {} explicit Semaphore(unsigned c = 0) : counter(c) {}
unsigned counter;
/*! \brief Wait for access to the critical area. /*! \brief Wait for access to the critical area.
* *

View File

@@ -44,11 +44,23 @@ extern "C" size_t syscall_handler(size_t sysnum, size_t p1, size_t p2,
Syscall::Skeleton::test(Guard::enter().vault(), p1, p2, p3, p4, p5); Syscall::Skeleton::test(Guard::enter().vault(), p1, p2, p3, p4, p5);
break; break;
case Syscall::ID::WRITE: case Syscall::ID::WRITE:
Guarded g = Guard::enter(); Guard::enter().vault().kout.setPos((int)p4, (int)p5);
g.vault().kout.setPos((int)p4, (int)p5); Syscall::Skeleton::write(Guard::enter().vault(), p1, (char*)p2, p3);
Syscall::Skeleton::write(g.vault(), p1, (char*)p2, p3);
break; break;
} case Syscall::ID::SEM_INIT:
Syscall::Skeleton::sem_init(Guard::enter().vault(), p1, p2);
break;
case Syscall::ID::SEM_DESTROY:
Syscall::Skeleton::sem_destroy(Guard::enter().vault(), p1);
break;
case Syscall::ID::SEM_WAIT:
Syscall::Skeleton::sem_wait(Guard::enter().vault(), p1);
break;
case Syscall::ID::SEM_SIGNAL:
Syscall::Skeleton::sem_signal(Guard::enter().vault(), p1);
break;
}
return static_cast<size_t>(-1); return static_cast<size_t>(-1);
} }

View File

@@ -46,10 +46,11 @@ void sleep(Vault &vault, size_t ms) {
} }
bool sem_init(Vault &vault, size_t id, uint32_t value) { bool sem_init(Vault &vault, size_t id, uint32_t value) {
(void)vault; if (id >= vault.MAX_SEMS) {
(void)id; return false; // outofrange id
(void)value; }
return false; vault.sems[id].counter=value;
return true;
} }
void sem_destroy(Vault &vault, size_t id) { void sem_destroy(Vault &vault, size_t id) {

View File

@@ -8,6 +8,15 @@ namespace Syscall {
enum class ID : size_t { enum class ID : size_t {
TEST = 0, TEST = 0,
WRITE = 1, WRITE = 1,
//READ = 2,
//SLEEP =3,
SEM_INIT=4,
SEM_DESTROY=5,
SEM_WAIT=6,
SEM_SIGNAL=7,
//SYS_GETPID=8,
//SYS_EXIT= 9,
//SYS_KILL=10
}; };
} // namespace Syscall } // namespace Syscall
@@ -25,5 +34,20 @@ extern "C" ssize_t sys_safe_call(Syscall::ID id, size_t p1, size_t p2,
} }
[[gnu::always_inline]] static inline int write(int fd, const void *buf, size_t len, int x = -1, int y = -1) { [[gnu::always_inline]] static inline int write(int fd, const void *buf, size_t len, int x = -1, int y = -1) {
return sys_call(Syscall::ID::WRITE, fd, (size_t)buf, len, x, y); return sys_call(Syscall::ID::WRITE, fd, (size_t)buf, len, x, y);
}
[[gnu::always_inline]] static inline int sem_init(int fd, int semid, int value) {
return sys_call(Syscall::ID::SEM_INIT, fd, semid, value, 0,0);
}
[[gnu::always_inline]] static inline int sem_destroy(int fd, int semid) {
return sys_call(Syscall::ID::SEM_DESTROY, fd, semid,0,0,0);
}
[[gnu::always_inline]] static inline int sem_wait(int fd, int semid) {
return sys_call(Syscall::ID::SEM_WAIT, fd, semid,0,0,0);
}
[[gnu::always_inline]] static inline int sem_signal(int fd, int semid) {
return sys_call(Syscall::ID::SEM_SIGNAL, fd, semid,0,0,0);
} }