Esempio n. 1
0
//!
//! Construct a lock for given semaphore. Wait forever if necessary.
//! numTokens tokens are acquired after the lock construction and
//! will be released when the lock is destructed. Use isOk() to verify
//! proper instantiation (i.e., to verify if tokens were successfully
//! acquired). Locking zero tokens is considered a failure.
//!
Semaphore::Lock::Lock(Semaphore& semaphore, unsigned int numTokens)
{
    numTokens_ = numTokens;
    semaphore_ = 0;
    if (numTokens > 0)
    {
        bool ok = (numTokens == 1)? semaphore.decrement(): semaphore.decrementBy(numTokens);
        if (ok)
        {
            semaphore_ = &semaphore;
        }
    }
}
Esempio n. 2
0
//!
//! Construct a lock for given semaphore. Wait forever if necessary. A
//! token is acquired after the lock construction and will be released
//! when the lock is destructed. Use isOk() to verify proper instantiation
//! (i.e., to verify if a token was successfully acquired).
//!
Semaphore::Lock::Lock(Semaphore& semaphore)
{
    numTokens_ = 1;
    semaphore_ = semaphore.decrement()? &semaphore: 0;
}