| 608 | } |
| 609 | |
| 610 | Database::GlobalObjectHolder* Database::GlobalObjectHolder::init(const string& id, |
| 611 | const PathName& filename, |
| 612 | RefPtr<const Config> config) |
| 613 | { |
| 614 | MutexLockGuard guard(g_mutex, FB_FUNCTION); |
| 615 | |
| 616 | // Get entry with incrementing ref counter, so if someone is currently destroying it, the object itself |
| 617 | // will remain alive. |
| 618 | RefPtr<Database::GlobalObjectHolder::DbId> entry(g_hashTable->lookup(id)); |
| 619 | if (entry) |
| 620 | { |
| 621 | auto& shutdownMutex = entry->shutdownMutex; |
| 622 | // Check if someone else currently destroying GlobalObject. |
| 623 | if (shutdownMutex.tryEnter(FB_FUNCTION)) |
| 624 | { |
| 625 | // No one is destroying GlobalObject, continue init routine. |
| 626 | shutdownMutex.leave(); |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | // Someone is currently destroying GlobalObject, wait until he finish it to eliminate potential |
| 631 | // race conditions. |
| 632 | { |
| 633 | MutexUnlockGuard unlockGuard(g_mutex, FB_FUNCTION); |
| 634 | |
| 635 | MutexLockGuard guard(shutdownMutex, FB_FUNCTION); |
| 636 | } |
| 637 | // Now we are the one who owned DbId object. |
| 638 | // It also was removed from hash table, so simply delete it and recreate it next. |
| 639 | fb_assert(entry->holder == nullptr); |
| 640 | entry = nullptr; |
| 641 | } |
| 642 | } |
| 643 | if (!entry) |
| 644 | { |
| 645 | const auto holder = FB_NEW Database::GlobalObjectHolder(id, filename, config); |
| 646 | entry = makeRef(FB_NEW Database::GlobalObjectHolder::DbId(id, holder)); |
| 647 | g_hashTable->add(entry); |
| 648 | entry->addRef(); |
| 649 | } |
| 650 | |
| 651 | entry->holder->addRef(); |
| 652 | return entry->holder; |
| 653 | } |
| 654 | |
| 655 | Database::GlobalObjectHolder::~GlobalObjectHolder() |
| 656 | { |