concurrent-5-AQS-Condition

等待

await

 public final void await() throws InterruptedException {
            if (Thread.interrupted())  //响应中断
                throw new InterruptedException();
            Node node = addConditionWaiter();   //加入到条件队列尾
            int savedState = fullyRelease(node);  //返回aqs状态
            int interruptMode = 0;
            while (!isOnSyncQueue(node)) {   //查看当前节点是否在同步队列中,等待signal后会入同步队列
                LockSupport.park(this);   //不在同步队列中需要睡眠
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)  //检查是否有被中断
                    break;
            }
            //尝试从队列中获取资源
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            if (node.nextWaiter != null) // clean up if cancelled
                unlinkCancelledWaiters();  //清除非等待的节点
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);  //判断中断类型后进行相应的中断处理
        }

addConditionWaiter

private Node addConditionWaiter() {
            Node t = lastWaiter;    //condition 队列尾
            // If lastWaiter is cancelled, clean out.
            if (t != null && t.waitStatus != Node.CONDITION) {
                unlinkCancelledWaiters();  //清除非条件等待的队列节点
                t = lastWaiter;
            }
            Node node = new Node(Thread.currentThread(), Node.CONDITION);   //创建当前线程为节点的条件队列
            if (t == null)
                firstWaiter = node;   //将当前节点标记为头
            else
                t.nextWaiter = node;   //加入到队列尾
            lastWaiter = node;    //标记为尾部节点
            return node;
        }

unlinkCancelledWaiters

 private void unlinkCancelledWaiters() {
            Node t = firstWaiter;  //获取头节点
            Node trail = null;    //临时节点
            while (t != null) {
                Node next = t.nextWaiter;   //获取下一节点
                if (t.waitStatus != Node.CONDITION) {   //如果当前节点不为条件等待
                    t.nextWaiter = null;    //清除t help gc
                    if (trail == null)  //如果临时节点为null
                        firstWaiter = next;  //则next节点为头节点
                    else
                        trail.nextWaiter = next;  //将当前节点过滤掉,
                    if (next == null)    //当next为null时,则表明到达了队列尾
                        lastWaiter = trail;
                }
                else
                    trail = t;  //将当前节点记录
                t = next;   //开始判断下一个节点
            }
        }

fullyRelease

 final int fullyRelease(Node node) {
        boolean failed = true;
        try {
            int savedState = getState();   //获取aqs状态
            if (release(savedState)) {  //尝试释放锁资源
                failed = false;
                return savedState;
            } else {
                throw new IllegalMonitorStateException();
            }
        } finally {
            if (failed)  //如果失败报错
                node.waitStatus = Node.CANCELLED;  //将节点标记为取消
        }
    }

isOnSyncQueue

final boolean isOnSyncQueue(Node node) {
        if (node.waitStatus == Node.CONDITION || node.prev == null)
            return false;
        if (node.next != null) // If has successor, it must be on queue
            return true;
        /*
         * node.prev can be non-null, but not yet on queue because
         * the CAS to place it on queue can fail. So we have to
         * traverse from tail to make sure it actually made it.  It
         * will always be near the tail in calls to this method, and
         * unless the CAS failed (which is unlikely), it will be
         * there, so we hardly ever traverse much.
         */
        return findNodeFromTail(node);  //从尾部遍历,如果不在队列中则返回false
    }

唤醒

signal

public final void signal() {
            if (!isHeldExclusively())  //如果当前线程不是持有锁的线程,则抛异常
                throw new IllegalMonitorStateException();
            Node first = firstWaiter;
            if (first != null)
                doSignal(first);  //将队列标记为signal 并入syc队列
        }

doSignal

private void doSignal(Node first) {
            do {
                if ( (firstWaiter = first.nextWaiter) == null)  //判断条件队列节点
                    lastWaiter = null;
                first.nextWaiter = null;
            } while (!transferForSignal(first) &&   //将条件队列入同步同列
                     (first = firstWaiter) != null);
        }

transferForSignal

final boolean transferForSignal(Node node) {
        /*
         * If cannot change waitStatus, the node has been cancelled.
         */
        if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
            return false;

        /*
         * Splice onto queue and try to set waitStatus of predecessor to
         * indicate that thread is (probably) waiting. If cancelled or
         * attempt to set waitStatus fails, wake up to resync (in which
         * case the waitStatus can be transiently and harmlessly wrong).
         */
        Node p = enq(node);  //入同步队列
        int ws = p.waitStatus;
        if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
            LockSupport.unpark(node.thread);
        return true;
    }

doSignalAll

  private void doSignalAll(Node first) {
            lastWaiter = firstWaiter = null;
            do {
                Node next = first.nextWaiter;
                first.nextWaiter = null;
                transferForSignal(first);  //一个个唤醒
                first = next;
            } while (first != null);
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In Verilog, a non-net refers to a variable that is not a wire or a register, but rather a constant or a parameter. Concurrent assignment refers to the assignment of a value to a variable using the "=" operator in a module outside of any procedural blocks (such as always or initial blocks). A concurrent assignment to a non-net is not allowed in Verilog. This is because a non-net does not have a storage element and cannot hold a value assigned to it. Instead, it is typically used as a constant or a parameter that is available for use in the module. To assign a value to a non-net, it should be done within a procedural block using the appropriate assignment operator (such as "<=" for registers or "assign" for wires). Alternatively, the value can be passed as an argument to the module using the parameter keyword. For example: module my_module #(parameter WIDTH = 8) ( input clk, input [WIDTH-1:0] data_in, output [WIDTH-1:0] data_out ); // This is a non-net parameter parameter ADD_VALUE = 5; // This is a register that can be assigned using the "=" operator within an always block reg [WIDTH-1:0] register_data; always @(posedge clk) begin register_data <= data_in + ADD_VALUE; end // This is a wire that can be assigned using the "assign" keyword assign data_out = register_data; endmodule In this example, ADD_VALUE is a non-net parameter that is used in the always block to add a constant value to the input data. The register_data variable is assigned using the "=" operator within the always block. The data_out wire is assigned using the "assign" keyword.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值