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>
|
|
void | Cleanup () |
| | Performs resource cleanup (munmap). Called by destructor.
|
| |
|
|
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 |
| |
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.
◆ SharedMemoryTimestampExporter()
| SharedMemoryTimestampExporter::SharedMemoryTimestampExporter |
( |
const std::string & |
name | ) |
|
|
explicit |
Construct with the desired shared memory name.
- Parameters
-
| name | Must start with '/' (e.g., "/idle_detect_shmem"). |
◆ 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
-
| mode | Permissions (e.g., 0666 or 0660) to use if creating the segment. |
- Returns
- True on success, false on any failure (shm_open, ftruncate, mmap).
◆ 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_time | The timestamp of the current update cycle. |
| last_active_time | The calculated overall last active time. |
- Returns
- True if updated successfully, false if not initialized or pointer is invalid.
The documentation for this class was generated from the following files: