Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- custom threading system
- usage; you basically do;
- void main(){
- int mythreadid=13;
- threading_init(); //only need to be done once
- threading_start(run_this_func, mythreadid );
- threading_wait_on(mythreadid); //option 1: block until thread is done
- while( !threading_done(mythreadid) ){ //option 2: watch for thread to be complete
- //do animation or something else while thread is running, if you chose not to block.
- }
- }
- static void *run_this_func(void *arg) {
- //your code here
- while( threading_loop() ){
- //your code here
- }
- threading_halt_self(); //tell threading system than we done
- } //end sure that all threads end normally or you will run out of thread slots
- */
- #include <errno.h>
- #include <ogcsys.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- //-----------------------------------------------------------------------------------
- #define THREADING_MAX_CONCURRENT 10
- #define THREADMAX 18
- #define STACKSIZE 8*1024 //was 8*1024
- static lwp_t main_thread[THREADMAX]; //singleton
- lwpq_t thread_queue;
- static volatile bool main_thread_active[THREADMAX];
- //static volatile bool main_thread_complete = false;
- //static volatile bool main_thread_busy = false;
- static u8 stack1[THREADMAX][STACKSIZE] ATTRIBUTE_ALIGN (32);
- static int current_thread=0;
- static int threading_system_initialized=0;
- static int threading_system_halted=0;
- static bool threading_debug = false;
- int testint;
- //----------------------------------------------------------------
- typedef struct {
- int thread_key;
- bool active;
- } ThreadKeyStruct;
- //-------------------------------------------
- ThreadKeyStruct threadkey_list[THREADMAX];
- //----------------------------------------------------------------
- int threading_active_count(){ //internal function
- unsigned int i;
- int temp=0;
- for(i=1;i<THREADMAX;++i) { //probably i should start from current_thread instead of zero
- if( threadkey_list[i].active ) temp++;
- }
- return temp;
- }
- //---------------------------------------------------------------------------
- bool threading_loop(){
- usleep(5);
- if( threading_system_halted==1 ) return false;
- return true;
- }
- //----------------------------------------------------------------
- bool threading_done(int thread_key){
- int i;
- for(i=0;i<THREADMAX;++i){
- if( threadkey_list[i].thread_key == thread_key ) {
- //printf("thread closed normally. i=%i, tk=%i\n", i, threadkey_list[i].thread_key);
- if(main_thread[i] == LWP_CLOSED ) threadkey_list[i].active=false;
- if(LWP_ThreadIsSuspended(main_thread[i])){
- threadkey_list[i].active=false; //thread must suspend itself when done by calling threading_halt_self()
- //or the thread will just keep running forever.
- }
- return !threadkey_list[i].active;
- }
- }
- return true; //probably not done or not in list at all
- }
- int threading_test(){
- return testint;
- }
- void threading_debug_flag(bool flag){
- threading_debug = flag;
- }
- //----------------------------------------------------------------
- void threading_init(){
- if(threading_system_initialized !=0 ) return; //already initialized
- int i;
- for(i=0;i<THREADMAX;++i) {
- main_thread[i] = LWP_THREAD_NULL;
- threadkey_list[i].active=false;
- threadkey_list[i].thread_key=-1;
- }
- threading_system_halted=0;
- threading_system_initialized=1;
- current_thread=0;
- }
- //----------------------------------------------------------------
- static void threading_find_next_empty_slot(){ //internal function
- if(current_thread > THREADMAX-1 ) current_thread = 0;
- if( threading_active_count() > THREADING_MAX_CONCURRENT-1 ){ //if too many threads open waiting until one stops
- if(threading_debug) printf("Too many threads open (%i/%i) waiting for a free slot ....\n", threading_active_count(), THREADING_MAX_CONCURRENT-1 );
- while(threading_loop()){
- if( threading_active_count() < THREADING_MAX_CONCURRENT ) break; //wait until a slot is free
- LWP_YieldThread();
- }
- if(threading_debug) printf("....done waiting for free slot (%i/%i).\n", threading_active_count(), THREADING_MAX_CONCURRENT-1 );
- }
- if( threadkey_list[current_thread].active ){ //slot if already in use
- unsigned int i;
- for(i=0;i<THREADMAX;++i) { //probably i should start from current_thread instead of zero
- if(main_thread[i] == LWP_CLOSED ) threadkey_list[i].active=false;
- if( threadkey_list[i].active ){ //check if this thread is suspended for some reason
- //if( LWP_ThreadIsSuspended(main_thread[i]) ) LWP_ResumeThread(main_thread[i]);
- }
- if( !threadkey_list[i].active ){
- current_thread=i; //set this to current and exit
- break;
- }
- }
- } else {
- //no need to do anything because this one is not active USED IT
- }
- //if it can't find a empty slot its going to just take the first one
- }
- //----------------------------------------------------------------
- bool threading_start( void * (*thread_func)(void *), int thread_key ){
- if(threading_system_initialized == 0 ) threading_init();
- threading_find_next_empty_slot();
- if(threading_debug) printf("thread selected. %i tk:%i\n", current_thread, thread_key );
- threadkey_list[current_thread].thread_key = thread_key;
- threadkey_list[current_thread].active = true;
- if(threading_debug) printf("thread created....\n");
- memset (&stack1[current_thread], 0, STACKSIZE); // Clear memory
- s32 result = LWP_CreateThread(&main_thread[current_thread], thread_func, NULL, stack1[current_thread], STACKSIZE, 60+current_thread);
- if (result >= 0) {
- testint=555;
- return true;
- }
- testint=threading_active_count();
- if(threading_debug) printf("thread failed to create.\n");
- return false;
- }
- //---------------------------------------------------------------------------
- void threading_wait_on(int thread_key){ //block until a thread is done
- if(threading_debug)
- printf("threading_wait_on(%i)...\n", thread_key);
- while( threading_loop() ){
- if( threading_done(thread_key) ) break;
- //fflush(stdout);
- //usleep(5);
- if( threading_system_halted==1 ) break;
- LWP_YieldThread();
- }
- if(threading_debug)
- printf("...done waiting on thread (%i)\n", thread_key);
- }
- //---------------------------------------------------------------------------
- void threading_exit(){
- threading_system_halted=1;
- threading_system_initialized=0;
- int i;
- for(i=0;i<THREADMAX;++i){
- if( !threadkey_list[i].active ) continue; //only check active threads
- threadkey_list[i].active=false;
- if( LWP_SuspendThread( main_thread[i] ) < 0 ){
- if(threading_debug)
- printf("failed to suspend thread. i=%i, tk=%i\n", i, threadkey_list[i].thread_key);
- }
- }
- if(threading_debug) printf("threading exited\n");
- }
- //---------------------------------------------------------------------------
- void threading_halt_self(){
- int i, tk, t_id;
- //bool found=false; //check to make sure this thread belongs to us before we halt it!
- for(i=0;i<THREADMAX;++i){
- if( !threadkey_list[i].active ) continue; //only check active threads
- if( main_thread[i] == LWP_GetSelf() ){
- t_id = i;
- threadkey_list[t_id].active=false;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement