I'm having a problem with threads.
What I am doing is creating and starting a thread from main() that
will call a routine which in turn will create and start another thread.
The first thread from main() will run a function that will open and buffer a sound file. When the buffer is full, this function starts a new thread that plays the buffer and then it gets back to work refilling the buffer. Everything works fine except in my main function. After I start the thread, the main() will continue to run until it eventually ends, which is normal, I don't want it to end however so I've placed a busy-waiting loop after starting the thread. This works, but it seems that whenever my thread read a sound file to buffer, it makes some incorrect reads.
Next, I tried placing a SleepThread or WaitSema after starting the thread. This works great. After the thread is done, I call WakeupThread and give it the ID of the main routine. Even though I called wakeupthread() at the end of my thread function, it crashes. The ps2 throws and exception at me.
My goal is to be able to stream a sound file while using the controller to browse other sound files on the HDD.
Anyway here is some code from my main(), maybe someone knows an alternative method i could use.
Code: Select all
int main()
{
   int pid=-1;
   int t=0;
   t_hddInfo hddInfo;
   int iFile;
   struct WAV decoder;
   
   SifInitRpc(0);
   init_scr();
   hddPreparePoweroff();
   hddInit();
   hddSetUserPoweroffCallback((void *)poweroffHandler,(void *)pid);
   ret = sbv_patch_enable_lmb();
   ret = sbv_patch_disable_prefix_check();
   
   rmallocInit();
	
   if (OpenPartition("hdd0:+WAV") < 0)
      return -1;
   SjPCM_Init(0);
   SjPCM_Clearbuff();
   SjPCM_Setvol(0x3fff);
	
   CreateDecoder("voice.wav", 8, &decoder); //This sets up the thread with priority 8
   StartDecoder(&decoder);  //StartThread();
   SleepThread();
   ClosePartition();
   scr_printf("Finished.\n");
   SifInitRpc(0);
   return 0;
}
int CreateDecoder(char *filename, int priority, struct WAV *decode)
{
   decode->thread.func = (void *)RunDecoder;
   decode->thread.stack = decode->decoderThreadStack;
   decode->thread.stack_size = sizeof(decode->decoderThreadStack);
   decode->thread.gp_reg = &_gp;
   decode->thread.initial_priority = priority;
   decode->pid = CreateThread(&decode->thread);
   strcpy(decode->fileName, filename);
   decode->parent = GetThreadId();
   decode->flag = 1;
   decode->sema.init_count = 0;
   decode->sema.max_count = 1;
   decode->sema.option = 0;
   decode->Semaphore = CreateSema(&decode->sema);
   return decode->pid;
}
void RunDecoder(void *arg) //this is the threaded function
{
   struct WAV *decode = (struct WAV *)arg;
   printf("opening...\n");
   decode->iFile = fileXioOpen(decode->fileName, O_RDONLY, 0);
   printf("opened %d\n", decode->iFile);
   WAVAudioDecoder(decode->iFile); //This buffers and calls the SjPCM thread.  This function does not end until the sound is done.
   decode->flag = 2;
   WakeupThread(decode->parent); //Wakes up main()
   printf("RunDecoder Done\n");  //Crashes after function exits.
}