threads

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

Moderators: cheriff, TyRaNiD

Post Reply
Alberto
Posts: 51
Joined: Mon Feb 12, 2007 8:16 pm
Location: Sofia

threads

Post by Alberto »

Hi all,

sorry for the topic, but couldn't find anything better.

Here's what I need: a procedure that is being constantly called every certain amount of time (I don't care about how often, provided I can count on it being every frame-start, or 60 times a second... I think it should be enough).

I guess this may be done with a separate thread in my application (all I need to do is read the keys and/or analog pad, and store the last-read data in some variables to access them later)

I am very new to using threads (or whatever is necessary to achieve this task); can any of you please shed some light?

Thank you all in advance,
A.
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

That's pretty much what sceCtrlReadBuffer() does. It reads the keys and then reschedules your thread to run at the next vblank.
so just

Code: Select all

do{
sceCtrlReadBuffer(...);
//i get here once per vblank
...
} while (!exit_program);
does what you want.

Jim
Alberto
Posts: 51
Joined: Mon Feb 12, 2007 8:16 pm
Location: Sofia

Post by Alberto »

Jim wrote:That's pretty much what sceCtrlReadBuffer() does. It reads the keys and then reschedules your thread to run at the next vblank.
so just

Code: Select all

do{
sceCtrlReadBuffer(...);
//i get here once per vblank
...
} while (!exit_program);
does what you want.

Jim
nope... ehm, what I catually want is something like a function that gets called "automatically", without me to include "check-the-keys();" in my main loop... sorry, to that i can get ;-)
Like a callback on button-presses or on analog-nub movement, or a sort of callback called every framestart.

Actually, I was reading through the toolchain source, and found sceRegisterButtonCallback (and if there was something for the analog nub too would be great...) but cannot get to compile, as it doesn't find a reference to sce_Kernel_{hex numbers here}...

Cheers, A.
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

You can probably do something like that, but I don't know how.
Can I ask why you want that? It doesn't seem all that useful. The callback for the analog joystick sounds like a bad requirement - how far should the nub move before you get a callback, for instance.

Jim
Alberto
Posts: 51
Joined: Mon Feb 12, 2007 8:16 pm
Location: Sofia

Post by Alberto »

Jim wrote:Can I ask why you want that? It doesn't seem all that useful.
of course you can ask; just because I would like to implement something like a keys (and not only) buffer
Jim wrote:The callback for the analog joystick sounds like a bad requirement - how far should the nub move before you get a callback, for instance.
yeah, that was just a thought.

What about an interrupt handler? I've been looking at "pspintrman.h" but I cannot succeed in hooking anything: either returns negative - error - codes in USER mode, or it freezes at startup in KERNEL mode.

Can you be of any help about?

Thanks,
A.
unsigned int
Posts: 18
Joined: Thu Aug 13, 2009 11:42 pm

Post by unsigned int »

You could write your own callback mechanism using a thread that checks the keys and on changes calls another function (your callback function).

Something like this (I guess this also answers how to create a thread):

Code: Select all


void inputThread(SceSize args, void *argp)
{
  unsigned int lastButtons = 0;
  sceCtrlData currentCtrlData;
  
  while (1)
  {
    sceCtrlPeekButterPositive(&currentCtrlData, 1);

    if ((currentCtrlData.buttons && PSP_CTRL_CROSS) && !(lastButtons && PSP_CTRL_CROSS))
    {
       myButtonCallback(PSP_CTRL_CROSS, KEY_DOWN);
    }
    else if .... etc.

    lastButtons = currentCtrlData.buttons;

    sceKernelDelayThread(10000); // 10 ms
  }
}


int main()
{
  SceUID inputThreadId = sceKernelCreateThread("inputThread", (SceKernelThreadEntry)inputThread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, 0);    
  sceKernelStartThread(inputThreadId, 0, 0);

  while (1)
  {
    // whatever
  }
}
Alberto
Posts: 51
Joined: Mon Feb 12, 2007 8:16 pm
Location: Sofia

Post by Alberto »

unsigned int wrote:You could write your own callback mechanism using a thread that checks the keys and on changes calls another function (your callback function).
yes, indeed that is what I was finally worging on, today... seems the only possibility.

Anyway, as I see I cannot have two threads working at the same time, that I need to sceKernelDelayThread() both of them.
But in the while(1) loop of the main in your example, I don't see any delay for the main application thread; have you just forgotten it or is there actually a way to have things run in parallel, and I don't find it?
unsigned int wrote:

Code: Select all

int main()
{
  SceUID inputThreadId = sceKernelCreateThread("inputThread", (SceKernelThreadEntry)inputThread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, 0);    
  sceKernelStartThread(inputThreadId, 0, 0);

  while (1)
  {
    // whatever
  }
}
unsigned int
Posts: 18
Joined: Thu Aug 13, 2009 11:42 pm

Post by unsigned int »

You are correct. The PSP incorporates cooperative multithreading, so threads have to actively give up control by sleeping. If the example was compiled like this the thread would never execute after the while loop got entered.
Post Reply