idle_detect  0.8.3.0
Provides overall idle detection for a linux computer
Public Member Functions | Private Member Functions | Private Attributes | List of all members
EventDetect::SharedMemoryTimestampExporter Class Reference

Manages a POSIX shared memory segment for exporting timestamps. Stores an array of two int64_t: {update_time, last_active_time}. Handles creation, mapping, updating, and cleanup via RAII. More...

#include <event_detect.h>

Public Member Functions

 SharedMemoryTimestampExporter (const std::string &name)
 Construct with the desired shared memory name. More...
 
 ~SharedMemoryTimestampExporter ()
 Destructor handles unmapping and potentially unlinking the shared memory.
 
 SharedMemoryTimestampExporter (const SharedMemoryTimestampExporter &)=delete
 
SharedMemoryTimestampExporteroperator= (const SharedMemoryTimestampExporter &)=delete
 
 SharedMemoryTimestampExporter (SharedMemoryTimestampExporter &&)=delete
 
SharedMemoryTimestampExporteroperator= (SharedMemoryTimestampExporter &&)=delete
 
bool CreateOrOpen (mode_t mode=0666)
 Creates (if necessary) and opens the shared memory segment, sets its size (to sizeof(int64_t[2])), and maps it for writing. Must be called before UpdateTimestamps or IsInitialized. More...
 
bool UpdateTimestamps (int64_t update_time, int64_t last_active_time)
 Updates both timestamps in the mapped shared memory. More...
 
bool IsInitialized () const
 Checks if the shared memory was successfully initialized (opened and mapped). More...
 
bool UnlinkSegment ()
 Explicitly unlinks the shared memory segment. Call during clean shutdown if desired. Idempotent. More...
 

Private Member Functions

void Cleanup ()
 Performs resource cleanup (munmap). Called by destructor.
 

Private Attributes

std::mutex mtx_shmem
 This protects against multiple threads in the event_detect process from simultaneously accessing and writing to the shared memory segment. It does NOT protect from another process encountering a torn read. I would prefer to use a pthread_mutex_t in the shmem data structure and manege the mutex across the writer and reader, but the BOINC architect does not believe this is necessary in practice given the small size and low frequency of writing and reading.
 
std::string m_shm_name
 
int m_shm_fd
 
int64_t * m_mapped_ptr
 
const size_t m_size
 
bool m_is_creator
 
std::atomic< bool > m_is_initialized
 

Detailed Description

Manages a POSIX shared memory segment for exporting timestamps. Stores an array of two int64_t: {update_time, last_active_time}. Handles creation, mapping, updating, and cleanup via RAII.

Constructor & Destructor Documentation

◆ SharedMemoryTimestampExporter()

SharedMemoryTimestampExporter::SharedMemoryTimestampExporter ( const std::string &  name)
explicit

Construct with the desired shared memory name.

Parameters
nameMust start with '/' (e.g., "/idle_detect_shmem").

Member Function Documentation

◆ CreateOrOpen()

bool SharedMemoryTimestampExporter::CreateOrOpen ( mode_t  mode = 0666)

Creates (if necessary) and opens the shared memory segment, sets its size (to sizeof(int64_t[2])), and maps it for writing. Must be called before UpdateTimestamps or IsInitialized.

Parameters
modePermissions (e.g., 0666 or 0660) to use if creating the segment.
Returns
True on success, false on any failure (shm_open, ftruncate, mmap).
Here is the call graph for this function:

◆ IsInitialized()

bool SharedMemoryTimestampExporter::IsInitialized ( ) const

Checks if the shared memory was successfully initialized (opened and mapped).

Returns
True if initialized and ready for updates, false otherwise.

◆ UnlinkSegment()

bool SharedMemoryTimestampExporter::UnlinkSegment ( )

Explicitly unlinks the shared memory segment. Call during clean shutdown if desired. Idempotent.

Returns
True if unlink succeeded or segment already gone, false on error.

◆ UpdateTimestamps()

bool SharedMemoryTimestampExporter::UpdateTimestamps ( int64_t  update_time,
int64_t  last_active_time 
)

Updates both timestamps in the mapped shared memory.

Parameters
update_timeThe timestamp of the current update cycle.
last_active_timeThe calculated overall last active time.
Returns
True if updated successfully, false if not initialized or pointer is invalid.
Here is the caller graph for this function:

The documentation for this class was generated from the following files: