package org.uberfire.commons.lock.impl;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.uberfire.commons.lock.LockService;

public class ThreadLockServiceImpl implements LockService {

    protected final AtomicReference<Thread> holder = new AtomicReference<Thread>();
    protected final AtomicInteger stackSize = new AtomicInteger();
    private final int sleep;

    public ThreadLockServiceImpl() {
        this( 50 );
    }

    public ThreadLockServiceImpl( int sleep ) {
        this.sleep = sleep;
    }

    @Override
    public synchronized void lock() {
        while ( holder.get() != null ) {
            if ( holder.get().equals( Thread.currentThread() ) ) {
                stackSize.incrementAndGet();
                return;
            }
            try {
                Thread.sleep( sleep );
            } catch ( final InterruptedException ignored ) {
            }
        }
        holder.set( Thread.currentThread() );
        stackSize.set( 1 );
    }

    @Override
    public synchronized void unlock() {
        int size = stackSize.decrementAndGet();
        if ( size == 0 ) {
            holder.set( null );
        } else if ( size < 0 ) {
            throw new IllegalMonitorStateException();
        }
    }

    @Override
    public boolean isLocked() {
        return holder.get() != null && holder.get().equals( Thread.currentThread() );
    }
}

@@ -594,37 +594,24 @@
 %0A    public 
-synchronized 
 void lock() 
@@ -1055,21 +1055,8 @@
 lic 
-synchronized 
 void

Revert "Fix for race condition in ThreadLockServiceImpl"