forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SDL and thread support

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
cosmito



Joined: 04 Mar 2007
Posts: 314
Location: Portugal

PostPosted: Thu Nov 29, 2007 9:29 am    Post subject: SDL and thread support Reply with quote

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
View user's profile Send private message Visit poster's website
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Thu Nov 29, 2007 11:24 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
EEUG



Joined: 13 May 2005
Posts: 136
Location: The Netherlands

PostPosted: Fri Nov 30, 2007 7:33 pm    Post subject: Reply with quote

...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
View user's profile Send private message
cosmito



Joined: 04 Mar 2007
Posts: 314
Location: Portugal

PostPosted: Sun Dec 02, 2007 8:27 am    Post subject: Reply with quote

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
Code:
void skip()
{
}
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
View user's profile Send private message Visit poster's website
cosmito



Joined: 04 Mar 2007
Posts: 314
Location: Portugal

PostPosted: Sun Dec 02, 2007 8:30 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
EEUG



Joined: 13 May 2005
Posts: 136
Location: The Netherlands

PostPosted: Sun Dec 02, 2007 8:37 am    Post subject: Reply with quote

...I've made a mistake. iRotateThreadReadyQueue. Sorry...
Back to top
View user's profile Send private message
DeRieux



Joined: 06 Jul 2006
Posts: 9

PostPosted: Tue Aug 12, 2008 8:20 am    Post subject: iRotateThreadReadyQueue (kernel.h) Reply with quote

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
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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