version 1.10, 2008/04/16 07:13:47
|
version 1.12, 2008/08/15 17:24:52
|
|
|
} | } |
} | } |
| |
void Mutex::try_lock() |
Boolean Mutex::try_lock() |
{ | { |
PEGASUS_DEBUG_ASSERT(_magic); | PEGASUS_DEBUG_ASSERT(_magic); |
| |
int r = pthread_mutex_trylock(&_rep.mutex); | int r = pthread_mutex_trylock(&_rep.mutex); |
if (r == -1) | if (r == -1) |
r=errno; | r=errno; |
switch (r) |
|
|
if (r == 0) |
{ | { |
case 0: |
|
#if defined(PEGASUS_DEBUG) | #if defined(PEGASUS_DEBUG) |
_rep.count++; | _rep.count++; |
#endif | #endif |
break; |
return true; |
|
} |
| |
case EBUSY: |
if (r == EBUSY) |
throw AlreadyLocked(Threads::self()); |
{ |
|
return false; |
|
} |
| |
default: |
|
throw WaitFailed(Threads::self()); | throw WaitFailed(Threads::self()); |
} | } |
} |
|
| |
void Mutex::timed_lock(Uint32 milliseconds) |
Boolean Mutex::timed_lock(Uint32 milliseconds) |
{ | { |
PEGASUS_DEBUG_ASSERT(_magic); | PEGASUS_DEBUG_ASSERT(_magic); |
| |
|
|
int r=pthread_mutex_trylock(&_rep.mutex); | int r=pthread_mutex_trylock(&_rep.mutex); |
if (r == -1) | if (r == -1) |
r = errno; | r = errno; |
switch (r) |
|
{ |
|
case 0: |
|
#if defined(PEGASUS_DEBUG) |
|
_rep.count++; |
|
#endif |
|
return; |
|
| |
case EBUSY: |
if (r == 0) |
|
{ |
|
break; |
|
} |
|
else if (r == EBUSY) |
{ | { |
gettimeofday(&now, NULL); | gettimeofday(&now, NULL); |
| |
if (Time::subtract(&remaining, &finish, &now)) | if (Time::subtract(&remaining, &finish, &now)) |
throw TimeOut(Threads::self()); |
{ |
|
return false; |
|
} |
| |
Threads::yield(); | Threads::yield(); |
break; |
|
} | } |
|
else |
default: |
{ |
throw WaitFailed(Threads::self()); | throw WaitFailed(Threads::self()); |
} | } |
} | } |
|
|
|
#if defined(PEGASUS_DEBUG) |
|
_rep.count++; |
|
#endif |
|
return true; |
} | } |
| |
void Mutex::unlock() | void Mutex::unlock() |
|
|
_rep.count++; | _rep.count++; |
} | } |
| |
void Mutex::try_lock() |
Boolean Mutex::try_lock() |
{ | { |
PEGASUS_DEBUG_ASSERT(_magic); | PEGASUS_DEBUG_ASSERT(_magic); |
| |
DWORD rc = WaitForSingleObject(_rep.handle, 0); | DWORD rc = WaitForSingleObject(_rep.handle, 0); |
| |
if (rc == WAIT_TIMEOUT) | if (rc == WAIT_TIMEOUT) |
throw AlreadyLocked(Threads::self()); |
{ |
|
return false; |
|
} |
| |
if (rc == WAIT_FAILED) | if (rc == WAIT_FAILED) |
|
{ |
throw WaitFailed(Threads::self()); | throw WaitFailed(Threads::self()); |
|
} |
| |
_rep.count++; | _rep.count++; |
|
return true; |
} | } |
| |
void Mutex::timed_lock(Uint32 milliseconds) |
Boolean Mutex::timed_lock(Uint32 milliseconds) |
{ | { |
PEGASUS_DEBUG_ASSERT(_magic); | PEGASUS_DEBUG_ASSERT(_magic); |
| |
DWORD rc = WaitForSingleObject(_rep.handle, milliseconds); | DWORD rc = WaitForSingleObject(_rep.handle, milliseconds); |
| |
if (rc == WAIT_TIMEOUT) | if (rc == WAIT_TIMEOUT) |
throw TimeOut(Threads::self()); |
return false; |
| |
if (rc == WAIT_FAILED) | if (rc == WAIT_FAILED) |
throw WaitFailed(Threads::self()); | throw WaitFailed(Threads::self()); |
| |
_rep.count++; | _rep.count++; |
|
return true; |
} | } |
| |
void Mutex::unlock() | void Mutex::unlock() |