| View previous topic :: View next topic |
| Author |
Message |
cosmito
Joined: 04 Mar 2007 Posts: 314 Location: Portugal
|
Posted: Thu Nov 29, 2007 9:29 am Post subject: SDL and thread support |
|
|
Hi,
I've been trying the SDL and thread support but it seems there's something I'm missing ...
A simple example : creating a thread that prints the sequence 1-2-3-4 with a 1 second delay while the main program prints '*' every 5 seconds.
| Code: | // adapted from : http://lazyfoo.net/SDL_tutorials/lesson33/index.php
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include <SDL_thread.h>
//The event structure
SDL_Event event;
//The thread that will be used
SDL_Thread *thread = NULL;
//Quit flag
int quit = 0;
int init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVENTTHREAD | SDL_INIT_TIMER ) == -1 )
{
return 0;
}
return 1;
}
/*
void clean_up()
{
//Stop the thread
SDL_KillThread( thread );
//Quit SDL
SDL_Quit();
}
*/
int my_thread( void *data )
{
//While the program is not over
while( quit == 0 )
{
scr_printf("1");
SDL_Delay( 1000 );
scr_printf("2");
SDL_Delay( 1000 );
scr_printf("3");
SDL_Delay( 1000 );
scr_printf("4");
SDL_Delay( 1000 );
}
scr_printf("over");
return 0;
}
int main(int argc, char *argv[])
{
//Initialize
SifInitRpc(0);
init_scr();
if( init() == 0 )
{
return 1;
}
//Create and run the thread
thread = SDL_CreateThread( my_thread, NULL );
//Forever
while(1)
{
/*
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = 1;
}
}
*/
SDL_Delay(5000);
scr_printf("*");
}
// Never got here ...
/*
//Clean up
clean_up();
*/
return 0;
}
|
The thread seems to be created right and starts running, but as soon the main program SDL_Delay(5000) ends, the thread stops. Try changing the delay to something bigger and the same happens.
(BTW : For quick compiling, I uploaded it along with the makefile to http://www.esnips.com/web/ps2-homebrew/. Look for SDL thread.zip and unzip to your sdl\test\ folder) |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Thu Nov 29, 2007 11:24 am Post subject: |
|
|
| The ps2 is just like the psp - they both use cooperative multitasking. Once the main thread comes back, it STAYS in control until you call something that releases control again. |
|
| Back to top |
|
 |
EEUG
Joined: 13 May 2005 Posts: 136 Location: The Netherlands
|
Posted: Fri Nov 30, 2007 7:33 pm Post subject: |
|
|
| ...though it is quite easy to implement "preemptive" multitasking. Just create your threads having the same priority and use 'iRotateThreadQueue' system call with that priority as argument in some kind of "timer interrupt handler". Be careful though with DMA, SIF and other hardware... |
|
| Back to top |
|
 |
cosmito
Joined: 04 Mar 2007 Posts: 314 Location: Portugal
|
Posted: Sun Dec 02, 2007 8:27 am Post subject: |
|
|
| J.F. wrote: | | Once the main thread comes back, it STAYS in control until you call something that releases control again. |
I'm having still a bit of trouble here... Trying to release control from the main thread I tried several things : I created a dummy function like
and called it just after the scr_printf("*"). I guess by doing this the control still stays on the main thread.
Accidentally I realised that if I do a printf instead of calling the dummy skip function this works, but only if the SDL_Delay is commented... Could you help me understanding what's going and why the call to SDL_Delay on the main loop stops this for working?
Thanks
(PS: I also added a small delay for compensate the removal of the SDL_Delay call. So, this is the working loop :
| Code: | while(1)
{
//SDL_Delay(1000);
scr_printf(">");
printf("-");
for(i=0; i<2; i++)
gsKit_vsync();
} |
|
|
| Back to top |
|
 |
cosmito
Joined: 04 Mar 2007 Posts: 314 Location: Portugal
|
Posted: Sun Dec 02, 2007 8:30 am Post subject: |
|
|
| EEUG wrote: | | Just create your threads having the same priority and use 'iRotateThreadQueue' system call with that priority as argument in some kind of "timer interrupt handler". |
I searched the whole ps2sdk for the iRotateThreadQueue but I didn't succeeded to find it... |
|
| Back to top |
|
 |
EEUG
Joined: 13 May 2005 Posts: 136 Location: The Netherlands
|
Posted: Sun Dec 02, 2007 8:37 am Post subject: |
|
|
| ...I've made a mistake. iRotateThreadReadyQueue. Sorry... |
|
| Back to top |
|
 |
DeRieux

Joined: 06 Jul 2006 Posts: 9
|
Posted: Tue Aug 12, 2008 8:20 am Post subject: iRotateThreadReadyQueue (kernel.h) |
|
|
| cosmito wrote: | | EEUG wrote: | | Just create your threads having the same priority and use 'iRotateThreadQueue' system call with that priority as argument in some kind of "timer interrupt handler". |
I searched the whole ps2sdk for the iRotateThreadQueue but I didn't succeeded to find it... |
I believe this is referenced in kernel.h from the "ee" build.
s32 iRotateThreadReadyQueue(s32 priority);
Edited: Had listed 's32 iReleaseWaitThread(s32 thread_id);' by mistake. _________________ William DeRieux -
Have just started out using ps2sdk
Have made my Laptop, Linux Only
and am in the process of putting the pieces back together (oops!)
Email me at WilliamDeRieux@gmail.com |
|
| Back to top |
|
 |
|