@@ -612,14 +612,17 @@ class MappingIpc FB_FINAL : public Firebird::IpcObject
612
612
613
613
void shutdown ()
614
614
{
615
+ if (!sharedMemory)
616
+ return ;
617
+ MutexLockGuard gLocal (initMutex, FB_FUNCTION);
615
618
if (!sharedMemory)
616
619
return ;
617
620
618
621
{
619
622
Guard gShared (this );
620
- MappingHeader* sMem = sharedMemory->getHeader ();
621
623
622
- startupSemaphore.tryEnter (5 );
624
+ MappingHeader* sMem = sharedMemory->getHeader ();
625
+ fb_assert (sMem ->process [process].id );
623
626
sMem ->process [process].flags &= ~MappingHeader::FLAG_ACTIVE;
624
627
(void ) // Ignore errors in cleanup
625
628
sharedMemory->eventPost (&sMem ->process [process].notifyEvent );
@@ -723,32 +726,37 @@ class MappingIpc FB_FINAL : public Firebird::IpcObject
723
726
if (sharedMemory)
724
727
return ;
725
728
726
- Arg::StatusVector statusVector ;
729
+ AutoSharedMemory tempSharedMemory ;
727
730
try
728
731
{
729
- sharedMemory .reset (FB_NEW_POOL (*getDefaultMemoryPool ())
732
+ tempSharedMemory .reset (FB_NEW_POOL (*getDefaultMemoryPool ())
730
733
SharedMemory<MappingHeader>(" fb_user_mapping" , DEFAULT_SIZE, this ));
731
734
}
732
735
catch (const Exception& ex)
733
736
{
734
- iscLogException (" MappingIpc: Cannot initialize the shared memory region" , ex);
737
+ StaticStatusVector s;
738
+ ex.stuffException (s);
739
+ if (!fb_utils::containsErrorCode (s.begin (), isc_instance_conflict))
740
+ iscLogException (" MappingIpc: Cannot initialize the shared memory region" , ex);
735
741
throw ;
736
742
}
737
- fb_assert (sharedMemory ->getHeader ()->mhb_header_version == MemoryHeader::HEADER_VERSION);
738
- fb_assert (sharedMemory ->getHeader ()->mhb_version == MAPPING_VERSION);
743
+ fb_assert (tempSharedMemory ->getHeader ()->mhb_header_version == MemoryHeader::HEADER_VERSION);
744
+ fb_assert (tempSharedMemory ->getHeader ()->mhb_version == MAPPING_VERSION);
739
745
740
- Guard gShared (this );
746
+ Guard gShared (tempSharedMemory );
741
747
742
- MappingHeader* sMem = sharedMemory ->getHeader ();
748
+ MappingHeader* sMem = tempSharedMemory ->getHeader ();
743
749
744
750
for (process = 0 ; process < sMem ->processes ; ++process)
745
751
{
746
752
if (!(sMem ->process [process].flags & MappingHeader::FLAG_ACTIVE))
747
753
break ;
754
+
748
755
if (!ISC_check_process_existence (sMem ->process [process].id ))
749
756
{
750
- sharedMemory->eventFini (&sMem ->process [process].notifyEvent );
751
- sharedMemory->eventFini (&sMem ->process [process].callbackEvent );
757
+ tempSharedMemory->eventFini (&sMem ->process [process].notifyEvent );
758
+ tempSharedMemory->eventFini (&sMem ->process [process].callbackEvent );
759
+
752
760
break ;
753
761
}
754
762
}
@@ -765,6 +773,8 @@ class MappingIpc FB_FINAL : public Firebird::IpcObject
765
773
766
774
sMem ->process [process].id = processId;
767
775
sMem ->process [process].flags = MappingHeader::FLAG_ACTIVE;
776
+ sharedMemory.reset (tempSharedMemory.release ());
777
+
768
778
if (sharedMemory->eventInit (&sMem ->process [process].notifyEvent ) != FB_SUCCESS)
769
779
{
770
780
(Arg::Gds (isc_random) << " Error initializing notifyEvent in mapping shared memory" ).raise ();
@@ -783,6 +793,7 @@ class MappingIpc FB_FINAL : public Firebird::IpcObject
783
793
sMem ->process [process].flags &= ~MappingHeader::FLAG_ACTIVE;
784
794
throw ;
785
795
}
796
+ startupSemaphore.enter ();
786
797
}
787
798
788
799
void exceptionHandler (const Exception& ex, ThreadFinishSync<MappingIpc*>::ThreadRoutine*)
@@ -864,34 +875,44 @@ class MappingIpc FB_FINAL : public Firebird::IpcObject
864
875
865
876
class Guard ;
866
877
friend class Guard ;
878
+ typedef SharedMemory<MappingHeader> MappingSharedMemory;
879
+ typedef AutoPtr<MappingSharedMemory> AutoSharedMemory;
867
880
868
881
class Guard
869
882
{
870
883
public:
871
884
explicit Guard (MappingIpc* ptr)
885
+ : data(ptr->sharedMemory)
886
+ {
887
+ fb_assert (data);
888
+ data->mutexLock ();
889
+ }
890
+
891
+ explicit Guard (MappingSharedMemory* ptr)
872
892
: data(ptr)
873
893
{
874
- data->sharedMemory ->mutexLock ();
894
+ fb_assert (data);
895
+ data->mutexLock ();
875
896
}
876
897
877
898
~Guard ()
878
899
{
879
- data->sharedMemory -> mutexUnlock ();
900
+ data->mutexUnlock ();
880
901
}
881
902
882
903
private:
883
904
Guard (const Guard&);
884
905
Guard& operator =(const Guard&);
885
906
886
- MappingIpc * const data;
907
+ MappingSharedMemory * const data;
887
908
};
888
909
889
910
static void clearDelivery (MappingIpc* mapping)
890
911
{
891
912
mapping->clearDeliveryThread ();
892
913
}
893
914
894
- AutoPtr<SharedMemory<MappingHeader> > sharedMemory;
915
+ AutoSharedMemory sharedMemory;
895
916
Mutex initMutex;
896
917
const SLONG processId;
897
918
unsigned process;
0 commit comments