Browse Source

Implemented threads for nx.

woollybah 7 years ago
parent
commit
79cf16622d
3 changed files with 114 additions and 7 deletions
  1. 81 2
      blitz.mod/blitz_thread.c
  2. 8 5
      blitz.mod/blitz_thread.h
  3. 25 0
      threads.mod/threads.c

+ 81 - 2
blitz.mod/blitz_thread.c

@@ -201,10 +201,89 @@ void bbThreadUnregister( BBThread * thread ) {
 	BB_UNLOCK
 }
 
-//#elif __SWITCH__
+#elif __SWITCH__
+
+static __thread BBThread * bbThread;
+
+void bbThreadStartup() {
+
+	BBThread *thread=GC_MALLOC_UNCOLLECTABLE( sizeof( BBThread ) );
+	
+	thread->proc=0;
+	thread->detached=0;
+	thread->handle=thrd_current();
+
+	bbThread = thread;
+
+	thread->succ=threads;
+	threads=thread;
+	mainThread=thread;
+}
+
+static int threadProc( void *p ){
+	BBThread *thread = p;
+	
+	bbThread = thread;
+	
+	BB_LOCK
+	addThread( thread );
+	BB_UNLOCK
+	
+#ifdef DEBUG_THREADS
+	printf( "Thread %p added\n",thread );fflush( stdout );
+#endif
+	
+	void *ret=thread->proc( thread->data[0] );
+	
+	BB_LOCK
+	removeThread( thread );
+	BB_UNLOCK
+	
+#ifdef DEBUG_THREADS
+	printf( "Thread %p removed\n",thread );fflush( stdout );
+#endif
+	
+	return ret;
+}
+
+BBThread * bbThreadCreate( BBThreadProc proc,BBObject *data ) {
+	BBThread *thread=GC_MALLOC_UNCOLLECTABLE( sizeof( BBThread ) );
+	memset( thread->data,0,sizeof(thread->data) );
+	
+	thread->proc=proc;
+	thread->data[0]=data;
+	thread->detached=0;
+	if( thrd_create( &thread->handle,threadProc,thread ) == thrd_success ){
+		_bbNeedsLock=1;
+		return thread;
+	}
+	GC_FREE( thread );
+	return 0;
+}
+
+void bbThreadDetach( BBThread *thread ) {
+	thread->detached=1;
+//	thrd_detach( thread->handle );
+}
+
+BBObject * bbThreadWait( BBThread *thread ) {
+	BBObject *p=0;
+	thread->detached=1;
+	thrd_join( thread->handle,&p );
+	return p;
+}
+
+BBThread * bbThreadGetMain() {
+	return mainThread;
+}
 
-// TODO
+BBThread * bbThreadGetCurrent() {
+	return bbThread;
+}
 
+int bbThreadResume( BBThread *thread ) {
+	return 0;
+}
 
 //***** POSIX threads *****
 #else

+ 8 - 5
blitz.mod/blitz_thread.h

@@ -30,13 +30,14 @@ typedef HANDLE bb_sem_t;
 #elif __SWITCH__
 #include<switch/kernel/mutex.h>
 #include<switch/kernel/semaphore.h>
+#include <threads.h>
 
-typedef Mutex bb_mutex_t;
-#define bb_mutex_init(MUTPTR) (mutexInit(MUTPTR),1)
+typedef mtx_t bb_mutex_t;
+#define bb_mutex_init(MUTPTR) (mtx_init(MUTPTR,mtx_recursive),1)
 #define bb_mutex_destroy(MUTPTR)
-#define bb_mutex_lock(MUTPTR) mutexLock(MUTPTR)
-#define bb_mutex_unlock(MUTPTR) mutexUnlock(MUTPTR)
-#define bb_mutex_trylock(MUTPTR) (mutexTryLock(MUTPTR)!=0)
+#define bb_mutex_lock(MUTPTR) mtx_lock(MUTPTR)
+#define bb_mutex_unlock(MUTPTR) mtx_unlock(MUTPTR)
+#define bb_mutex_trylock(MUTPTR) (mtx_trylock(MUTPTR)!=0)
 
 typedef Semaphore bb_sem_t;
 #define bb_sem_init(SEMPTR,COUNT) (semaphoreInit( (SEMPTR), (COUNT) ), 1)
@@ -103,6 +104,8 @@ struct BBThread{
 #ifdef _WIN32
 	HANDLE handle;
 	DWORD id;
+#elif __SWITCH__
+	thrd_t handle;
 #else
 	pthread_t handle;
 #endif

+ 25 - 0
threads.mod/threads.c

@@ -123,6 +123,31 @@ void threads_BroadcastCond( BBCond *cond ){
 	}
 }
 
+#elif __SWITCH__
+
+cnd_t *threads_CreateCond(){
+	cnd_t *cond=malloc( sizeof(cnd_t) );
+	if( cnd_init( cond )==thrd_success ) return cond;
+	free( cond );
+	return 0;
+}
+
+void threads_CloseCond( cnd_t *cond ){
+	free( cond );
+}
+
+void threads_WaitCond( cnd_t *cond,bb_mutex_t *mutex ){
+	cnd_wait( cond,mutex );
+}
+
+void threads_SignalCond( cnd_t *cond ){
+	cnd_signal( cond );
+}
+
+void threads_BroadcastCond( cnd_t *cond ){
+	cnd_broadcast( cond );
+}
+
 #else
 
 pthread_cond_t *threads_CreateCond(){