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 

Using threads and Classes

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



Joined: 09 May 2007
Posts: 28

PostPosted: Sat May 23, 2009 8:51 am    Post subject: Using threads and Classes Reply with quote

Hey,

Im back again with another annoying problem of mine...

i've created a thread in my program and a class that has private and public members.
the thread is created like this:
Code:
class Camera
{
public:
   Camera();
   ~Camera();
   int stop();
   int start();
   int fillBuffer(bool blitToScreen);
   void Init(u8* c_buf,u8* c_work,u32* c_fbuf);
   void destroy();
   bool Started;
   bool Error;
   
   u8* buffer;
private:
   PspUsbCamSetupVideoParam videoparam;
   int LoadModules();
   int UnloadModules();
   int InitCamera();
   int StartUsb();
   int StopUsb();
   int InitJpegDecoder();
   int FinishJpegDecoder();
   bool initiated;

   u8*  work;//[68*1024] __attribute__((aligned(64)));
   u32* framebuffer;//[480*272] __attribute__((aligned(64)));
};
Code:
video_thid = sceKernelCreateThread("video_thread", video_thread, 16, 16*1024, 0, NULL);
   if (video_thid < 0)
   {
      printf("Cannot create video thread.\n");
      //return -1;
   }
   
   if (sceKernelStartThread(video_thid, 0, NULL) < 0)
   {
      printf("Cannot start video thread.\n");
      //return -1;
   }

that works fine, but in the thread, when i call a class member, my psp freezes:
Code:
int video_thread(SceSize args, void *argp){
   while(true){
      if(!WebCam.Error && WebCam.Started){
            if(WebCam.FillBuffer(true)<0){
               printf("There was an error filling the buffer...\n"); 
            }
         }
       }else if(WebCam.Error){
         printf("Webcam Error...\n");
         sceKernelDelayThread(50000);
      }
   }
   sceKernelExitDeleteThread(0);
   return 0;
}

the program freezes the moment it tries to access 'WebCam.Error' or 'WebCam.Started', i've even tried calling 'WebCam.FillBuffer(true)' and still nothing.

im not 100% clear with threads yet, but i cant see anything going wrong here.

Maybe you guys can see what im doing wrong?
Cheers
Roman
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat May 23, 2009 9:23 am    Post subject: Reply with quote

You're creating a kernel thread. If you want a user thread, be sure to use PSP_THREAD_ATTR_USER when creating the thread instead of 0. Kernel level stuff can't access user level stuff without setting k1.

Code:
   unsigned int k1;
   k1 = pspSdkSetK1(0);

   // do stuff here

   pspSdkSetK1(k1);
Back to top
View user's profile Send private message AIM Address
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Sat May 23, 2009 9:42 am    Post subject: Reply with quote

so if i make a kernel thread in my main eboot does that mean i can access kernel functions
Back to top
View user's profile Send private message MSN Messenger
smartie_on_computer



Joined: 09 May 2007
Posts: 28

PostPosted: Sat May 23, 2009 10:09 am    Post subject: Reply with quote

J.F. wrote:
You're creating a kernel thread. If you want a user thread, be sure to use PSP_THREAD_ATTR_USER when creating the thread instead of 0. Kernel level stuff can't access user level stuff without setting k1.

Code:
   unsigned int k1;
   k1 = pspSdkSetK1(0);

   // do stuff here

   pspSdkSetK1(k1);


i've tried setting K1 and tried PSP_THREAD_ATTR_USER. and my psp still crashes...
Back to top
View user's profile Send private message
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Sat May 23, 2009 10:17 am    Post subject: Reply with quote

no you have to do either or not both pspSdkSetK1 if in kernel thread or set the thread to a user thread not both or else your calling a kernel funct in a user thread
Back to top
View user's profile Send private message MSN Messenger
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Sat May 23, 2009 10:18 am    Post subject: Reply with quote

do you have enoph heap allocated for this thread
Back to top
View user's profile Send private message MSN Messenger
smartie_on_computer



Joined: 09 May 2007
Posts: 28

PostPosted: Sat May 23, 2009 10:19 am    Post subject: Reply with quote

yeah i figured that part, but no i didnt do both things at once, im not that stupid to do that lol
Back to top
View user's profile Send private message
smartie_on_computer



Joined: 09 May 2007
Posts: 28

PostPosted: Sat May 23, 2009 10:20 am    Post subject: Reply with quote

coolkehon wrote:
do you have enoph heap allocated for this thread


Heap?
Back to top
View user's profile Send private message
smartie_on_computer



Joined: 09 May 2007
Posts: 28

PostPosted: Sat May 23, 2009 10:30 am    Post subject: Reply with quote

im pretty sure the heap size is fine...
the has a small amount of memory because the buffers are pointers to byte arrays defined in the main code. so the heap size should be fine.
Back to top
View user's profile Send private message
smartie_on_computer



Joined: 09 May 2007
Posts: 28

PostPosted: Sat May 23, 2009 10:58 am    Post subject: Reply with quote

Sorry, i will have to excuse myself here.

its working perfectly now, the reason why it was freezing was because i was not delaying the thread so it was looping the thread but not executing anything else.

so it works wonderfully now and thanks for your help J.F. and coolkehon!

Cheers
Roman
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat May 23, 2009 1:52 pm    Post subject: Reply with quote

I just remembered something - creating a thread from a user thread never creates a kernel thread. That's in an old thread too. I was going to suggest bumping the size of the thread's stack. When you create a thread, many people just do it like the examples... which tend to use tiny stacks as the examples don't do much. If you have functions called by the thread with large local arrays, it would be easy to overflow the stack and create a fault. Good to hear you have it working, but check your stack size anyway to make sure it doesn't cause trouble later. :)
Back to top
View user's profile Send private message AIM Address
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Sun May 24, 2009 7:56 am    Post subject: Reply with quote

could you explain what a stack is for me i've seen on other sites but it would be helpful if described in noobs term
Back to top
View user's profile Send private message MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sun May 24, 2009 9:18 am    Post subject: Reply with quote

Anytime you call a function, the return address and arguments have to be stored somewhere. The stack is a last-in, first-out buffer (or first-in, last-out) where such things are stored. The function in turn needs a place to save registers that are used by the function, and a place for temporary (local) variables. The stack is used for that as well. So the stack is a block of memory somewhere that is used by the thread for this temporary storage of addresses and registers and arguments and local variables.

The majority of the space is used by local variables. Consider this:

Code:
int foo(int bar)
{
    int myarray[1000];
    int i;

    for (i=0; i<1000; i++)
        myarray[i] = i*i + 35;

    return myarray[bar];
}


It's not how you would write such a function, but I'm making a point. The point is that function foo() has a local variable called myarray that needs memory for 1000 ints (4000 bytes). Local variables are on the stack, so when foo() is called, 4000 bytes are taken from the stack for that array. Now suppose you only allocated a block of memory that is 1024 bytes for the stack - you don't have 4000 bytes for myarray.

One of the values you pass to the CreateThread function is the size of the block of memory to allocate for the stack. It is up to you to make sure it's big enough to hold everything needed by the functions called. So if you have arrays as local variables (in particular), be sure to make the stack bigger than the total space they occupy.
Back to top
View user's profile Send private message AIM Address
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