Welcome! Log In Create A New Profile

Advanced

Using LWP_Mutex functions

Posted by DrTwox 
Using LWP_Mutex functions
January 13, 2009 12:21AM
Hi all. Forgive me if this is a daft question!

I have read the libogc mutex documentation, but I am unsure about something; When using mutex locks, which is the correct way to get a lock? Do you simply call LWP_MutexLock(lock) or do I need to use while and wait for LWP_MutexLock to return 0?
//global
mutex_t lock;

void InitFunction() {
    LWP_MutexInit(lock);
}

void Function_1() {
    LWP_MutexLock(lock);
    // Do stuff here
    LWP_MutexUnlock(lock);
}
void Function_2() {
    while (LWP_MutexLock(lock) != 0); // Do I need to wait for the lock like this?
    // Do stuff here
    LWP_MutexUnlock(lock);
}
Which is the correct method, Function_1 or Function_2?
Re: Using LWP_Mutex functions
January 13, 2009 07:09AM
I am sure that it is a blocking function, when it returns, the mutex will be locked.
Re: Using LWP_Mutex functions
January 13, 2009 10:43AM
Hi,

Function_1 is enough. Also LWP_MutexInit takes a 2nd paramter which tells whether the lock should be recursive or not.
By recursive means the thread which acquired the lock won't deadlock if LWP_MutexLock is called again.

regards
shagkur
Re: Using LWP_Mutex functions
January 13, 2009 12:52PM
Thanks for the help!
If LWP_MutexLock returns < 0 on failure, what conditions would cause a failure and should I be checking for them?
Re: Using LWP_Mutex functions
January 13, 2009 01:13PM
Hi,

LWP_MutexLock returns with !=0 not < 0 and will only happen if you try to call MutexLock another time in the same thread without having it initialized as a recursive lock.

regards
shagkur
Sorry, only registered users may post in this forum.

Click here to login