Semaphorer源码
2022-11-12 11:17:52 0 举报
Semaphorer
作者其他创作
大纲/内容
}
state小于0或者cas设置成功
设置当前节点为头节点
overflow
释放一个许可
if (propagate > 0 || h == null || h.waitStatus < 0 ||(h = head) == null || h.waitStatus < 0)
if (s == null || s.isShared())
if (next < current)
throw new Error(\"Maximum permit count exceeded\");
if (tryAcquireShared(arg) < 0)
for (;;) {
共享锁传播,唤醒后续的共享节点,继续去尝试获取锁
true
如果当前节点的下一个节点也是共享节点,继续唤醒后续节点
if (p == head)
获取锁失败
队列头节点设置为singal阻塞当前线程
unparkSuccessor(h)
尝试获取锁,release释放的
获取锁成功
唤醒阻塞线程
int remaining = available - acquires;
cas设置state
int available = getState();
doAcquireSharedInterruptibly(arg)
final Node p = node.predecessor();
semaphore.acquire(1)
doReleaseShared()
doReleaseShared()
if (tryReleaseShared(arg))
setHead(node);
false,同步队列已经有节点在等待
addWaiter(Node.SHARED);
减去需求的许可数
int next = current + releases;
if (r >= 0)
semaphore.release(2)
int current = getState();
nonfairTryAcquireShared(acquires)
获取共享锁
int r = tryAcquireShared(arg);
获取state
cas失败
0 条评论
回复 删除
下一页