os: refactor/rewrite entire namespace.

This commit is contained in:
Michael Scire 2020-04-08 02:21:35 -07:00
parent 6193283f03
commit 065485b971
181 changed files with 5353 additions and 1929 deletions

View file

@ -14,72 +14,132 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "impl/os_waitable_object_list.hpp"
#include "impl/os_timeout_helper.hpp"
namespace ams::os {
Semaphore::Semaphore(int c, int mc) : count(c), max_count(mc) {
new (GetPointer(this->waitlist)) impl::WaitableObjectList();
void InitializeSemaphore(SemaphoreType *sema, s32 count, s32 max_count) {
AMS_ASSERT(max_count >= 1);
AMS_ASSERT(count >= 0);
/* Setup objects. */
new (GetPointer(sema->cs_sema)) impl::InternalCriticalSection;
new (GetPointer(sema->cv_not_zero)) impl::InternalConditionVariable;
/* Setup wait lists. */
new (GetPointer(sema->waitlist)) impl::WaitableObjectList;
/* Set member variables. */
sema->count = count;
sema->max_count = max_count;
/* Mark initialized. */
sema->state = SemaphoreType::State_Initialized;
}
Semaphore::~Semaphore() {
GetReference(this->waitlist).~WaitableObjectList();
void FinalizeSemaphore(SemaphoreType *sema) {
AMS_ASSERT(sema->state = SemaphoreType::State_Initialized);
AMS_ASSERT(GetReference(sema->waitlist).IsEmpty());
/* Mark uninitialized. */
sema->state = SemaphoreType::State_NotInitialized;
/* Destroy wait lists. */
GetReference(sema->waitlist).~WaitableObjectList();
/* Destroy objects. */
GetReference(sema->cv_not_zero).~InternalConditionVariable();
GetReference(sema->cs_sema).~InternalCriticalSection();
}
void Semaphore::Acquire() {
std::scoped_lock lk(this->mutex);
void AcquireSemaphore(SemaphoreType *sema) {
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
while (this->count == 0) {
this->condvar.Wait(&this->mutex);
{
std::scoped_lock lk(GetReference(sema->cs_sema));
while (sema->count == 0) {
GetReference(sema->cv_not_zero).Wait(GetPointer(sema->cs_sema));
}
--sema->count;
}
this->count--;
}
bool Semaphore::TryAcquire() {
std::scoped_lock lk(this->mutex);
bool TryAcquireSemaphore(SemaphoreType *sema) {
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
if (this->count == 0) {
return false;
}
{
std::scoped_lock lk(GetReference(sema->cs_sema));
this->count--;
return true;
}
bool Semaphore::TimedAcquire(u64 timeout) {
std::scoped_lock lk(this->mutex);
TimeoutHelper timeout_helper(timeout);
while (this->count == 0) {
if (timeout_helper.TimedOut()) {
if (sema->count == 0) {
return false;
}
this->condvar.TimedWait(&this->mutex, timeout_helper.NsUntilTimeout());
--sema->count;
}
this->count--;
return true;
}
void Semaphore::Release() {
std::scoped_lock lk(this->mutex);
bool TimedAcquireSemaphore(SemaphoreType *sema, TimeSpan timeout) {
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
AMS_ASSERT(timeout.GetNanoSeconds() >= 0);
AMS_ABORT_UNLESS(this->count + 1 <= this->max_count);
this->count++;
{
impl::TimeoutHelper timeout_helper(timeout);
std::scoped_lock lk(GetReference(sema->cs_sema));
this->condvar.Signal();
GetReference(this->waitlist).SignalAllThreads();
while (sema->count == 0) {
if (timeout_helper.TimedOut()) {
return false;
}
GetReference(sema->cv_not_zero).TimedWait(GetPointer(sema->cs_sema), timeout_helper);
}
--sema->count;
}
return true;
}
void Semaphore::Release(int count) {
std::scoped_lock lk(this->mutex);
void ReleaseSemaphore(SemaphoreType *sema) {
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
AMS_ABORT_UNLESS(this->count + count <= this->max_count);
this->count += count;
{
std::scoped_lock lk(GetReference(sema->cs_sema));
this->condvar.Broadcast();
GetReference(this->waitlist).SignalAllThreads();
AMS_ASSERT(sema->count + 1 <= sema->max_count);
++sema->count;
GetReference(sema->cv_not_zero).Signal();
GetReference(sema->waitlist).SignalAllThreads();
}
}
void ReleaseSemaphore(SemaphoreType *sema, s32 count) {
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
{
std::scoped_lock lk(GetReference(sema->cs_sema));
AMS_ASSERT(sema->count + count <= sema->max_count);
sema->count += count;
GetReference(sema->cv_not_zero).Signal();
GetReference(sema->waitlist).SignalAllThreads();
}
}
s32 GetCurrentSemaphoreCount(const SemaphoreType *sema) {
AMS_ASSERT(sema->state == SemaphoreType::State_Initialized);
return sema->count;
}
// void InitializeWaitableHolder(WaitableHolderType *waitable_holder, SemaphoreType *sema);
}