Console input

This commit is contained in:
2025-08-20 21:23:12 -05:00
parent fbfcbe9578
commit 84f2735246
8 changed files with 125 additions and 48 deletions

View File

@@ -26,11 +26,9 @@ void threadInit(thread_t *thread, const threadcallback_t callback) {
void threadStartRequest(thread_t *thread) {
assertNotNull(thread, "Thread cannot be NULL.");
printf("Starting thread...\n");
threadMutexLock(&thread->stateMutex);
thread->state = THREAD_STATE_STARTING;
threadMutexUnlock(&thread->stateMutex);
printf("Thread marked starting.\n");
#if DUSK_THREAD_PTHREAD
assertTrue(thread->threadId == 0, "Thread id not 0.");
@@ -43,8 +41,6 @@ void threadStartRequest(thread_t *thread) {
);
pthread_detach(thread->threadId);
#endif
printf("Thread created.\n");
}
void threadStopRequest(thread_t *thread) {
@@ -70,7 +66,6 @@ void threadStart(thread_t *thread) {
void threadStop(thread_t *thread) {
assertNotNull(thread, "Thread cannot be NULL.");
threadStopRequest(thread);
threadMutexLock(&thread->stateMutex);
while(thread->state != THREAD_STATE_STOPPED) {
@@ -79,6 +74,25 @@ void threadStop(thread_t *thread) {
threadMutexUnlock(&thread->stateMutex);
}
bool_t threadShouldStop(thread_t *thread) {
bool_t state;
assertNotNull(thread, "Thread cannot be NULL.");
threadMutexLock(&thread->stateMutex);
switch(thread->state) {
case THREAD_STATE_STOPPED:
case THREAD_STATE_STOP_REQUESTED:
state = true;
break;
default:
state = false;
break;
}
threadMutexUnlock(&thread->stateMutex);
return state;
}
#if DUSK_THREAD_PTHREAD
void * threadHandler(thread_t *thread) {
assertNotNull(thread, "Thread cannot be NULL.");

View File

@@ -78,12 +78,21 @@ void threadStart(thread_t *thread);
/**
* Stops the thread, blocking until it has stopped. Does this as efficiently as
* possible depending on the threading implementation.
* possible depending on the threading implementation. Note that it is possible
* for the thread to fully COMPLETE well before this function returns.
*
* @param thread Pointer to the thread structure to stop.
*/
void threadStop(thread_t *thread);
/**
* Checks if the thread should stop, based on its state.
*
* @param thread Pointer to the thread structure to check.
* @return true if the thread should stop, false otherwise.
*/
bool_t threadShouldStop(thread_t *thread);
#if DUSK_THREAD_PTHREAD
/**
* Handles the thread's lifecycle for pthreads.