Package EDU.oswego.cs.dl.util.concurrent

Examples of EDU.oswego.cs.dl.util.concurrent.Sync


        ReadLockImpl(ItemId id) {
            this.id = id;
        }

        public void release() {
            Sync shared = writerStateRWLock.readLock();
            for (;;) {
                try {
                    shared.acquire();
                    break;
                } catch (InterruptedException e) {
                    // try again
                    Thread.interrupted();
                }
            }
            try {
                readLockMap.removeLock(id);
                if (activeWriterThread != Thread.currentThread()) {
                    // only notify waiting writers if we do *not* hold a write
                    // lock at the same time. that would be a waste of cpu time.
                    notifyWaitingWriters();
                }
            } finally {
                shared.release();
            }
        }
View Full Code Here


        }

        // if we get here the following is true:
        // - the current thread does not hold a write lock
        for (;;) {
            Sync signal;
            // make sure writer state does not change
            Sync shared = writerStateRWLock.readLock();
            shared.acquire();
            try {
                if (activeWriter == null
                        || !hasDependency(activeWriter.changes, id)) {
                    readLockMap.addLock(id);
                    return new ReadLockImpl(id);
                } else {
                    signal = new Latch();
                    waitingReaders.add(signal);
                }
            } finally {
                shared.release();
            }

            // if we get here there was an active writer with
            // a dependency to the current id.
            // wait for the writer until it is done, then try again
View Full Code Here

     * {@inheritDoc}
     */
    public WriteLock acquireWriteLock(ChangeLog changeLog)
            throws InterruptedException {
        for (;;) {
            Sync signal;
            // we want to become the current writer
            Sync exclusive = writerStateRWLock.writeLock();
            exclusive.acquire();
            try {
                if (activeWriter == null
                        && !readLockMap.hasDependency(changeLog)) {
                    activeWriter = new WriteLockImpl(changeLog);
                    activeWriterId = getCurrentThreadId();
                    return activeWriter;
                } else {
                    signal = new Latch();
                    waitingWriters.add(signal);
                }
            } finally {
                exclusive.release();
            }
            // if we get here there is an active writer or there is a read
            // lock that conflicts with the change log
            signal.acquire();
        }
View Full Code Here

        WriteLockImpl(ChangeLog changes) {
            this.changes = changes;
        }

        public void release() {
            Sync exclusive = writerStateRWLock.writeLock();
            for (;;) {
                try {
                    exclusive.acquire();
                    break;
                } catch (InterruptedException e) {
                    // try again
                    Thread.interrupted();
                }
            }
            try {
                activeWriter = null;
                activeWriterId = null;
                notifyWaitingReaders();
                notifyWaitingWriters();
            } finally {
                exclusive.release();
            }
        }
View Full Code Here

            }
        }

        public ReadLock downgrade() {
            readLockMap.addLock(null);
            Sync exclusive = writerStateRWLock.writeLock();
            for (;;) {
                try {
                    exclusive.acquire();
                    break;
                } catch (InterruptedException e) {
                    // try again
                    Thread.interrupted();
                }
            }
            try {
                activeWriter = null;
                // only notify waiting readers since we still hold a down
                // graded lock, which is kind of exclusiv with respect to
                // other writers
                notifyWaitingReaders();
            } finally {
                exclusive.release();
            }
            return anonymousReadLock;
        }
View Full Code Here

        ReadLockImpl(ItemId id) {
            this.id = id;
        }

        public void release() {
            Sync shared = writerStateRWLock.readLock();
            for (;;) {
                try {
                    shared.acquire();
                    break;
                } catch (InterruptedException e) {
                    // try again
                    Thread.interrupted();
                }
            }
            try {
                readLockMap.removeLock(id);
                if (!isSameThreadId(activeWriterId, getCurrentThreadId())) {
                    // only notify waiting writers if we do *not* hold a write
                    // lock at the same time. that would be a waste of cpu time.
                    notifyWaitingWriters();
                }
            } finally {
                shared.release();
            }
        }
View Full Code Here

     *
     * @param key the key object
     */
    public void remove(Object key)
    {
        Sync sync = this.lock.writeLock();
        try
        {
            sync.acquire();
            try
            {
                this.doRemove(key);
                m_sizeInstrument.setValue( doGetSize() );
            }
            finally
            {
                sync.release();
            }
        }
        catch (InterruptedException ignore)
        {
        }
View Full Code Here

     * @param key the key object
     * @return true if Key exists and false if not
     */
    public boolean containsKey(Object key)
    {
        Sync sync = this.lock.readLock();
        try
        {
            sync.acquire();
            try
            {
                return this.doContainsKey(key);
            }
            finally
            {
                sync.release();
            }
        }
        catch (InterruptedException ignore)
        {
            return false;
View Full Code Here

     *
     * @return  Enumeration Object with all existing keys
     */
    public Enumeration keys()
    {
        Sync sync = this.lock.readLock();
        try
        {
            sync.acquire();
            try
            {
                return this.doGetKeys();
            }
            finally
            {
                sync.release();
            }
        }
        catch (InterruptedException ignore)
        {
            return Collections.enumeration(Collections.EMPTY_LIST);
View Full Code Here

        }
    }

    public int size()
    {
        Sync sync = this.lock.readLock();
        try
        {
            sync.acquire();
            try
            {
                return this.doGetSize();
            }
            finally
            {
                sync.release();
            }
        }
        catch (InterruptedException ignore)
        {
            return 0;
View Full Code Here

TOP

Related Classes of EDU.oswego.cs.dl.util.concurrent.Sync

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.