hello everyone, i have a problem on thread.

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

Moderators: cheriff, TyRaNiD

Post Reply
jcyuan
Posts: 4
Joined: Thu Jan 21, 2010 5:41 pm

hello everyone, i have a problem on thread.

Post by jcyuan »

i'm newbie here, i have a question and need your help.

while(1)
{
SceCtrlData ctl;
do {
sceKernelDelayThread(20000);
sceCtrlPeekBufferPositive(&ctl, 1);
} while((ctl.Buttons & 0xF0FFFF) != PSP_CTRL_XXX);

//do something
}

i don't quite understand of 'sceKernelDelayThread' (red above), if i call this, will this thread be suspended? or?
if suspended, when press any button on psp, will the system awake this thread automatically then?
if not, what does this code mean here? i guess it will block the thread so if i press any button the thread will don't have any response?

thanks in advance.
hope you're clear on this, my English is not very good, sorry. :(
oops
anmabagima
Posts: 87
Joined: Thu Oct 01, 2009 8:43 pm

Post by anmabagima »

Hi,

this will just make the thread do nothing for the specified time.
It has nothing to do with sleep mode of PSP. There is a thread dealing with sleep mode, check out this one...
http://forums.ps2dev.org/viewtopic.php?t=12766
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

20000ms is a little high. You'll miss some button presses. Use sceDisplayWaitVblankStart().
jcyuan
Posts: 4
Joined: Thu Jan 21, 2010 5:41 pm

Post by jcyuan »

thanks for your reply.

this code is part of the CheatMaster v0.7 source code, i think it's absolutely useful there.

this is the whole source code:

Code: Select all

#include <pspkernel.h>
#include <pspctrl.h>
#include <pspctrl_kernel.h>
#include <pspsdk.h>
#include <pspdisplay.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ctrl.h"
#include "graphic.h"
#include "menu.h"
#include "util.h"

#include "screenshot.h"

PSP_MODULE_INFO&#40;"SCEP_CM", 0x1000, 1, 2&#41;;
PSP_MAIN_THREAD_ATTR&#40;0&#41;;

#define MAX_THREAD 64

static int thread_count_start, thread_count_now, pauseuid = -1;
static SceUID thread_buf_start&#91;MAX_THREAD&#93;, thread_buf_now&#91;MAX_THREAD&#93;, thid1 = -1, thid2 = -1;

static void pause_game&#40;SceUID thid&#41;
&#123;
	if&#40;pauseuid >= 0&#41;
		return;
	pauseuid = thid;
	sceKernelGetThreadmanIdList&#40;SCE_KERNEL_TMID_Thread, thread_buf_now, MAX_THREAD, &thread_count_now&#41;;
	int x, y, match;
	for&#40;x = 0; x < thread_count_now; x++&#41;
	&#123;
		match = 0;
		SceUID tmp_thid = thread_buf_now&#91;x&#93;;
		for&#40;y = 0; y < thread_count_start; y++&#41;
		&#123;
			if&#40;&#40;tmp_thid == thread_buf_start&#91;y&#93;&#41; || &#40;tmp_thid == thid1&#41; || &#40;tmp_thid == thid2&#41;&#41;
			&#123;
				match = 1;
				break;
			&#125;
		&#125;
		if&#40;match == 0&#41;
			sceKernelSuspendThread&#40;tmp_thid&#41;;
	&#125;
&#125;

static void resume_game&#40;SceUID thid&#41;
&#123;
	if&#40;pauseuid != thid&#41;
		return;
	pauseuid = -1;
	int x, y, match;
	for&#40;x = 0; x < thread_count_now; x++&#41;
	&#123;
		match = 0;
		SceUID tmp_thid = thread_buf_now&#91;x&#93;;
		for&#40;y = 0; y < thread_count_start; y++&#41;
		&#123;
			if&#40;&#40;tmp_thid == thread_buf_start&#91;y&#93;&#41; || &#40;tmp_thid == thid1&#41; || &#40;tmp_thid == thid2&#41;&#41;
			&#123;
				match = 1;
				break;
			&#125;
		&#125;
		if&#40;match == 0&#41;
			sceKernelResumeThread&#40;tmp_thid&#41;;
	&#125;
