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 

Thread calling function from class problem

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



Joined: 31 Dec 2005
Posts: 288

PostPosted: Fri Nov 23, 2007 1:59 am    Post subject: Thread calling function from class problem Reply with quote

Hi folks,

I am trying to create a simple loadingscreen function. I do it with the sceKernelCreateThread function which create the loading sequence rendering code while the main thread remains loading up models/pictures/variable/files etc....

anyway... that was the idea :) As in the past I have some problem regarding function pointers and the like.

The second parameter of the function sceKernelCreateThread is pointer to a function. I try to do it like this:

Code:

LoadingScreen::LoadingScreen() {
   // the loadingscreen is loaded
   thid_ = sceKernelCreateThread("LoadingThread", this->RunLoadingScreen(), 0x18, 0x10000, 0, NULL);
}


void LoadingScreen::KillLoadingScreen() {
   // shut down the loading screen again.
   sceKernelDeleteThread(thid_);
}

void LoadingScreen::RunLoadingScreen() {
   
   while(1) {
      // render loading screen


   }

}


Now calling this function returns an error with compiling.

Code:

oadingScreen.cpp: In constructor 'LoadingScreen::LoadingScreen()':
LoadingScreen.cpp:6: error: invalid use of void expression


I remember from before that there is a way of using pointers to functions from classes but I do not know anymore how exactly and I could not find it with google :(

Hope you guys can shed some light on it !

greets Ghoti
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Fri Nov 23, 2007 2:21 am    Post subject: Re: Thread calling function from class problem Reply with quote

Ghoti wrote:

Code:

   thid_ = sceKernelCreateThread("LoadingThread", this->RunLoadingScreen(), 0x18, 0x10000, 0, NULL);


What this line does is call this->RunLoadingScreen() and try to submit its return value (which is void) as the second parameter to sceKernelCreateThread.
What you want to do is this:
Code:

thid_ = sceKernelCreateThread("LoadingThread", RunLoadingScreen, 0x18, 0x10000, 0, NULL);

_________________
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
Back to top
View user's profile Send private message Visit poster's website
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Fri Nov 23, 2007 2:36 am    Post subject: Reply with quote

Hi thanks!

you are right but I also had to change the function to a static and like this:
Code:
int LoadingScreen::RunLoadingScreen(SceSize args, void *argp) {


but what I do not understand is why, why does it need the args and argp? why does it have to be an int as return value ? I really want to understand it before I will use it for whatever.

greets ghoti

EDIT::EDIT
Is there a non kernel function for creating and using thread by the way ?
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
Cpasjuste



Joined: 29 May 2005
Posts: 214

PostPosted: Fri Nov 23, 2007 2:42 am    Post subject: Reply with quote

You can create and start threads in user mode the same way!
Back to top
View user's profile Send private message
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Fri Nov 23, 2007 2:45 am    Post subject: Reply with quote

Ghoti wrote:

but what I do not understand is why, why does it need the args and argp? why does it have to be an int as return value ?

Because that's the definition of a thread entry function, same as the main program entry function main( ... ).

Quote:

Is there a non kernel function for creating and using thread by the way ?

sceKernelCreateThread is actually a user function. Just because it starts with sceKernel doesn't mean the function only works in kernel mode.
See the header files in pspsdk\src\user
_________________
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
Back to top
View user's profile Send private message Visit poster's website
Ghoti



Joined: 31 Dec 2005
Posts: 288

PostPosted: Fri Nov 23, 2007 2:51 am    Post subject: Reply with quote

Raphael wrote:
Ghoti wrote:

but what I do not understand is why, why does it need the args and argp? why does it have to be an int as return value ?

Because that's the definition of a thread entry function, same as the main program entry function main( ... ).

Quote:

Is there a non kernel function for creating and using thread by the way ?

sceKernelCreateThread is actually a user function. Just because it starts with sceKernel doesn't mean the function only works in kernel mode.
See the header files in pspsdk\src\user

Yes your right about the main function :) forgot about that, for the PSP I always just pass void to the main function.

I yes I should have found that in the header files, I just assumed without checking if there is kernel in the name it is a kernel function :s will not do that anymore :D
_________________
My PSP games:

Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Fri Nov 23, 2007 3:58 am    Post subject: Reply with quote

The reason it needs to be static is due to C++ calling conventions. There are some tricks you can do to fudge it but normally best to do something akin to:

Code:
class MyClass
{
public:
    static int ThreadEntry(int args, void *argp) {
         MyClass *this = *((MyClass **) argp;

         this->RunLoadingScreen();
         return 0;
    }
}

MyClass* p = new MyClass;
uid = sceKernelCreateThread("MyClassThread", MyClass::ThreadEntry, ...);
sceKernelStartThread(uid, sizeof(p), &p);
Back to top
View user's profile Send private message
pspZorba



Joined: 22 Sep 2007
Posts: 156
Location: NY

PostPosted: Fri Dec 14, 2007 8:27 am    Post subject: Reply with quote

Does it mean that the real signature of a thread entry is :

int threadEntry( int argc, void *argv[] )

rather than

int threadEntry( int argc, void *argv )
_________________
--pspZorba--
NO to K1.5 !
Back to top
View user's profile Send private message
pspZorba



Joined: 22 Sep 2007
Posts: 156
Location: NY

PostPosted: Fri Dec 14, 2007 8:33 am    Post subject: Reply with quote

froget about the question.

I understood my mistake
_________________
--pspZorba--
NO to K1.5 !
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP 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