Threads / Kernel mode error

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

Moderators: cheriff, TyRaNiD

Post Reply
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Threads / Kernel mode error

Post by Insert_witty_name »

Hi.

I'm trying to get WiFi code working in my game. Before this the game was running pure usermode and worked perfectly.

Now I'm trying to load the net modules, the game is having errors.

The LoadMainMenu() function is being run correctly, except that if I try and create a new player, or load a previously saved player, the PSP crashes - it's as though file io has stopped working.

Thanks in advance for looking at the code.

Code: Select all

// INCLUDES
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <stdio.h>
#include <pspsdk.h>
#include "graphics.h"
#include <psppower.h>
#include "mainmenu.h"
#include "functions.h"
#include "sound.h"
#include "wifi.h"

// DEFINES
#define RGB&#40;r,g,b&#41; &#40;&#40;&#40;r&#41; << 16&#41; | &#40;&#40;g&#41; << 8&#41; | &#40;b&#41;&#41;

// GLOBAL VARIABLES
char BornOn&#91;255&#93;; //ADOPTED DATE & TIME
int GreenBarLength&#91;5&#93;; //ARRAY FOR LENGTH OF GREEN BARS
int PlayerFileLines&#91;10&#93;; //NUMBER OF LINES IN THE SAVE FILE
char PlayerName&#91;8&#93;; //PLAYER NAME
char GameSavesName&#91;100&#93;&#91;255&#93;; //GAME SAVES NAMES
char TrackName&#91;100&#93;&#91;255&#93;; //TRACK NAMES FOR DANCE OFF
Color WhiteColor = RGB&#40;255, 255, 255&#41;; //COLOR WHITE
SceCtrlData pad, lastpad; //CONTROLS & OLD CONTROLS
int AddToEnergy = 0; //MODIFIER FOR ENERGY
int AddToHunger = 0; //MODIFIER FOR HUNGER
int AddToFun = 0; //MODIFIER FOR FUN
int AddToHygiene = 0; //MODIFIER FOR HYGIENE
Image* LetterImg&#91;27&#93;; //IMAGES FOR LETTERS
Image* PetIdle; //IMAGE FOR PET NOT BLINKING
Image* PetBlink; //IMAGE FOR PET BLINKING
SceKernelUtilsMt19937Context ctx; //MERSENNE TWISTER FOR RANDOM NUMBERS
int NumPerfectScores&#91;3&#93;; //HOLDER FOR NUMBER OF PERFECT SCORES
int GameTime = 0; //GAME TIME &#40;3600 TO A MINUTE&#41;

// MODULE INFO
PSP_MODULE_INFO&#40;"AniMate", 0x1000, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;0&#41;;
PSP_MAIN_THREAD_STACK_SIZE_KB&#40;32&#41;; /* smaller stack for kernel thread */

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
          WAV_End&#40;&#41;;
          sceKernelExitGame&#40;&#41;;
          return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
          int cbid;

          cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
          sceKernelRegisterExitCallback&#40;cbid&#41;;

          sceKernelSleepThreadCB&#40;&#41;;

          return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41; &#123;
          int thid = 0;

          thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
          if&#40;thid >= 0&#41; &#123;
                    sceKernelStartThread&#40;thid, 0, 0&#41;;
          &#125;

          return thid;
&#125;

// START MAIN
int user_main&#40;SceSize argc, void* argv&#41;
&#123;
          SetupCallbacks&#40;&#41;;
          scePowerSetClockFrequency&#40;333, 333, 166&#41;; //SOFTWARE DECODING OF MUSIC SLOWS DOWN CPU, UGH!
          initGraphics&#40;&#41;;

          //pspAudioInit&#40;&#41;;
          WAV_Init&#40;&#41;;

          int i;  //FOR 'FOR' LOOP
          char buffer&#91;255&#93;; //CHARACTER BUFFER

          //MAIN MENU--NEW GAME LETTERS
          extern Image* LetterImg&#91;27&#93;;
          for &#40;i = 0; i < 27; i++&#41; &#123;
          sprintf&#40;buffer, "./Images/Letters/%i.png", i&#41;;
          LetterImg&#91;i&#93; = Load_Image&#40;buffer&#41;;
          &#125;
          
          //PET IDLE
          extern Image* PetIdle;
          PetIdle = Load_Image&#40;"./Images/Pet/PetIdle.png"&#41;;

          //PET BLINK
          extern Image* PetBlink;
          PetBlink = Load_Image&#40;"./Images/Pet/PetBlink.png"&#41;;

          while&#40;1&#41;
                  &#123;
                  LoadMainMenu&#40;&#41;;
                  &#125;
                  
          //CLEAN UP ON EXIT
          for &#40;i = 0; i < 27; i++&#41;
          freeImage&#40;LetterImg&#91;i&#93;&#41;;

          freeImage&#40;PetIdle&#41;;
          freeImage&#40;PetBlink&#41;;

   return 0;
&#125;

int main&#40;void&#41;
&#123;
   pspDebugScreenInit&#40;&#41;;
   int err = pspSdkLoadInetModules&#40;&#41;;

   if &#40;err != 0&#41; &#123;
      pspDebugScreenPrintf&#40;"pspSdkLoadInetModules failed with %x\n", err&#41;;
      sceKernelDelayThread&#40;5*1000000&#41;; // 5 sec to read error
      //return 1;
   &#125;

   // create user thread, tweek stack size here if necessary
   SceUID thid = sceKernelCreateThread&#40;"User Mode Thread", user_main,
      0x11, // default priority
      256 * 1024, // stack size &#40;256KB is regular default&#41;
      PSP_THREAD_ATTR_USER, NULL&#41;;

   // start user thread, then wait for it to do everything else
   sceKernelStartThread&#40;thid, 0, NULL&#41;;
   sceKernelWaitThreadEnd&#40;thid, NULL&#41;;

   sceKernelExitGame&#40;&#41;;

   return 0;

