@@ -80,20 +80,6 @@ SharedMemoryTimestampExporter g_shmem_exporter(SHMEM_NAME_CONFIG);
8080// !
8181std::atomic<bool > g_shm_initialized_successfully = false ;
8282
83- // Note this is to check that the data structures are the same between the std::atomic version of in64_t and the
84- // raw version, because some DC platforms do not have access to std::atomic on their side, and so will read these
85- // raw. Note that the chances of a torn read is very low, and on the write side, we are guaranteeing atomic writes
86- // by the use of std::atomic.
87- static_assert (sizeof (std::atomic<int64_t >) == sizeof (int64_t ),
88- " Size mismatch between atomic<int64_t> and int64_t" );
89- static_assert (alignof (std::atomic<int64_t >) == alignof (int64_t ),
90- " Alignment mismatch between atomic<int64_t> and int64_t" );
91- // Check the array specifically
92- static_assert (sizeof (std::atomic<int64_t >[2 ]) == sizeof (int64_t [2 ]),
93- " Size mismatch between atomic<int64_t>[2] and int64_t[2]" );
94- static_assert (alignof (std::atomic<int64_t >[2 ]) == alignof (int64_t [2 ]),
95- " Alignment mismatch between atomic<int64_t>[2] and int64_t[2]" );
96-
9783// Class Monitor
9884
9985Monitor::Monitor ()
@@ -976,6 +962,9 @@ bool SharedMemoryTimestampExporter::CreateOrOpen(mode_t mode) {
976962 debug_log (" INFO: %s: Initializing shared memory segment %s for atomic int64_t[2]" , __func__, m_shm_name.c_str ());
977963
978964 Cleanup (); // Ensure clean state
965+
966+ std::unique_lock<std::mutex> lock (mtx_shmem);
967+
979968 m_is_creator = false ;
980969
981970 // 1. Create or open RW
@@ -1028,12 +1017,11 @@ bool SharedMemoryTimestampExporter::CreateOrOpen(mode_t mode) {
10281017 }
10291018
10301019 // 5. Store pointer, initialize if we created/resized it
1031- m_mapped_ptr = static_cast <std::atomic< int64_t > *>(mapped_mem);
1020+ m_mapped_ptr = static_cast <int64_t *>(mapped_mem);
10321021 if (m_is_creator) {
1033- int64_t initial_time = GetUnixEpochTime (); // Assumes GetUnixEpochTime() is available
1034- // Initialize both elements atomically
1035- m_mapped_ptr[0 ].store (initial_time, std::memory_order_relaxed); // update_time
1036- m_mapped_ptr[1 ].store (initial_time, std::memory_order_relaxed); // last_active_time
1022+ int64_t initial_time = GetUnixEpochTime ();
1023+ m_mapped_ptr[0 ] = initial_time; // update_time
1024+ m_mapped_ptr[1 ] = initial_time; // last_active_time
10371025 debug_log (" INFO: %s: Shared memory segment %s initialized. update=%lld, last_active=%lld" ,
10381026 __func__, m_shm_name.c_str (), (long long )initial_time, (long long )initial_time);
10391027 } else {
@@ -1045,18 +1033,16 @@ bool SharedMemoryTimestampExporter::CreateOrOpen(mode_t mode) {
10451033}
10461034
10471035bool SharedMemoryTimestampExporter::UpdateTimestamps (int64_t update_time, int64_t last_active_time) {
1036+ std::unique_lock<std::mutex> lock (mtx_shmem);
1037+
10481038 if (!m_is_initialized.load () || m_mapped_ptr == nullptr ) {
1049- // Log periodically? Avoid flooding logs.
1050- // static std::atomic<int> s_log_counter = 0;
1051- // if (s_log_counter++ % 60 == 0) // Log once a minute maybe
1052- // error_log("ERROR: %s: Cannot update timestamp, shared memory not initialized.", __func__);
10531039 return false ;
10541040 }
10551041 // Use relaxed memory order: assumes readers don't need strict ordering
10561042 // relative to other non-atomic operations in this thread. Atomicity
10571043 // of the store itself is guaranteed.
1058- m_mapped_ptr[0 ]. store (update_time, std::memory_order_relaxed) ; // Store update_time at index 0
1059- m_mapped_ptr[1 ]. store (last_active_time, std::memory_order_relaxed) ; // Store last_active_time at index 1
1044+ m_mapped_ptr[0 ] = update_time ; // Store update_time at index 0
1045+ m_mapped_ptr[1 ] = last_active_time ; // Store last_active_time at index 1
10601046 return true ;
10611047}
10621048
@@ -1065,6 +1051,8 @@ bool SharedMemoryTimestampExporter::IsInitialized() const {
10651051}
10661052
10671053void SharedMemoryTimestampExporter::Cleanup () { // Renamed from Close
1054+ std::unique_lock<std::mutex> lock (mtx_shmem);
1055+
10681056 if (m_mapped_ptr != nullptr ) {
10691057 debug_log (" DEBUG: %s: Unmapping shared memory %s..." , __func__, m_shm_name.c_str ());
10701058 if (munmap (m_mapped_ptr, m_size) == -1 ) { // <-- Use correct size
@@ -1084,6 +1072,8 @@ void SharedMemoryTimestampExporter::Cleanup() { // Renamed from Close
10841072}
10851073
10861074bool SharedMemoryTimestampExporter::UnlinkSegment () {
1075+ std::unique_lock<std::mutex> lock (mtx_shmem);
1076+
10871077 debug_log (" INFO: %s: Requesting unlink for shared memory %s..." , __func__, m_shm_name.c_str ());
10881078 errno = 0 ;
10891079 if (shm_unlink (m_shm_name.c_str ()) == -1 ) {
0 commit comments