[Zion] Add a semaphore primitive with related syscalls.
This commit is contained in:
parent
2df1f6c006
commit
da3901e104
9 changed files with 107 additions and 1 deletions
|
|
@ -15,6 +15,7 @@ class KernelObject : public glcr::RefCounted<KernelObject> {
|
|||
ENDPOINT = 0x7,
|
||||
REPLY_PORT = 0x8,
|
||||
MUTEX = 0x9,
|
||||
SEMAPHORE = 0x10,
|
||||
};
|
||||
|
||||
virtual uint64_t TypeTag() = 0;
|
||||
|
|
|
|||
29
zion/object/semaphore.cpp
Normal file
29
zion/object/semaphore.cpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include "object/semaphore.h"
|
||||
|
||||
#include "scheduler/scheduler.h"
|
||||
|
||||
glcr::RefPtr<Semaphore> Semaphore::Create() {
|
||||
return glcr::AdoptPtr<Semaphore>(new Semaphore());
|
||||
}
|
||||
|
||||
// FIXME: We almost certainly have some race conditions
|
||||
// between this and unlock where we could end up with
|
||||
// a thread in the blocked_threads_ queue while noone is holding the lock.
|
||||
void Semaphore::Signal() {
|
||||
__atomic_fetch_add(&lock_, 0x1, __ATOMIC_SEQ_CST);
|
||||
if (blocked_threads_.size() > 0) {
|
||||
auto thread = blocked_threads_.PopFront();
|
||||
thread->SetState(Thread::RUNNABLE);
|
||||
gScheduler->Enqueue(thread);
|
||||
}
|
||||
}
|
||||
|
||||
void Semaphore::Wait() {
|
||||
while (lock_ == 0) {
|
||||
auto thread = gScheduler->CurrentThread();
|
||||
thread->SetState(Thread::BLOCKED);
|
||||
blocked_threads_.PushBack(thread);
|
||||
gScheduler->Yield();
|
||||
}
|
||||
__atomic_fetch_sub(&lock_, 0x1, __ATOMIC_SEQ_CST);
|
||||
}
|
||||
33
zion/object/semaphore.h
Normal file
33
zion/object/semaphore.h
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/container/intrusive_list.h>
|
||||
#include <glacier/memory/ref_ptr.h>
|
||||
|
||||
#include "include/ztypes.h"
|
||||
#include "object/kernel_object.h"
|
||||
#include "object/thread.h"
|
||||
|
||||
class Semaphore;
|
||||
|
||||
template <>
|
||||
struct KernelObjectTag<Semaphore> {
|
||||
static const uint64_t type = KernelObject::SEMAPHORE;
|
||||
};
|
||||
|
||||
class Semaphore : public KernelObject {
|
||||
public:
|
||||
uint64_t TypeTag() override { return KernelObject::SEMAPHORE; }
|
||||
static uint64_t DefaultPermissions() {
|
||||
return kZionPerm_Wait | kZionPerm_Signal;
|
||||
}
|
||||
|
||||
static glcr::RefPtr<Semaphore> Create();
|
||||
|
||||
void Wait();
|
||||
void Signal();
|
||||
|
||||
private:
|
||||
uint8_t lock_ = 0;
|
||||
|
||||
glcr::IntrusiveList<Thread> blocked_threads_;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue