AQS-源码分析(一)-------排它锁

最近仔细学习了一下java AbstractQueuedSynchronizer 的源码,这里来一起分享下,如果有错误的地方,欢迎指正!

我们将会从一下几个方面介绍:

 

一、简介

AQS是Doug Lea 大师创作用来构建锁或其他同步组件的基础框架类。AQS主要采用模板方法实现同步原语,以简化并发工具的内部实现,AQS主要做了三件事情:

  • 同步状态的管理,主要是通过state变量
  • 线程的阻塞和唤醒,通过LockSupport.park 和 unpark方法
  • 内部队列的维护,Condition队列 和 CLH队列

下面三个protected final方法是AQS中用来访问/修改同步状态的方法: 

 /**
     * The synchronization state.
     */
    private volatile int state;

    /**
     * Returns the current value of synchronization state.
     * This operation has memory semantics of a {@code volatile} read.
     * @return current state value
     */
    protected final int getState() {
        return state;
    }

    /**
     * Sets the value of synchronization state.
     * This operation has memory semantics of a {@code volatile} write.
     * @param newState the new state value
     */
    protected final void setState(int newState) {
        state = newState;
    }

    /**
     * Atomically sets synchronization state to the given updated
     * value if the current state value equals the expected value.
     * This operation has memory semantics of a {@code volatile} read
     * and write.
     *
     * @param expect the expected value
     * @param update the new value
     * @return {@code true} if successful. False return indicates that the actual
     *         value was not equal to the expected value.
     */
    protected final boolean compareAndSetState(int expect, int update) {
        // See below for intrinsics setup to support this
        return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
    }

Condition 中的 node 在两种情况下会加入到 CLH :(下文会讲到)

  1. 正常流程中调用 signal 方法
  2. 发生中断,在 await 的时候也会加入到 CLH 中

二、API

在自定义基于AQS的同步工具时,我们可以选择覆盖实现以下几个方法来实现同步状态的管理,以下方法在 AQS中没有实现, 在AQS中会抛出UnsupportedOperationException异常,即把获取和释放的具体逻辑由子类来实现(主要是对state 变量的操作):

方法描述
boolean tryAcquire(int arg)试图获取独占锁
boolean tryRelease(int arg)试图释放独占锁
int tryAcquireShared(int arg)试图获取共享锁
boolean tryReleaseShared(int arg)试图释放共享锁
boolean isHeldExclusively()当前线程是否获得了独占锁

 

 

 

 

 

 

 

AQS本身将同步状态的管理用模板方法模式都封装好了,以下列举了AQS中的一些模板方法: 

方法描述
void acquire(int arg)获取独占锁。会调用tryAcquire方法,如果 未获取成功,则会进入同步队列等待
void acquireInterruptibly(int arg)响应中断版本的acquire
boolean tryAcquireNanos(int arg,long nanos)响应中断+带超时版本的acquire
void acquireShared(int arg)获取共享锁。会调用tryAcquireShared方法
void acquireSharedInterruptibly(int arg)响应中断版本的acquireShared
boolean tryAcquireSharedNanos(int arg,long nanos)响应中断+带超时版本的acquireShared
boolean release(int arg)释放独占锁
boolean releaseShared(int arg)释放共享锁
Collection getQueuedThreads()获取同步队列上的线程集合

 

 

 

 

 

 

 

 

 

 

 

上面看上去很多方法,其实从语义上来区分就是获取和释放,从模式上区分就是独占式和共享式,从中断相应 上来看就是支持和不支持。 AQS为在独占模式和共享模式下获取锁分别提供三种获取方式:不响应线程中断获取,响应线程中断获取,设 置超时时间获取。

三、内部类

1. Node

static final class Node {
        /** Marker to indicate a node is waiting in shared mode */
        static final Node SHARED = new Node();
        /** Marker to indicate a node is waiting in exclusive mode */
        static final Node EXCLUSIVE = null;

        /** waitStatus value to indicate thread has cancelled */
        static final int CANCELLED =  1;
        /** waitStatus value to indicate successor's thread needs unparking */
        static final int SIGNAL    = -1;
        /** waitStatus value to indicate thread is waiting on condition */
        static final int CONDITION = -2;
        /**
         * waitStatus value to indicate the next acquireShared should
         * unconditionally propagate
         */
        static final int PROPAGATE = -3;

        /**
         * Status field, taking on only the values:
         *   SIGNAL:     The successor of this node is (or will soon be)
         *               blocked (via park), so the current node must
         *               unpark its successor when it releases or
         *               cancels. To avoid races, acquire methods must
         *               first indicate they need a signal,
         *               then retry the atomic acquire, and then,
         *               on failure, block.
         *   CANCELLED:  This node is cancelled due to timeout or interrupt.
         *               Nodes never leave this state. In particular,
         *               a thread with cancelled node never again blocks.
         *   CONDITION:  This node is currently on a condition queue.
         *               It will not be used as a sync queue node
         *               until transferred, at which time the status
         *               will be set to 0. (Use of this value here has
         *               nothing to do with the other uses of the
         *               field, but simplifies mechanics.)
         *   PROPAGATE:  A releaseShared should be propagated to other
         *               nodes. This is set (for head node only) in
         *               doReleaseShared to ensure propagation
         *               continues, even if other operations have
         *               since intervened.
         *   0:          None of the above
         *
         * The values are arranged numerically to simplify use.
         * Non-negative values mean that a node doesn't need to
         * signal. So, most code doesn't need to check for particular
         * values, just for sign.
         *
         * The field is initialized to 0 for normal sync nodes, and
         * CONDITION for condition nodes.  It is modified using CAS
         * (or when possible, unconditional volatile writes).
         */
        volatile int waitStatus;

        /**
         * Link to predecessor node that current node/thread relies on
         * for checking waitStatus. Assigned during enqueuing, and nulled
         * out (for sake of GC) only upon dequeuing.  Also, upon
         * cancellation of a predecessor, we short-circuit while
         * finding a non-cancelled one, which will always exist
         * because the head node is never cancelled: A node becomes
         * head only as a result of successful acquire. A
         * cancelled thread never succeeds in acquiring, and a thread only
         * cancels itself, not any other node.
         */
        volatile Node prev;

        /**
         * Link to the successor node that the current node/thread
         * unparks upon release. Assigned during enqueuing, adjusted
         * when bypassing cancelled predecessors, and nulled out (for
         * sake of GC) when dequeued.  The enq operation does not
         * assign next field of a predecessor until after attachment,
         * so seeing a null next field does not necessarily mean that
         * node is at end of queue. However, if a next field appears
         * to be null, we can scan prev's from the tail to
         * double-check.  The next field of cancelled nodes is set to
         * point to the node itself instead of null, to make life
         * easier for isOnSyncQueue.
         */
        volatile Node next;

        /**
         * The thread that enqueued this node.  Initialized on
         * construction and nulled out after use.
         */
        volatile Thread thread;

        /**
         * Link to next node waiting on condition, or the special
         * value SHARED.  Because condition queues are accessed only
         * when holding in exclusive mode, we just need a simple
         * linked queue to hold nodes while they are waiting on
         * conditions. They are then transferred to the queue to
         * re-acquire. And because conditions can only be exclusive,
         * we save a field by using special value to indicate shared
         * mode.
         */
        Node nextWaiter;

        /**
         * Returns true if node is waiting in shared mode.
         */
        final boolean isShared() {
            return nextWaiter == SHARED;
        }

        /**
         * Returns previous node, or throws NullPointerException if null.
         * Use when predecessor cannot be null.  The null check could
         * be elided, but is present to help the VM.
         *
         * @return the predecessor of this node
         */
        final Node predecessor() throws NullPointerException {
            Node p = prev;
            if (p == null)
                throw new NullPointerException();
            else
                return p;
        }

        Node() {    // Used to establish initial head or SHARED marker
        }

        Node(Thread thread, Node mode) {     // Used by addWaiter
            this.nextWaiter = mode;
            this.thread = thread;
        }

        Node(Thread thread, int waitStatus) { // Used by Condition
            this.waitStatus = waitStatus;
            this.thread = thread;
        }
    }

代码中的注释写的已经很清楚了,我们主要来看下这几个状态,个人觉得状态的设置在一些业务系统中也很有作用。

英文取值语义
CANCELLED1因为超时或者中断,结点会被设置为取消状态,被取消状态的结点不应该去竞 争锁,只能保持取消状态不变,不能转换为其他状态。处于这种状态的结点会 被踢出队列,被GC回收;
SIGNAL-1表示这个结点的继任结点被阻塞了,到时需要通知它
CONDITION-2表示这个结点在条件队列中,因为等待某个条件而被阻塞
PROPAGATE-3使用在共享模式头结点有可能牌处于这种状态,表示锁的下一次获取可以无条件传播
None of the above0新结点会处于这种状态

 

 

 

 

 

 

 

 

2. ConditionObject

/**
     * Condition implementation for a {@link
     * AbstractQueuedSynchronizer} serving as the basis of a {@link
     * Lock} implementation.
     *
     * <p>Method documentation for this class describes mechanics,
     * not behavioral specifications from the point of view of Lock
     * and Condition users. Exported versions of this class will in
     * general need to be accompanied by documentation describing
     * condition semantics that rely on those of the associated
     * {@code AbstractQueuedSynchronizer}.
     *
     * <p>This class is Serializable, but all fields are transient,
     * so deserialized conditions have no waiters.
     */
    public class ConditionObject implements Condition, java.io.Serializable {
        private static final long serialVersionUID = 1173984872572414699L;
        /** First node of condition queue. */
        private transient Node firstWaiter;
        /** Last node of condition queue. */
        private transient Node lastWaiter;

        /**
         * Creates a new {@code ConditionObject} instance.
         */
        public ConditionObject() { }

      
    }

这个类主要提供了一些在条件队列里边阻塞(await)唤醒(singal)的方法《AQS-源码分析 (三)》会讲到,注意Condition队列只是在一些特定的实现类工具中才会被使用到。AQS中只有一个阻塞队列,但是可以有多个条件队列

 

四,源码分析

1.acquire  获取独占锁的实现

这个方法首先会尝试获取锁,成功直接返回,否则会把当前线程包装成node添加到阻塞队列中,并检测如果是head节点的直接后继,则再次尝试获取锁,如果获取失败,则会通过LockSupport阻塞当前线程,直至被释放锁的线程唤醒或者被中断,随后再次尝试获取锁,如此反复

/**
     * Acquires in exclusive mode, ignoring interrupts.  Implemented
     * by invoking at least once {@link #tryAcquire},
     * returning on success.  Otherwise the thread is queued, possibly
     * repeatedly blocking and unblocking, invoking {@link
     * #tryAcquire} until success.  This method can be used
     * to implement method {@link Lock#lock}.
     *
     * @param arg the acquire argument.  This value is conveyed to
     *        {@link #tryAcquire} but is otherwise uninterpreted and
     *        can represent anything you like.
     */
    public final void acquire(int arg) {
        if (!tryAcquire(arg) &&
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }

 

 addWaiter  在队列中新增一个节点

**
     * Creates and enqueues node for current thread and given mode.
     *
     * @param mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared
     * @return the new node
     */
    private Node addWaiter(Node mode) {
        Node node = new Node(Thread.currentThread(), mode);
        // Try the fast path of enq; backup to full enq on failure
        // 首先会通过CAS的方式先尝试插入一次,
        Node pred = tail;
        if (pred != null) {
            node.prev = pred;
            if (compareAndSetTail(pred, node)) {
                pred.next = node;
                return node;
            }
        }
        // 初始情况或者在快速尝试失败后插入节点
        enq(node);
        return node;
    }

enq  通过循环+CAS在队列中成功插入一个节点后返回

/**
     * Inserts node into queue, initializing if necessary. See picture above.
     * @param node the node to insert
     * @return node's predecessor
     */
    private Node enq(final Node node) {
        for (;;) {
            Node t = tail;
            if (t == null) { // Must initialize
                // 初始化head和tail
                if (compareAndSetHead(new Node()))
                    tail = head;
            } else {
                /*
                 * 此处的else分支是先在CAS的if前设置node.prev = t,而不是在CAS成功之后再设置。
                 * 这样子做的好处是:
                 * 保证每时每刻tail.prev都不会是一个null值,否则如果node.prev = t
                 * 放在下面if的里面,会导致一个瞬间tail.prev = null,这样会使得队列不完整。
                 */
                 node.prev = t;
                 // CAS设置tail为node,成功后把老的tail也就是t连接到node。
                if (compareAndSetTail(t, node)) {
                    t.next = node;
                    return t;
                }
            }
        }
    }

acquireQueued  在队列中的节点通过此方法获取锁,对中断不敏感

/**
     * Acquires in exclusive uninterruptible mode for thread already in
     * queue. Used by condition wait methods as well as acquire.
     *
     * @param node the node
     * @param arg the acquire argument
     * @return {@code true} if interrupted while waiting
     */
    final boolean acquireQueued(final Node node, int arg) {
        boolean failed = true;
        try {
            boolean interrupted = false;
            for (;;) {
                final Node p = node.predecessor();
                /*
                 * 检测当前节点前驱是否head,这是试获取锁的资格。
                 * 如果是的话,则调用tryAcquire尝试获取锁,
                 * 成功,则将head置为当前节点。
                 */
                if (p == head && tryAcquire(arg)) {
                    setHead(node);
                    p.next = null; // help GC
                    failed = false;
                    return interrupted;
                }
                /*
                 * 如果未成功获取锁则根据前驱节点判断是否要阻塞。
                 * 如果阻塞过程中被中断,则置interrupted标志位为true
                 */
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    interrupted = true;
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

shouldParkAfterFailedAcquire

根据前驱节点中的waitStatus来判断是否需要阻塞当前线程。在前驱状态不为SIGNAL的情况下都会循环重试获取锁。

/**
     * Checks and updates status for a node that failed to acquire.
     * Returns true if thread should block. This is the main signal
     * control in all acquire loops.  Requires that pred == node.prev.
     *
     * @param pred node's predecessor holding status
     * @param node the node
     * @return {@code true} if thread should block
     */
    private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
        int ws = pred.waitStatus;
        if (ws == Node.SIGNAL)
            /*
             * This node has already set status asking a release
             * to signal it, so it can safely park.
             */
            return true;
        if (ws > 0) {
            // Node.CANCELLED 向前遍历,更新当前节点的前驱为往前第一个非取消节点
            /*
             * Predecessor was cancelled. Skip over predecessors and
             * indicate retry.
             */
            do {
                node.prev = pred = pred.prev;
            } while (pred.waitStatus > 0);
            pred.next = node;
        } else {
            /*
             * waitStatus must be 0 or PROPAGATE.  Indicate that we
             * need a signal, but don't park yet.  Caller will need to
             * retry to make sure it cannot acquire before parking.
             */
             /*
             * 等待状态为0或者PROPAGATE(-3),设置前驱的等待状态为SIGNAL,
             * 并且之后会回到循环再次重试获取锁。
             */
            compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
        }
        return false;
    }

 

cancelAcquire  该方法实现某个node取消获取锁

/**
     * Cancels an ongoing attempt to acquire.
     *
     * @param node the node
     */
    private void cancelAcquire(Node node) {
        // Ignore if node doesn't exist
        if (node == null)
            return;

        node.thread = null;

        // Skip cancelled predecessors
        Node pred = node.prev;
        while (pred.waitStatus > 0)
            node.prev = pred = pred.prev;

        // predNext is the apparent node to unsplice. CASes below will
        // fail if not, in which case, we lost race vs another cancel
        // or signal, so no further action is necessary.
        Node predNext = pred.next;

        // Can use unconditional write instead of CAS here.
        // After this atomic step, other Nodes can skip past us.
        // Before, we are free of interference from other threads.
        node.waitStatus = Node.CANCELLED;

        // If we are the tail, remove ourselves.
        /*
         * 如果CAS将tail从node置为pred节点了
         * 则剩下要做的事情就是尝试用CAS将pred节点的next更新为null以彻底切断pred和node的联系。
         * 这样一来就断开了pred与pred的所有后继节点,这些节点由于变得不可达,最终会被回收掉。
         * 由于node没有后继节点,所以这种情况到这里整个cancel就算是处理完毕了。
         * 这里的CAS更新pred的next即使失败了也没关系,说明有其它新入队线程或者其它取消线程更新        掉了。
         */
        if (node == tail && compareAndSetTail(node, pred)) {
            compareAndSetNext(pred, predNext, null);
        } else {
            // 如果node还有后继节点,这种情况要做的事情是把pred和后继非取消节点拼起来。
            // If successor needs signal, try to set pred's next-link
            // so it will get one. Otherwise wake it up to propagate.
            int ws;
            if (pred != head &&
                ((ws = pred.waitStatus) == Node.SIGNAL ||
                 (ws <= 0 && compareAndSetWaitStatus(pred, ws, Node.SIGNAL))) &&
                pred.thread != null) {
                Node next = node.next;
                if (next != null && next.waitStatus <= 0)
                    compareAndSetNext(pred, predNext, next);
            } else {
                /*
                 * 这时说明pred == head或者pred状态取消或者pred.thread == null
                 * 在这些情况下为了保证队列的活跃性,需要去唤醒一次后继线程。
                 * 举例来说pred == head完全有可能实际上目前已经没有线程持有锁了,
                 * 自然就不会有释放锁唤醒后继的动作。如果不唤醒后继,队列就挂掉了。
                 *
                 * 这种情况下看似由于没有更新pred的next的操作,队列中可能会留有一大把的取消节点。
                 * 实际上不要紧,因为后继线程唤醒之后会走一次试获取锁的过程,
                 * 失败的话会走到shouldParkAfterFailedAcquire的逻辑。
                 * 那里面的if中有处理前驱节点如果为取消则维护pred/next,踢掉这些取消节点的逻辑。
                 */
                unparkSuccessor(node);
            }

            /*
             * 取消节点的next之所以设置为自己本身而不是null,
             * 是为了方便AQS中Condition部分的isOnSyncQueue方法,
             * 判断一个原先属于条件队列的节点是否转移到了同步队列。
             *
             * 因为同步队列中会用到节点的next域,取消节点的next也有值的话,
             * 可以断言next域有值的节点一定在同步队列上。
             *
             * 在GC层面,和设置为null具有相同的效果。
             */
            node.next = node; // help GC
        }
    }

unparkSuccessor  唤醒后继线程。

/**
     * Wakes up node's successor, if one exists.
     *
     * @param node the node
     */
    private void unparkSuccessor(Node node) {
        /*
         * If status is negative (i.e., possibly needing signal) try
         * to clear in anticipation of signalling.  It is OK if this
         * fails or if status is changed by waiting thread.
         */
        int ws = node.waitStatus;
        if (ws < 0)
            compareAndSetWaitStatus(node, ws, 0);

        /*
         * Thread to unpark is held in successor, which is normally
         * just the next node.  But if cancelled or apparently null,
         * traverse backwards from tail to find the actual
         * non-cancelled successor.
         */
        Node s = node.next;
        if (s == null || s.waitStatus > 0) {
            s = null;
            for (Node t = tail; t != null && t != node; t = t.prev)
                if (t.waitStatus <= 0)
                    s = t;
        }
        if (s != null)
            LockSupport.unpark(s.thread);
    }

最后附上流程图

共享锁的 aquire 和 release请看下一篇《AQS-源码分析(二)》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值