 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
radad
Joined: 19 May 2004 Posts: 246 Location: Melbourne, Australia
|
Posted: Wed Oct 26, 2005 6:10 pm Post subject: semaphores |
|
|
Could someone explain to me how semaphores work?
They seem to have an initial count and a max count.
I am guessing the SignalSema increments the count by 1 (unless it is already at max) and WaitSema blocks until the count reaches max. But what happens to the count after it has unblocked? |
|
| Back to top |
|
 |
radad
Joined: 19 May 2004 Posts: 246 Location: Melbourne, Australia
|
Posted: Fri Oct 28, 2005 9:35 am Post subject: |
|
|
After some testing last night, here are my findings:
SignalSema - increases the count by 1
PollSema - decrease the count by 1
WaitSema - decreases the count by 1 but blocks if it is currently 0.
Interestingly, PollSema will decrease the count past 0 into negative values.
More interestingly, SignalSema increases the count beyond maxcount.
Does anybody understand the purpose of maxcount? |
|
| Back to top |
|
 |
Lukasz

Joined: 19 Jan 2004 Posts: 248 Location: Denmark
|
Posted: Mon Nov 14, 2005 10:06 pm Post subject: |
|
|
The bottomline is that semaphores are to make threads wait for each other. A good example of this is the npmputs.c source available with naplink from www.napalm-x.com. Because all printfs to host go through SIF ( basically the DMA channel(s) between EE and IOP), only one thread can do a printf at a time, to make this thread-safe npmput implements a semaphore.
If the sema value is 0, then Wait (it also decreaes the value as you mentioned) will put the calling thread into wait-state and it will be awoken when the value is 1 or greater. Signal increaes the value. So you Signal to allow one thread to go pass a Wait, but if a thread calls the same function before the sema has be signaled again, it will wait. Rule of thumb, Signal at init, Wait a beginning of your single-threaded function and Signal when function is done with its single-thread work, this will make it threadsafe.
As for the details of maxCount I would assume this is the max value of the semaphore aswell, why it doesn't work as expected only the coders at Sony know. I havn't seen any code on the PS2 which allows more than 1 thread to enter and maxCount also allows less-clever thread code to work, eg. you could do unnecessary Signals. _________________ Lukasz.dk |
|
| Back to top |
|
 |
|
|
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
|