 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
PrimeTime
Joined: 19 Aug 2004 Posts: 25
|
Posted: Sun Aug 29, 2004 5:54 pm Post subject: Multi-tasking |
|
|
Hi again,
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: |
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.
}
|
|
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Tue Aug 31, 2004 7:46 am Post subject: |
|
|
If you noticed today's thread, mrbrown mentioned that the PS2 uses cooperative multitasking. The moment you wake the main thread, if it's a higher priority, it will run instead of the decoder thread. The main thread will cleanup and exit, then the decoder thread gets to run again - which is a serious bug.
You need to wait for the decoder thread to exit before doing any cleanup in the main thread. That might help the problem. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|