start of actual thread work

This commit is contained in:
zetaPRIME 2017-05-01 00:40:13 -04:00
parent 2e4d55a1ff
commit 9645920759
3 changed files with 75 additions and 1 deletions

View File

@ -0,0 +1,43 @@
#include "Thread.h"
#include "3ds.h"
using starlight::threading::Thread;
using SysThread = ::Thread;
namespace {
void _ThreadEnter(void* arg) {
// cast to thread and start up
static_cast<starlight::threading::Thread*>(arg)->_FinishStart();
}
}
Thread::~Thread() {
// ...threadjoin? something like that??
if (event != 0) svcCloseHandle(event);
}
void Thread::Start() {
svcCreateEvent(&event, RESET_ONESHOT);
sthread = static_cast<void*>(threadCreate(_ThreadEnter, static_cast<void*>(this), 4*1024, 0x3F, -2, false));
}
void Thread::_FinishStart() {
// lock out once already done
if (state != ThreadState::Init) return;
Yield();
Body();
}
void Thread::Yield() {
if (state != ThreadState::Running) return; // not executing this right now, this would just futz it up
svcWaitSynchronization(event, -1 /*U64_MAX*/);
svcClearEvent(event);
}
void Thread::Resume() {
if (state != ThreadState::Idle) return; // not applicable
svcSignalEvent(event);
}
//

View File

@ -0,0 +1,31 @@
#pragma once
#include "starlight/_global.h"
namespace starlight {
namespace threading {
enum class ThreadState : unsigned char {
Unqueued, Init, Idle, Running
};
class Thread {
protected:
volatile ThreadState state;
void* sthread;
long unsigned int event = 0;
void Start();
public:
~Thread();
inline ThreadState State() { return state; }
void _FinishStart();
void Enqueue();
void Yield();
void Resume();
virtual void Body() = 0;
};
}
}

View File

@ -13,7 +13,7 @@ roadmap to v0.5.1 {
proper thread dispatch? { proper thread dispatch? {
Application main loop keeps a libctru TickCounter and keeps track of frame time; Application main loop keeps a libctru TickCounter and keeps track of frame time;
thread objects are held in a std::list, Application dispatches resume events and splice()s them to the end until frame time reaches some proportion of 1/60s thread objects are held in a std::list, Application dispatches resume events and splice()s them to the end until frame time reaches some proportion of 1/60s
thread gets a yield function that calls svcWaitSynchronization on its resume event - thread gets a yield function that calls svcWaitSynchronization on its resume event
...and some mechanism for allowing it to opt out of the rest of the cycle ...and some mechanism for allowing it to opt out of the rest of the cycle
} }
} then consider these before 1.0 "gold" { } then consider these before 1.0 "gold" {