&#125;

int ss_main_thread&#40;SceSize args, void *argp&#41; &#123;
	thid2 = sceKernelGetThreadId&#40;&#41;;
	screenshot_init&#40;&#41;;
	&#91;b&#93;while&#40;1&#41;
	&#123;
		SceCtrlData ctl;
		do &#123;
			sceKernelDelayThread&#40;20000&#41;;
			sceCtrlPeekBufferPositive&#40;&ctl, 1&#41;;
		&#125; while&#40;&#40;ctl.Buttons & 0xF0FFFF&#41; != PSP_CTRL_VOLDOWN&#41;;

		Screenshot&#40;&#41;;
	&#125;&#91;/b&#93;
	return 0;
&#125;

void start_ssthread&#40;&#41;
&#123;
	thid2 = sceKernelCreateThread&#40;"Screenshot main_thread", ss_main_thread, 47, 0x1000, 0, NULL&#41;;
	if&#40;thid2 >= 0&#41;
		sceKernelStartThread&#40;thid2, 0, 0&#41;;
&#125;

void stop_ssthread&#40;&#41;
&#123;
	sceKernelTerminateThread&#40;thid2&#41;;
	thid2 = -1;
&#125;

int main_thread&#40;SceSize args, void *argp&#41;
&#123;
	while&#40;!sceKernelFindModuleByName&#40;"sceKernelLibrary"&#41;&#41;
		&#91;b&#93;sceKernelDelayThread&#40;1000000&#41;;&#91;/b&#93;
	
	scm_option * scm = loadscm&#40;&#41;;
	
	&#91;b&#93;sceKernelDelayThread&#40;5000000&#41;;&#91;/b&#93;
	getGameInfo&#40;&#41;;
	
	ctrl_module_start&#40;0, 0&#41;;
	setrskey&#40;scm->rskey, getrskey&#40;&#41;&#41;;
	
	SceCtrlData ctl;
	while&#40;1&#41;
	&#123;
		do &#123;
			&#91;b&#93;sceKernelDelayThread&#40;20000&#41;;
			sceCtrlPeekBufferPositive&#40;&ctl, 1&#41;;
		&#125; while&#40; &#40;ctl.Buttons & scm->hotkey&#41; != scm->hotkey&#41;;
		thid1 = sceKernelGetThreadId&#40;&#41;;
		pause_game&#40;thid1&#41;;
		if &#40;initGraphic&#40;scm->com_mode&#41;&#41;
		&#123;
			setflag&#40;0&#41;;
			start_ssthread&#40;&#41;;
			drawMainMenu&#40;&#41;;
			flipScreen&#40;&#41;;
			stop_ssthread&#40;&#41;;
			setflag&#40;1&#41;;
		&#125;
		freeGraphic&#40;&#41;;
		resume_game&#40;thid1&#41;;
	&#125;&#91;/b&#93;
	return sceKernelExitDeleteThread&#40;0&#41;;
&#125;

int module_start&#40;SceSize args, void *argp&#41;
&#123;
	thid1 = thid2 = -1;
	sceKernelGetThreadmanIdList&#40;SCE_KERNEL_TMID_Thread, thread_buf_start, MAX_THREAD, &thread_count_start&#41;;
	thid1 = sceKernelCreateThread&#40;"SCM_thread", main_thread, 47, 0x1000, 0, NULL&#41;;
	if&#40;thid1 >= 0&#41;
		sceKernelStartThread&#40;thid1, 0, 0&#41;;
	return 0;
&#125;

int module_stop&#40;SceSize args, void *argp&#41;
&#123;
	return 0;
&#125;

int module_reboot_before&#40;SceSize args, void *argp&#41;
&#123;
	return 0;
&#125;
as you can see, there is a lot of similar code there (i marked them with bold).
oops
jcyuan
Posts: 4
Joined: Thu Jan 21, 2010 5:41 pm

Post by jcyuan »

sorry, it seems that bold font can't be shown in

Code: Select all

 section.
oops
Post Reply