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 

Semaphore problem

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



Joined: 01 Feb 2006
Posts: 26
Location: Sydney

PostPosted: Wed Feb 01, 2006 9:56 pm    Post subject: Semaphore problem Reply with quote

I'm trying to write a small program to demostrate two threads writing to a file. I protect simaltenous writes to the file with a semaphore, but unfortunately I'm get some weird result.

Can anyone tell me if the following is correct

<pre>

void init_Dbug()
{
_writeSema = sceKernelCreateSema("_writeSema", 0, 0, 1, 0);
}

void write_DBug(const char * const text)
{
sceKernelWaitSema(_writeSema, 0, 0);
sceKernelSignalSema(_writeSema, 1);

fprintf(_dBugFile, text);
fflush(_dBugFile);

sceKernelSignalSema(_writeSema, 0);
}

</pre>


Ooutput file, note that 'thread 1' first write is missing, and that the 'newline' character is missing in one case.

<pre>

Thread [2] idx [0]
Thread [1] idx [1]
Thread [2] idx [1]
Thread [1] idx [2]
Thread [2] idx [2]
Thread [1] idx [3]
Thread [2] idx [3]
Thread [1] idx [4]
Thread [2] idx [4]
Thread [1] idx [5]
Thread [2] idx [5]
Thread [1] idx [6]
Thread [2] idx [6]
Thread [1] idx [7]Thread [2] idx [7]
Thread [1] idx [8]
Thread [2] idx [8]
Thread [1] idx [9]
Thread [2] idx [9]
Thread [1] idx [10]
Thread [2] idx [10]

</pre>

Does anyone know if my semaphore code is correct, and if it is why the output file seems to get incorrect data.
Back to top
View user's profile Send private message
PeterM



Joined: 31 Dec 2005
Posts: 125
Location: Edinburgh, UK

PostPosted: Thu Feb 02, 2006 1:57 am    Post subject: Reply with quote

Here's how I've done it, and it seems to work correctly:

// Create the mutex.
SceUID mutex = sceKernelCreateSema("myMutexName", 0, 1, 1, 0);

// Wait until the mutex is available. (Implicitly decrements its value)
sceKernelWaitSema(mutex, 1, 0);

// Do your protected stuff here...

// Unlock the mutex.
sceKernelSignalSema(mutex, 1);

Hope this helps, please someone correct me if it's not right.

Pete
Back to top
View user's profile Send private message Visit poster's website
ipsp



Joined: 01 Feb 2006
Posts: 26
Location: Sydney

PostPosted: Thu Feb 02, 2006 9:25 am    Post subject: I'll try that Reply with quote

PeterM wrote:
Here's how I've done it, and it seems to work correctly:

// Create the mutex.
SceUID mutex = sceKernelCreateSema("myMutexName", 0, 1, 1, 0);

// Wait until the mutex is available. (Implicitly decrements its value)
sceKernelWaitSema(mutex, 1, 0);

// Do your protected stuff here...

// Unlock the mutex.
sceKernelSignalSema(mutex, 1);

Hope this helps, please someone correct me if it's not right.

Pete


Look I'll try that, seems i thought of the semaphore the wrong way, it increments, and yes I realise this this is pretty standard.

Thanks for the help hopefully it will work,

Just quickly though do I need to use 'SceUID' or is 'int' good enough, as the docs I have indicate that most threading methods return and 'int' corresponding to the thread or semaphore.
Back to top
View user's profile Send private message
PeterM



Joined: 31 Dec 2005
Posts: 125
Location: Edinburgh, UK

PostPosted: Thu Feb 02, 2006 9:37 am    Post subject: Reply with quote

A few of the PSPSDK functions seem to take or return ints when there are appropriate enums or other types.

I guess it doesn't matter too much.

Anyway, the thing that confused me when trying semaphores for the first time was that waiting for one implicitly subtracted 1 from its value, so if you wait for it to become "1", when you get it, it will then be "0".

Then when you're done, you signal it, adding 1 to it, to make it "1" again. Then anyone else waiting for it to be "1" will get it.

Hope it helped.
Back to top
View user's profile Send private message Visit poster's website
ipsp



Joined: 01 Feb 2006
Posts: 26
Location: Sydney

PostPosted: Thu Feb 02, 2006 9:49 am    Post subject: Reply with quote

PeterM wrote:
Anyway, the thing that confused me when trying semaphores for the first time was that waiting for one implicitly subtracted 1 from its value, so if you wait for it to become "1", when you get it, it will then be "0".

Then when you're done, you signal it, adding 1 to it, to make it "1" again. Then anyone else waiting for it to be "1" will get it.

Hope it helped.


That was exactly it, I totally forgot that when you wait on the semaphore value it will decrement it once the value has been obtained, and hence if you look at my original code that is a problem.

Look I'm not sure this will fix my problem, but unfortunately the game I'm writing has a timer thread and controller thread, and they both must modify the data structure used to control the game display.

Anyways I'm sure this is the problem, thanks very much.
Back to top
View user's profile Send private message
urchin



Joined: 02 Jun 2005
Posts: 121

PostPosted: Thu Feb 02, 2006 7:42 pm    Post subject: Reply with quote

Quote:
do I need to use 'SceUID' or is 'int' good enough


'SceID' is defined as 'int' at the moment, so you could just use 'int'. However, in theory, Sony could change the type and code that used 'int' *could* break. So you're not gaining anything by using 'int', and it's good coding practice to use the right type.
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