[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
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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue