/*
 * Copyright 2014-2017 Higher Frequency Trading
 *
 * http://www.higherfrequencytrading.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.openhft.chronicle.queue.impl.single;

import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.queue.impl.TableStore;
import net.openhft.chronicle.queue.impl.table.AbstractTSQueueLock;
import net.openhft.chronicle.threads.TimingPauser;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;

import static net.openhft.chronicle.core.Jvm.warn;

/**
 * Implements queue lock via TableStore mechanism.
 */
public class TSQueueLock extends AbstractTSQueueLock implements QueueLock {

    private static final String LOCK_KEY = "chronicle.queue.lock";
    private static final long PID = Jvm.getProcessId();
    private final ThreadLocal<Long> lockHolderTidTL = new ThreadLocal<>();
    private final long timeout;

    public TSQueueLock(final TableStore<?> tableStore, Supplier<TimingPauser> pauser, Long timeoutMs) {
        super(LOCK_KEY, tableStore, pauser);
        timeout = timeoutMs;
    }

    /**
     * Stores current PID to table store, and any other process trying to acquire lock will wait for
     * <code>chronicle.queue.lock.timeoutMS</code> millis (default is 30000) for the lock to be released, and if it is not
     * after timeout, throws {@link IllegalStateException}. Also the locking thread ID is stored in threadLocal field, so
     * that only locking thread is allowed to {@link net.openhft.chronicle.queue.ChronicleQueue#acquireAppender} while the queue is locked.
     */
    @Override
    public void acquireLock() {
        closeCheck();
        long tid = Thread.currentThread().getId();
        try {
            while (!lock.compareAndSwapValue(UNLOCKED, PID)) {
                if (Thread.interrupted())
                    throw new IllegalStateException("Interrupted");
                pauser.pause(timeout, TimeUnit.MILLISECONDS);
            }

            // success
            lockHolderTidTL.set(tid);
        } catch (TimeoutException e) {
            warn().on(getClass(), "Couldn't acquire lock after " + timeout + "ms for the lock file:"
                    + path + ", overriding the lock. Lock was held by PID " + lock.getVolatileValue());
            forceUnlock();
            acquireLock();
        } finally {
            pauser.reset();

        }
    }

    /**
     * checks if current thread holds lock. If not, it will wait for <code>chronicle.queue.lock.timeoutMS</code> millis
     * for the lock to be released, and if it is not after timeout, throws {@link IllegalStateException}.
     */
    @Override
    public void waitForLock() {
        closeCheck();
        if (isLockHeldByCurrentThread())
            return;

        try {
            while (lock.getVolatileValue() != UNLOCKED) {
                if (Thread.interrupted())
                    throw new IllegalStateException("Interrupted");
                pauser.pause(timeout, TimeUnit.MILLISECONDS);
            }
        } catch (TimeoutException e) {
            warn().on(getClass(), "Queue lock is still held after " + timeout + "ms for the lock file:"
                    + path + ". Lock is held by PID " + lock.getVolatileValue() + ". Unlocking forcibly");
            forceUnlock();
        } catch (NullPointerException ex) {
            if (!tableStore.isClosed())
                throw ex;
            throw new IllegalStateException("The table store is closed!", ex);
        } finally {
            pauser.reset();
        }
    }

    /**
     * Checks if the lock is held by current thread and if so, releases it, removing entry from TableStore and clearing
     * ThreadLocal state, allowing anyone to proceed with {@link net.openhft.chronicle.queue.ChronicleQueue#acquireAppender}.
     */
    @Override
    public void unlock() {
        closeCheck();
        if (!isLockHeldByCurrentThread())
            throw new IllegalStateException("Can't unlock when lock is not held by this thread");

        if (!lock.compareAndSwapValue(PID, UNLOCKED)) {
            warn().on(getClass(), "Queue lock was unlocked by someone else!");
        }

        lockHolderTidTL.remove();
    }

    private boolean isLockHeldByCurrentThread() {
        long tid = Thread.currentThread().getId();
        Long lockHolderTid = lockHolderTidTL.get();
        return lockHolderTid != null && lockHolderTid == tid;
    }

}

@@ -1307,20 +1307,19 @@
 c final 
-long
+int
  PID = J
@@ -1341,83 +1341,8 @@
 ();%0A
-    private final ThreadLocal%3CLong%3E lockHolderTidTL = new ThreadLocal%3C%3E();%0A
     
@@ -2168,32 +2168,108 @@
 read().getId();%0A
+        if (isLockHeldByCurrentThread(tid)) %7B%0A            return;%0A        %7D%0A
         try %7B%0A  
@@ -2321,19 +2321,40 @@
 LOCKED, 
-PID
+getLockValueFromTid(tid)
 )) %7B%0A   
@@ -2539,70 +2539,8 @@
    %7D
-%0A%0A            // success%0A            lockHolderTidTL.set(tid);
 %0A   
@@ -2892,32 +2892,120 @@
        %7D%0A    %7D%0A%0A
+    private long getLockValueFromTid(long tid) %7B%0A        return tid %3C%3C 32 %7C PID;%0A    %7D%0A%0A
     /**%0A     * c
@@ -3290,32 +3290,83 @@
   closeCheck();%0A
+        long tid = Thread.currentThread().getId();%0A
         if (isLo
@@ -3379,32 +3379,35 @@
 ByCurrentThread(
+tid
 ))%0A            r
@@ -4520,32 +4520,83 @@
   closeCheck();%0A
+        long tid = Thread.currentThread().getId();%0A
         if (!isL
@@ -4618,16 +4618,19 @@
 tThread(
+tid
 ))%0A     
@@ -4761,19 +4761,40 @@
 apValue(
-PID
+getLockValueFromTid(tid)
 , UNLOCK
@@ -4892,43 +4892,8 @@
    %7D
-%0A%0A        lockHolderTidTL.remove();
 %0A   
@@ -4946,175 +4946,87 @@
 ead(
-) %7B%0A        long tid = Thread.currentThread().getId();%0A        Long lockHolderTid = lockHolderTidTL.get();%0A        return lockHolderTid != null && lockHolderTid == 
+long tid) %7B%0A        return lock.getVolatileValue()  == getLockValueFromTid(
 tid
+)
 ;%0A  

Make QueueLock reentrant and improve owning thread check