| 531 | |
| 532 | |
| 533 | bool CCH_exclusive_attachment(thread_db* tdbb, USHORT level, SSHORT wait_flag, Sync* exGuard) |
| 534 | { |
| 535 | /************************************** |
| 536 | * |
| 537 | * C C H _ e x c l u s i v e _ a t t a c h m e n t |
| 538 | * |
| 539 | ************************************** |
| 540 | * |
| 541 | * Functional description |
| 542 | * Get exclusive access to a database. If we get it, return true. |
| 543 | * If the wait flag is FALSE, and we can't get it, give up and |
| 544 | * return false. |
| 545 | * |
| 546 | **************************************/ |
| 547 | const int CCH_EXCLUSIVE_RETRY_INTERVAL = 10; // retry interval in millseconds |
| 548 | |
| 549 | SET_TDBB(tdbb); |
| 550 | Database* const dbb = tdbb->getDatabase(); |
| 551 | |
| 552 | Sync dsGuard(&dbb->dbb_sync, FB_FUNCTION); |
| 553 | |
| 554 | const bool exLock = dbb->dbb_sync.ourExclusiveLock(); |
| 555 | if (!exLock) |
| 556 | dsGuard.lock(level != LCK_none ? SYNC_EXCLUSIVE : SYNC_SHARED); |
| 557 | else |
| 558 | fb_assert(exGuard); |
| 559 | |
| 560 | Jrd::Attachment* const attachment = tdbb->getAttachment(); |
| 561 | |
| 562 | if (attachment->att_flags & ATT_exclusive) |
| 563 | return true; |
| 564 | |
| 565 | attachment->att_flags |= (level == LCK_none) ? ATT_attach_pending : ATT_exclusive_pending; |
| 566 | |
| 567 | const SLONG timeout = (wait_flag == LCK_WAIT) ? 1L << 30 : (-wait_flag * 1000 / CCH_EXCLUSIVE_RETRY_INTERVAL); |
| 568 | |
| 569 | // If requesting exclusive database access, then re-position attachment as the |
| 570 | // youngest so that pending attachments may pass. |
| 571 | |
| 572 | if (level != LCK_none) |
| 573 | { |
| 574 | for (Jrd::Attachment** ptr = &dbb->dbb_attachments; *ptr; ptr = &(*ptr)->att_next) |
| 575 | { |
| 576 | if (*ptr == attachment) |
| 577 | { |
| 578 | *ptr = attachment->att_next; |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | attachment->att_next = dbb->dbb_attachments; |
| 583 | dbb->dbb_attachments = attachment; |
| 584 | |
| 585 | if (!exLock) |
| 586 | dsGuard.downgrade(SYNC_SHARED); |
| 587 | } |
| 588 | |
| 589 | for (SLONG remaining = timeout; remaining >= 0; remaining -= CCH_EXCLUSIVE_RETRY_INTERVAL) |
| 590 | { |
no test coverage detected