mirror of
https://github.com/thunderbrewhq/squall.git
synced 2025-12-12 10:32:29 +00:00
feat(thread): add thread functions
This commit is contained in:
parent
d0487caba3
commit
266bdc7737
16 changed files with 523 additions and 0 deletions
19
storm/thread/mac/SThreadRunner.h
Normal file
19
storm/thread/mac/SThreadRunner.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef STORM_THREAD_MAC_S_THREAD_RUNNER_H
|
||||
#define STORM_THREAD_MAC_S_THREAD_RUNNER_H
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
struct SThreadParmBlock;
|
||||
|
||||
@interface SThreadRunner : NSObject
|
||||
|
||||
@property SThreadParmBlock* m_params;
|
||||
@property(assign) NSAutoreleasePool* m_autoreleasePool;
|
||||
|
||||
- (instancetype)init:(SThreadParmBlock*)params;
|
||||
|
||||
- (void)run;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
23
storm/thread/mac/SThreadRunner.mm
Normal file
23
storm/thread/mac/SThreadRunner.mm
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include "thread/mac/SThreadRunner.h"
|
||||
#include "thread/S_Thread.hpp"
|
||||
|
||||
@implementation SThreadRunner
|
||||
|
||||
- (instancetype)init:(SThreadParmBlock*)params {
|
||||
if (self = [super init]) {
|
||||
self.m_params = params;
|
||||
self.m_autoreleasePool = nullptr;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)run {
|
||||
self.m_autoreleasePool = [[NSAutoreleasePool alloc] init];
|
||||
|
||||
S_Thread::s_SLaunchThread(self.m_params);
|
||||
|
||||
[self.m_autoreleasePool release];
|
||||
}
|
||||
|
||||
@end
|
||||
43
storm/thread/mac/S_Thread.mm
Normal file
43
storm/thread/mac/S_Thread.mm
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include "thread/S_Thread.hpp"
|
||||
#include "Memory.hpp"
|
||||
|
||||
uint32_t S_Thread::s_SLaunchThread(void* threadParam) {
|
||||
// TODO
|
||||
// SClearFP(1);
|
||||
|
||||
auto params = static_cast<SThreadParmBlock*>(threadParam);
|
||||
|
||||
// TODO
|
||||
// S_Thread::s_threadCrit.Enter();
|
||||
|
||||
// TODO
|
||||
// - something involving pthread_self()
|
||||
|
||||
// TODO
|
||||
// S_Thread::s_threadCrit.Leave();
|
||||
|
||||
// TODO
|
||||
// SErrAddFrameText("Calling thread proc %lx\n", params->threadProc);
|
||||
params->threadProc(params->threadParam);
|
||||
|
||||
// TODO
|
||||
// S_Thread::s_threadCrit.Enter();
|
||||
|
||||
// TODO
|
||||
// - remove tracking in S_Thread::s_threads
|
||||
// - decrement num threads
|
||||
|
||||
// TODO
|
||||
// S_Thread::s_threadCrit.Leave();
|
||||
|
||||
if (params->syncObject) {
|
||||
pthread_mutex_lock(¶ms->syncObject->m_mutex);
|
||||
params->syncObject->m_value = 1;
|
||||
pthread_mutex_unlock(¶ms->syncObject->m_mutex);
|
||||
pthread_cond_signal(¶ms->syncObject->m_cond);
|
||||
}
|
||||
|
||||
SMemFree(threadParam);
|
||||
|
||||
return 0;
|
||||
}
|
||||
75
storm/thread/mac/Thread.mm
Normal file
75
storm/thread/mac/Thread.mm
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#include "Thread.hpp"
|
||||
#include "Memory.hpp"
|
||||
#include "String.hpp"
|
||||
#include "thread/S_Thread.hpp"
|
||||
#include "thread/mac/SThreadRunner.h"
|
||||
#include <new>
|
||||
|
||||
int32_t SCreateThread(uint32_t (*threadProc)(void*), void* threadParam, void* a3, SThread* syncObject, const char* threadName) {
|
||||
if (!threadName) {
|
||||
threadName = "";
|
||||
}
|
||||
|
||||
// TODO
|
||||
// S_Thread::s_threadCrit.Enter();
|
||||
|
||||
if (!S_Thread::s_numthreads) {
|
||||
uint32_t threadId = S_Thread::s_threadID++;
|
||||
|
||||
auto& mainThread = S_Thread::s_threads[0];
|
||||
mainThread.suspended = 0;
|
||||
mainThread.live = 1;
|
||||
mainThread.threadId = threadId;
|
||||
SStrCopy(mainThread.name, "main", sizeof(mainThread.name));
|
||||
|
||||
S_Thread::s_numthreads++;
|
||||
|
||||
// TODO
|
||||
/*
|
||||
if (StormGetOption(8, &v32, v33)) {
|
||||
auto& cdThread = S_Thread::s_threads[S_Thread::s_numthreads];
|
||||
cdThread.suspended = 0;
|
||||
cdThread.live = 1;
|
||||
cdThread.threadId = v32;
|
||||
SStrCopy(cdThread.name, "CdThreadProc", sizeof(cdThread.name));
|
||||
|
||||
S_Thread::s_numthreads++;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
uint32_t threadId = S_Thread::s_threadID++;
|
||||
|
||||
void* m = SMemAlloc(sizeof(SThreadParmBlock), __FILE__, __LINE__, 0x8);
|
||||
auto params = new (m) SThreadParmBlock();
|
||||
params->threadProc = threadProc;
|
||||
params->threadParam = threadParam;
|
||||
params->threadID = threadId;
|
||||
params->syncObject = syncObject;
|
||||
|
||||
if (syncObject) {
|
||||
syncObject->m_value = 0;
|
||||
pthread_cond_init(&syncObject->m_cond, nullptr);
|
||||
pthread_mutex_init(&syncObject->m_mutex, nullptr);
|
||||
}
|
||||
|
||||
// Launch thread
|
||||
auto threadRunner = [[SThreadRunner alloc] init:params];
|
||||
[NSThread
|
||||
detachNewThreadSelector:@selector(run)
|
||||
toTarget:threadRunner
|
||||
withObject:nil];
|
||||
|
||||
auto& thread = S_Thread::s_threads[S_Thread::s_numthreads];
|
||||
thread.suspended = 0;
|
||||
thread.live = 1;
|
||||
thread.threadId = threadId;
|
||||
SStrCopy(thread.name, threadName, sizeof(thread.name));
|
||||
|
||||
S_Thread::s_numthreads++;
|
||||
|
||||
// TODO
|
||||
// S_Thread::s_threadCrit.Leave();
|
||||
|
||||
return threadId;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue