// 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)
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.
...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...
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 :
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...
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!)