SDL and thread support

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Post Reply
cosmito
Posts: 307
Joined: Sun Mar 04, 2007 4:26 am
Location: Portugal
Contact:

SDL and thread support

Post by cosmito »

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: Select all

// 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&#40;&#41;
&#123;
    //Initialize all SDL subsystems
    if&#40; SDL_Init&#40; SDL_INIT_EVENTTHREAD | SDL_INIT_TIMER &#41; == -1 &#41;
    &#123;
        return 0;    
    &#125;
        
    return 1;
&#125;

/*
void clean_up&#40;&#41;
&#123;
    //Stop the thread
    SDL_KillThread&#40; thread &#41;;
        
    //Quit SDL
    SDL_Quit&#40;&#41;;    
&#125;
*/

int my_thread&#40; void *data &#41;
&#123;
    //While the program is not over
    while&#40; quit == 0 &#41;
    &#123;
        scr_printf&#40;"1"&#41;;
        SDL_Delay&#40; 1000 &#41;;
        
        scr_printf&#40;"2"&#41;;
        SDL_Delay&#40; 1000 &#41;;
        
        scr_printf&#40;"3"&#41;;
        SDL_Delay&#40; 1000 &#41;;
        
        scr_printf&#40;"4"&#41;;
        SDL_Delay&#40; 1000 &#41;;
    &#125;
    scr_printf&#40;"over"&#41;;
    return 0;    
&#125;

int main&#40;int argc, char *argv&#91;&#93;&#41;
&#123;   
    //Initialize
    SifInitRpc&#40;0&#41;;
    init_scr&#40;&#41;;

    if&#40; init&#40;&#41; == 0 &#41;
    &#123;
        return 1;    
    &#125;
  
    //Create and run the thread
    thread = SDL_CreateThread&#40; my_thread, NULL &#41;;

    //Forever
    while&#40;1&#41;
    &#123;        
        /*
        //While there's events to handle
        while&#40; SDL_PollEvent&#40; &event &#41; &#41;
        &#123;
            //If the user has Xed out the window
            if&#40; event.type == SDL_QUIT &#41;
            &#123;
                //Quit the program
                quit = 1;
            &#125;    
        &#125;
        */

        SDL_Delay&#40;5000&#41;;

        scr_printf&#40;"*"&#41;;
    &#125;
    
    // Never got here ...
    
    /*
    //Clean up
    clean_up&#40;&#41;;
    */
        
    return 0;    
&#125;
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)
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

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.
EEUG
Posts: 136
Joined: Fri May 13, 2005 4:49 am
Location: The Netherlands

Post by EEUG »

...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...
cosmito
Posts: 307
Joined: Sun Mar 04, 2007 4:26 am
Location: Portugal
Contact:

Post by cosmito »

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: Select all

void skip&#40;&#41;
&#123;
&#125;
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: Select all

    while&#40;1&#41;
    &#123;        
        //SDL_Delay&#40;1000&#41;;
        scr_printf&#40;">"&#41;;
        printf&#40;"-"&#41;;
        for&#40;i=0; i<2; i++&#41;
            gsKit_vsync&#40;&#41;;        
    &#125;
cosmito
Posts: 307
Joined: Sun Mar 04, 2007 4:26 am
Location: Portugal
Contact:

Post by cosmito »

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
Posts: 136
Joined: Fri May 13, 2005 4:49 am
Location: The Netherlands

Post by EEUG »

...I've made a mistake. iRotateThreadReadyQueue. Sorry...
User avatar
DeRieux
Posts: 9
Joined: Thu Jul 06, 2006 9:44 am

iRotateThreadReadyQueue (kernel.h)

Post by DeRieux »

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 [email protected]
Post Reply