Dawn/src/dawn/engine/thread.c

45 lines
965 B
C

/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "thread.h"
void threadInit(thread_t *thread, threadfunction_t *call) {
thread->call = call;
thread->user = NULL;
thread->state = 0x00;
}
void threadStart(thread_t *thread) {
thread->state |= THREAD_FLAG_QUEUED;
sysThreadCreate(&_threadWrappedCallback, thread->pt, thread);
}
void threadJoin(thread_t *thread) {
sysThreadJoin(thread->pt);
}
void threadCancel(thread_t *thread) {
sysThreadCancel(thread->pt, 1);
}
void threadSleep(float time) {
sleep((unsigned long)(time * 1000.0f));
}
int32_t _threadWrappedCallback(thread_t *thread) {
int32_t response;
flagOff(thread->state, THREAD_FLAG_QUEUED);
thread->state |= THREAD_FLAG_RUNNING;
response = thread->call(thread);
thread->state |= THREAD_FLAG_HAS_RUN;
flagOff(thread->state, THREAD_FLAG_RUNNING);
return response;
}