* ConditionalLockBufferForCleanup - as above, but don't wait to get the lock * * We won't loop, but just check once to see if the pin count is OK. If * not, return false with no lock held. */
| 4520 | * not, return false with no lock held. |
| 4521 | */ |
| 4522 | bool |
| 4523 | ConditionalLockBufferForCleanup(Buffer buffer) |
| 4524 | { |
| 4525 | BufferDesc *bufHdr; |
| 4526 | uint32 buf_state, |
| 4527 | refcount; |
| 4528 | |
| 4529 | Assert(BufferIsValid(buffer)); |
| 4530 | |
| 4531 | if (BufferIsLocal(buffer)) |
| 4532 | { |
| 4533 | refcount = LocalRefCount[-buffer - 1]; |
| 4534 | /* There should be exactly one pin */ |
| 4535 | Assert(refcount > 0); |
| 4536 | if (refcount != 1) |
| 4537 | return false; |
| 4538 | /* Nobody else to wait for */ |
| 4539 | return true; |
| 4540 | } |
| 4541 | |
| 4542 | /* There should be exactly one local pin */ |
| 4543 | refcount = GetPrivateRefCount(buffer); |
| 4544 | Assert(refcount); |
| 4545 | if (refcount != 1) |
| 4546 | return false; |
| 4547 | |
| 4548 | /* Try to acquire lock */ |
| 4549 | if (!ConditionalLockBuffer(buffer)) |
| 4550 | return false; |
| 4551 | |
| 4552 | bufHdr = GetBufferDescriptor(buffer - 1); |
| 4553 | buf_state = LockBufHdr(bufHdr); |
| 4554 | refcount = BUF_STATE_GET_REFCOUNT(buf_state); |
| 4555 | |
| 4556 | Assert(refcount > 0); |
| 4557 | if (refcount == 1) |
| 4558 | { |
| 4559 | /* Successfully acquired exclusive lock with pincount 1 */ |
| 4560 | UnlockBufHdr(bufHdr, buf_state); |
| 4561 | return true; |
| 4562 | } |
| 4563 | |
| 4564 | /* Failed, so release the lock */ |
| 4565 | UnlockBufHdr(bufHdr, buf_state); |
| 4566 | LockBuffer(buffer, BUFFER_LOCK_UNLOCK); |
| 4567 | return false; |
| 4568 | } |
| 4569 | |
| 4570 | /* |
| 4571 | * IsBufferCleanupOK - as above, but we already have the lock |
no test coverage detected