// Path: src/main/java/com/github/mottox/taomp/common/ThreadID.java
// public class ThreadID {
// 
//     private static Map<Long, Integer> threadIDs = new ConcurrentHashMap<>();
// 
//     private static AtomicInteger counter = new AtomicInteger();
// 
//     public static int get() {
//         long id = Thread.currentThread().getId();
//         return threadIDs.computeIfAbsent(id, key -> counter.getAndIncrement());
//     }
// }


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.github.mottox.taomp.common.ThreadID;
package com.github.mottox.taomp.concurrent.locks;



/**
 * 简单的可重入锁。
 */
public class SimpleReentrantLock implements SimpleLock {

    private Lock lock;

    private Condition condition;

    private volatile int owner;

    private volatile int holdCount;

    public SimpleReentrantLock() {
        // 只是借用java.util.concurrent.ReentrantLock来实现互斥。
        this.lock = new ReentrantLock();
        this.condition = lock.newCondition();
        this.owner = -1;
        this.holdCount = 0;
    }

    @Override
    public void lock() {
// Path: src/main/java/com/github/mottox/taomp/common/ThreadID.java
// public class ThreadID {
// 
//     private static Map<Long, Integer> threadIDs = new ConcurrentHashMap<>();
// 
//     private static AtomicInteger counter = new AtomicInteger();
// 
//     public static int get() {
//         long id = Thread.currentThread().getId();
//         return threadIDs.computeIfAbsent(id, key -> counter.getAndIncrement());
//     }
// }


// Path: src/main/java/com/github/mottox/taomp/concurrent/locks/SimpleReentrantLock.java
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.github.mottox.taomp.common.ThreadID;

package com.github.mottox.taomp.concurrent.locks;



/**
 * 简单的可重入锁。
 */
public class SimpleReentrantLock implements SimpleLock {

    private Lock lock;

    private Condition condition;

    private volatile int owner;

    private volatile int holdCount;

    public SimpleReentrantLock() {
        // 只是借用java.util.concurrent.ReentrantLock来实现互斥。
        this.lock = new ReentrantLock();
        this.condition = lock.newCondition();
        this.owner = -1;
        this.holdCount = 0;
    }

    @Override
    public void lock() {
        int me = ThreadID.get();