FileDocCategorySizeDatePackage
SemaphoreImpl.javaAPI DocGlassfish v2 API5339Fri May 04 22:35:20 BST 2007com.sun.jdo.spi.persistence.utility

SemaphoreImpl

public class SemaphoreImpl extends Object implements Semaphore
Implements a simple semaphore.
author
Dave Bristor
author
Marina Vatkina

Fields Summary
private static final com.sun.jdo.spi.persistence.utility.logging.Logger
_logger
Where to log messages about locking operations
private final String
_owner
For logging, indicates on whose behalf locking is done.
private final Object
_lock
Synchronizes the lock.
private Thread
_holder
Thread which holds the lock.
private int
_counter
Semaphore counter.
private static final ResourceBundle
messages
I18N message handler
Constructors Summary
public SemaphoreImpl(String owner)



       
        _owner = owner;
    
Methods Summary
public voidacquire()
Acquire a lock.

        boolean debug = _logger.isLoggable(Logger.FINEST);

        if (debug) {
            Object[] items = new Object[] {_owner, Thread.currentThread(),new Integer(_counter)};
            _logger.finest("utility.semaphoreimpl.acquire",items); // NOI18N
        }

        synchronized (_lock) {
            //
            // If the current thread already holds this lock, we simply
            // update the count and return.
            //
            if (Thread.currentThread() == _holder) {
                _counter++;
                
            } else {
                while (_counter > 0) {
                    try {
                        // wait for the lock to be released
                        _lock.wait();
                    } catch (InterruptedException e) {
                    }
                }
                _holder = Thread.currentThread();
                _counter++;
                
                if (debug) {
                    Object[] items = new Object[] {_owner, Thread.currentThread(),new Integer(_counter)};
                    _logger.finest("utility.semaphoreimpl.gotlock",items); // NOI18N
                }
            }
        }
    
public voidrelease()
Release a lock.

        boolean debug = _logger.isLoggable(Logger.FINEST);
        
        if (debug) {
            Object[] items = new Object[] {_owner, Thread.currentThread(),new Integer(_counter)};
            _logger.finest("utility.semaphoreimpl.release",items); // NOI18N
        }
        
        synchronized (_lock) {
            //
            // If the current thread already holds this lock, we simply
            // update the count and return.
            //
            if (Thread.currentThread() == _holder) {
                if (--_counter == 0) {
                    _holder = null;
                    _lock.notify();
                }
            } else {
                throw new IllegalMonitorStateException(
                  I18NHelper.getMessage(messages,
                                        "utility.semaphoreimpl.wrongthread", // NOI18N
                                        new Object[] {_owner, Thread.currentThread()}));
            }
        }