Callback - Multi threading - Help Please

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
r3v3rb
Posts: 6
Joined: Sat Jul 02, 2005 2:51 am

Callback - Multi threading - Help Please

Post by r3v3rb »

I am trying to setup the controller to change some mod (music files) that i have playing over a little demo i am creating, but if the code runs in the main thread then the whole thing stops whilst the new track is called in to play.

So i have tried setting this process up as a seperate thread, the only issue i seem to be having is that after a short period it stops working and no button presses get parsed, at the very start of the program the callback works reasonably well, still pauses slighty but not as ugly. Below is the call back code i have produced any help would be greatly appreciated::

Code: Select all

void controllertest(void *arg){

ctrl_data_t pad;

sceCtrlReadBufferPositive(&pad, 1);
    
		if(pad.buttons & CTRL_RIGHT){
			ModPlay_Stop();
			ModPlay_Init(0,music2);
			ModPlay_Play();
		}
		if(pad.buttons==CTRL_LEFT){
			ModPlay_Stop();
			ModPlay_Init(0,music3);
			ModPlay_Play();
		}
		if(pad.buttons==CTRL_UP){
			ModPlay_Stop();
			ModPlay_Init(0,music4);
			ModPlay_Play();
		}
		if(pad.buttons==CTRL_DOWN){
			ModPlay_Stop();
			ModPlay_Init(0,music5);
			ModPlay_Play();
		}
		else
		{
		pgPrint(0,0,0xffff,"Now Playing :: ",0,255);
		}
}

int setupcontroller(void){

int thrd = 0;

thrd = sceKernelCreateThread("audio_thread", controllertest, 0x11, 0xFA0, 0, 0);

if(thrd>=0)
sceKernelStartThread(thrd,0,0);
return thrd;
}
I am not sure of the arguments 0x11 and 0xFA could they be conflicting with the same args in the home button callback as the program fails to quit properly as well when this call back has been implemented.

regards,

r3v3rb
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

Set 0x11 to sommit like 0x12 and it should work, or at the very least call sceDisplayWaitVblankStart to put your test thread into a wait state, otherwise the callback thread cannot preempt the currently running thread and thus never gets called.
r3v3rb
Posts: 6
Joined: Sat Jul 02, 2005 2:51 am

Post by r3v3rb »

thank you tyranid, tried to catch you on efnet the other day for this but missed you. I have done the 0x12 already and its still plays about so now its time for the sceDisplayWaitVBlankStart, I'm guessing that this needs to go with the main program part and not in the thread ? or is it round the other way ?
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

Hmmlooking at your code again you you don't actually loop in that thread so it must be dropping out of it again. What does the rest of your code do? i.e. the put after you call setup controller ?
r3v3rb
Posts: 6
Joined: Sat Jul 02, 2005 2:51 am

Post by r3v3rb »

the bit before and after the setupcontroller function loops round forever. It's wierd though even by altering to 0x12 it screws the home button callback. PSP goes to 'Please Wait' and then just sits there, the music continues to play but then nothing happens. I have to physically turn off he psp...
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

Well it will do :) Setting 0x12 only works for the thread you created, once it drops out of the thread at the end of the function is it cleaned up anyway so it doesn't make a difference. Either in your main thread (the thing which created the thread as is running in an infinite loop) call either sceKernelSleepThread() or sceKernelExitDeleteThread(0) to stop it or sceDisplayWaitVblankStart if you are still doing stuff in the loop.

Although if you main thread is of a low enough prioroty the callback should just work but it is best to try, though without the code it is hard to tell what is wrong.
r3v3rb
Posts: 6
Joined: Sat Jul 02, 2005 2:51 am

Post by r3v3rb »

Well, i took out the thread setup from the code and put it back to a straight function call from within the main program loop. Lo and behold, the audio changes, the home button works properly but I get a horrendous pause whilst it inits the music files. So i can either leave like this which is crap or something is wrong with this

Code: Select all

void setupcontroller(void){ 

int thrd = 0; 

thrd = sceKernelCreateThread("audio_thread", controllertest, 0x12, 0xFA0, 0, 0); 

if(thrd>=0) 
sceKernelStartThread(thrd,0,0); 
return thrd;

} 
or the way I am implementing the thread is wrong. sceDisplayWaitVBlankStart gave errors when compiling...

Or I need a way to load data into memory during the loop and not have it init until all data is ready to be dealt with... any ideas ?
r3v3rb
Posts: 6
Joined: Sat Jul 02, 2005 2:51 am

Post by r3v3rb »

success, setting the thread to loop and removing the call from the main loop has the program running without too many hitches, just the modplay_init pauses to try and suss now.

Many thanks TyRaNID !! :D
Post Reply