mirror of
https://github.com/Atmosphere-NX/Atmosphere-libs.git
synced 2025-06-29 14:32:58 +02:00
ProcessManager: Implement core process management logic.
This commit is contained in:
parent
af6d60d06c
commit
b657d4adb5
@ -10,6 +10,7 @@
|
||||
|
||||
#include "stratosphere/ievent.hpp"
|
||||
#include "stratosphere/systemevent.hpp"
|
||||
#include "stratosphere/hossynch.hpp"
|
||||
|
||||
#include "stratosphere/waitablemanager.hpp"
|
||||
|
||||
|
120
include/stratosphere/hossynch.hpp
Normal file
120
include/stratosphere/hossynch.hpp
Normal file
@ -0,0 +1,120 @@
|
||||
#pragma once
|
||||
#include <switch.h>
|
||||
|
||||
class HosMutex {
|
||||
private:
|
||||
Mutex m;
|
||||
public:
|
||||
HosMutex() {
|
||||
mutexInit(&this->m);
|
||||
}
|
||||
|
||||
void Lock() {
|
||||
mutexLock(&this->m);
|
||||
}
|
||||
|
||||
void Unlock() {
|
||||
mutexUnlock(&this->m);
|
||||
}
|
||||
|
||||
bool TryLock() {
|
||||
return mutexTryLock(&this->m);
|
||||
}
|
||||
};
|
||||
|
||||
class HosRecursiveMutex {
|
||||
private:
|
||||
RMutex m;
|
||||
public:
|
||||
HosRecursiveMutex() {
|
||||
rmutexInit(&this->m);
|
||||
}
|
||||
|
||||
void Lock() {
|
||||
rmutexLock(&this->m);
|
||||
}
|
||||
|
||||
void Unlock() {
|
||||
rmutexUnlock(&this->m);
|
||||
}
|
||||
|
||||
bool TryLock() {
|
||||
return rmutexTryLock(&this->m);
|
||||
}
|
||||
};
|
||||
|
||||
class HosCondVar {
|
||||
private:
|
||||
CondVar cv;
|
||||
Mutex m;
|
||||
public:
|
||||
HosCondVar() {
|
||||
mutexInit(&m);
|
||||
condvarInit(&cv, &m);
|
||||
}
|
||||
|
||||
Result WaitTimeout(u64 timeout) {
|
||||
return condvarWaitTimeout(&cv, timeout);
|
||||
}
|
||||
|
||||
Result Wait() {
|
||||
return condvarWait(&cv);
|
||||
}
|
||||
|
||||
Result Wake(int num) {
|
||||
return condvarWake(&cv, num);
|
||||
}
|
||||
|
||||
Result WakeOne() {
|
||||
return condvarWakeOne(&cv);
|
||||
}
|
||||
|
||||
Result WakeAll() {
|
||||
return condvarWakeAll(&cv);
|
||||
}
|
||||
};
|
||||
|
||||
class HosSemaphore {
|
||||
private:
|
||||
CondVar cv;
|
||||
Mutex m;
|
||||
u64 count;
|
||||
public:
|
||||
HosSemaphore() {
|
||||
count = 0;
|
||||
mutexInit(&m);
|
||||
condvarInit(&cv, &m);
|
||||
}
|
||||
|
||||
HosSemaphore(u64 c) : count(c) {
|
||||
mutexInit(&m);
|
||||
condvarInit(&cv, &m);
|
||||
}
|
||||
|
||||
void Signal() {
|
||||
mutexLock(&this->m);
|
||||
count++;
|
||||
condvarWakeOne(&cv);
|
||||
mutexUnlock(&this->m);
|
||||
}
|
||||
|
||||
void Wait() {
|
||||
mutexLock(&this->m);
|
||||
while (!count) {
|
||||
condvarWait(&cv);
|
||||
}
|
||||
count--;
|
||||
mutexUnlock(&this->m);
|
||||
}
|
||||
|
||||
bool TryWait() {
|
||||
mutexLock(&this->m);
|
||||
bool success = false;
|
||||
if (count) {
|
||||
count--;
|
||||
success = true;
|
||||
}
|
||||
mutexUnlock(&this->m);
|
||||
return success;
|
||||
}
|
||||
};
|
@ -6,7 +6,7 @@
|
||||
|
||||
typedef Result (*EventCallback)(Handle *handles, size_t num_handles, u64 timeout);
|
||||
|
||||
class IEvent : IWaitable {
|
||||
class IEvent : public IWaitable {
|
||||
protected:
|
||||
std::vector<Handle> handles;
|
||||
EventCallback callback;
|
||||
|
@ -7,7 +7,7 @@
|
||||
#define SYSTEMEVENT_INDEX_WAITHANDLE 0
|
||||
#define SYSTEMEVENT_INDEX_SGNLHANDLE 1
|
||||
|
||||
class SystemEvent : IEvent {
|
||||
class SystemEvent : public IEvent {
|
||||
public:
|
||||
SystemEvent(EventCallback callback) : IEvent(0, callback) {
|
||||
Handle wait_h;
|
||||
|
@ -6,9 +6,9 @@
|
||||
|
||||
class WaitableManager {
|
||||
std::vector<IWaitable *> waitables;
|
||||
|
||||
u64 timeout;
|
||||
|
||||
private:
|
||||
void process_internal(bool break_on_timeout);
|
||||
public:
|
||||
WaitableManager(u64 t) : waitables(0), timeout(t) { }
|
||||
~WaitableManager() {
|
||||
@ -22,4 +22,5 @@ class WaitableManager {
|
||||
unsigned int get_num_signalable();
|
||||
void add_waitable(IWaitable *waitable);
|
||||
void process();
|
||||
void process_until_timeout();
|
||||
};
|
@ -17,7 +17,7 @@ void WaitableManager::add_waitable(IWaitable *waitable) {
|
||||
this->waitables.push_back(waitable);
|
||||
}
|
||||
|
||||
void WaitableManager::process() {
|
||||
void WaitableManager::process_internal(bool break_on_timeout) {
|
||||
std::vector<IWaitable *> signalables;
|
||||
std::vector<Handle> handles;
|
||||
|
||||
@ -56,6 +56,9 @@ void WaitableManager::process() {
|
||||
for (auto & waitable : signalables) {
|
||||
waitable->update_priority();
|
||||
}
|
||||
if (break_on_timeout) {
|
||||
return;
|
||||
}
|
||||
} else if (rc != 0xF601) {
|
||||
/* TODO: Panic. When can this happen? */
|
||||
}
|
||||
@ -91,4 +94,12 @@ void WaitableManager::process() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WaitableManager::process() {
|
||||
WaitableManager::process_internal(false);
|
||||
}
|
||||
|
||||
void WaitableManager::process_until_timeout() {
|
||||
WaitableManager::process_internal(true);
|
||||
}
|
Loading…
Reference in New Issue
Block a user