&#125;
mbf
Posts: 55
Joined: Fri Aug 18, 2006 7:43 pm

Post by mbf »

have you tried this for your initial kernel thread:

Code: Select all

    sceKernelStartThread&#40;thid, 0, NULL&#41;;
    sceKernelExitDeleteThread&#40;0&#41;;
    return 0;
instead of:

Code: Select all

   sceKernelStartThread&#40;thid, 0, NULL&#41;;
   sceKernelWaitThreadEnd&#40;thid, NULL&#41;;

   sceKernelExitGame&#40;&#41;;

   return 0;
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Thanks for the reply mbf, unfortunately I'm still getting the same errors.

I've narrowed the error down somewhat.

I'm loading all my external files (images/sounds etc) using relative paths ie. "./Sounds/mysound.wav".

The sounds load fine, as do the images, it's when I'm trying to do something like create a file or list the files in a directory when I'm getting errors.

It's like my relative path is getting messed up somehow.

Anybody have any ideas please?

PS. When I move all my code from 'user_main' into 'main', it works flawlessly.
mbf
Posts: 55
Joined: Fri Aug 18, 2006 7:43 pm

Post by mbf »

Mhh... weird. You can check the current directory with the cwd() function, from main() and from user_main(). I don't see why it would be different though.

Also check your heap size (shouldn't be an issue either).

What kind of errors do you get?
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Insert_witty_name wrote:The sounds load fine, as do the images, it's when I'm trying to do something like create a file or list the files in a directory when I'm getting errors.

It's like my relative path is getting messed up somehow.
Are you using sceIo* or POSIX calls? The sceIo functions are broken in many ways regarding relative paths. We worked around many known issues when we implemented the standard calls in newlib.
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

jimparis wrote:Are you using sceIo* or POSIX calls? The sceIo functions are broken in many ways regarding relative paths. We worked around many known issues when we implemented the standard calls in newlib.
I use both in different functions, neither works.

mbf: Basically my game crashes when trying to list the files in a directory, or create a new file.

I briefly messed with getcwd() and chdir() this morning to try and isolate where the error is coming from.

I'll mess about with it again when I come back from work.

Thank you both for you assistance.
Fanjita
Posts: 217
Joined: Wed Sep 28, 2005 9:31 am

Post by Fanjita »

Insert_witty_name wrote:Basically my game crashes when trying to list the files in a directory, or create a new file.
If you're using the native sceIo* function for reading directories, make sure that you have read the other threads here about it. You can expect odd problems if you pass the necessary structure on the stack, or don't properly initialise it to 0s.
Got a v2.0-v2.80 firmware PSP? Download the eLoader here to run homebrew on it!
The PSP Homebrew Database needs you!
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

I'll convert all my sceIo* calls to POSIX and see how it goes.

This is my GetGameSaves() function, can't see anything wrong with it:

Code: Select all

int GetGameSaves&#40;&#41;
&#123;

int dfd;
SceIoDirent GameSavesDir;
memset&#40;&GameSavesDir, 0, sizeof&#40;GameSavesDir&#41;&#41;;
extern char GameSavesName&#91;100&#93;&#91;255&#93;;
int GameSaveCounter = 0;
int MaxGameSaves = 0;

dfd = sceIoDopen&#40;"./GameSaves/"&#41;;

while&#40;sceIoDread&#40;dfd, &GameSavesDir&#41; > 0&#41;
&#123;
sprintf&#40;GameSavesName&#91;GameSaveCounter&#93;,"%s",GameSavesDir.d_name&#41;;
MaxGameSaves = GameSaveCounter;
GameSaveCounter++;
&#125;
sceIoDclose&#40;dfd&#41;;

return MaxGameSaves;
&#125;
[EDIT] Actually on a second glance through my code, it seems that the POSIX stuff is working (image/sound etc loading) it's just the sceIO calls that aren't. I'll convert them over to POSIX and report back.

Thanks for the point in the right direction.
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Ok, I've changed all my file IO to newlib calls and all is well on the file io front.

But now for some reason my music/sound is choppy in places when it wasn't before (in user mode)

Any suggestions as to what could be causing it?
Fanjita
Posts: 217
Joined: Wed Sep 28, 2005 9:31 am

Post by Fanjita »

Checked the relative thread priorities? Don't forget that the scheduler on the PSP is also not as pre-emptive as you would like...
Got a v2.0-v2.80 firmware PSP? Download the eLoader here to run homebrew on it!
The PSP Homebrew Database needs you!
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

The only threads I'm using are the ones that are in my original post.

I'm going to try and optimise my code some more and see if that solves the issue (the sound only goes choppy when the CPU is under load).

Thanks for the help on this, It's all been pretty smooth until this whole threading fiasco.
jockyw2001
Posts: 339
Joined: Thu Sep 29, 2005 4:19 pm

Post by jockyw2001 »

try this:

Code: Select all

// create user thread, tweek stack size here if necessary 
   SceUID thid = sceKernelCreateThread&#40;"User Mode Thread", user_main, 
      8, // default priority 
      256 * 1024, // stack size &#40;256KB is regular default&#41; 
      PSP_THREAD_ATTR_USER, NULL&#41;; 
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Thanks for that jockyw2001 - using that, the other ideas in this thread and optimising my code has removed the stuttering in my sounds.

Thanks to all.
Post Reply