-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
8
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
When I read the StampedLock source code, There is a code at the end of the cancelWaiter method
if (h == whead) {
if (q != null && h.status == 0 &&
((s = state) & ABITS) != WBIT &&
(s == 0L || q.mode == RMODE))
release(h);
break;
}
I wonder when this happens?
I think there might be a problem here, mainly with this condition 'h.status == 0', because 'q != null', so, there are only 2 cases that satisfy this condition 'h.status == 0'.
The first is when a node is queued for initialization. Before the status is set, the status is 0, and it is in read lock state, the first three conditions can be met, but the fourth condition cannot.
The second case is when the thread releases the lock. When the thread that acquired the lock executes the release method, status will be set to 0, so the release here is repeated, it's redundant.
Both cases are meaningless.
I think the author wrote this code for this scenario. There is a node in read mode, and there are some other nodes in front of it. Originally, it cannot get the lock. At a certain point, all the nodes in front of it are interrupted and cancelled, at this point, 'Stamplock' is in read lock state, so it can get the lock at this time, and then it will call release method to wake it up.
Therefore, I think the condition 'h.status == 0' should be deleted to satisfy the scenario I mentioned.
When I read the StampedLock source code, There is a code at the end of the cancelWaiter method
if (h == whead) {
if (q != null && h.status == 0 &&
((s = state) & ABITS) != WBIT &&
(s == 0L || q.mode == RMODE))
release(h);
break;
}
I wonder when this happens?
I think there might be a problem here, mainly with this condition 'h.status == 0', because 'q != null', so, there are only 2 cases that satisfy this condition 'h.status == 0'.
The first is when a node is queued for initialization. Before the status is set, the status is 0, and it is in read lock state, the first three conditions can be met, but the fourth condition cannot.
The second case is when the thread releases the lock. When the thread that acquired the lock executes the release method, status will be set to 0, so the release here is repeated, it's redundant.
Both cases are meaningless.
I think the author wrote this code for this scenario. There is a node in read mode, and there are some other nodes in front of it. Originally, it cannot get the lock. At a certain point, all the nodes in front of it are interrupted and cancelled, at this point, 'Stamplock' is in read lock state, so it can get the lock at this time, and then it will call release method to wake it up.
Therefore, I think the condition 'h.status == 0' should be deleted to satisfy the scenario I mentioned.