mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-02-14 08:28:42 +01:00
Dev/shm free obsolete (#1274)
* freeing obsolete shm withoua a 'isValid' should access raw pointers. Need to move this all into the shm class * fixed obsolete shm free issue * minor * ensuring the test works platform independent for size of int
This commit is contained in:
@@ -27,17 +27,25 @@
|
||||
|
||||
namespace sls {
|
||||
|
||||
struct sharedDetector;
|
||||
struct CtbConfig;
|
||||
// struct sharedDetector;
|
||||
|
||||
#define SHM_DETECTOR_PREFIX "/slsDetectorPackage_detector_"
|
||||
#define SHM_MODULE_PREFIX "_module_"
|
||||
#define SHM_ENV_NAME "SLSDETNAME"
|
||||
#define SHM_IS_VALID_CHECK_VERSION 0x250820
|
||||
#define SHM_DETECTOR_PREFIX "/slsDetectorPackage_detector_"
|
||||
#define SHM_MODULE_PREFIX "_module_"
|
||||
#define SHM_ENV_NAME "SLSDETNAME"
|
||||
|
||||
template <typename T, typename U> constexpr bool is_type() {
|
||||
return std::is_same_v<std::decay_t<U>, T>;
|
||||
}
|
||||
|
||||
template <typename T> class SharedMemory {
|
||||
|
||||
#ifndef DISABLE_STATIC_ASSERT
|
||||
static_assert(has_bool_isValid<T>::value,
|
||||
"SharedMemory requires the struct to have a bool member "
|
||||
"named 'isValid'");
|
||||
#endif
|
||||
|
||||
static constexpr int NAME_MAX_LENGTH = 255;
|
||||
std::string name;
|
||||
@@ -72,21 +80,49 @@ template <typename T> class SharedMemory {
|
||||
unmapSharedMemory();
|
||||
}
|
||||
|
||||
bool memoryHasValidFlag() const {
|
||||
if constexpr (is_type<CtbConfig, T>()) {
|
||||
// CtbConfig did not have shmversion before, so exact value check
|
||||
if (shared_struct->shmversion == SHM_IS_VALID_CHECK_VERSION) {
|
||||
return true;
|
||||
}
|
||||
} else if (shared_struct->shmversion >= SHM_IS_VALID_CHECK_VERSION) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool checkValidity() const {
|
||||
if (memoryHasValidFlag() && !shared_struct->isValid)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void invalidate() {
|
||||
// also called by obsolete shm test structure (so check added)
|
||||
if constexpr (has_bool_isValid<T>::value) {
|
||||
if (memoryHasValidFlag())
|
||||
shared_struct->isValid = false;
|
||||
}
|
||||
}
|
||||
|
||||
T *operator()() {
|
||||
if (!shared_struct)
|
||||
throw SharedMemoryError(getNoShmAccessMessage());
|
||||
if (!shared_struct->isValid) {
|
||||
|
||||
if (!checkValidity())
|
||||
throw SharedMemoryError(getInvalidShmMessage());
|
||||
}
|
||||
|
||||
return shared_struct;
|
||||
}
|
||||
|
||||
const T *operator()() const {
|
||||
if (!shared_struct)
|
||||
throw SharedMemoryError(getNoShmAccessMessage());
|
||||
if (!shared_struct->isValid) {
|
||||
|
||||
if (!checkValidity())
|
||||
throw SharedMemoryError(getInvalidShmMessage());
|
||||
}
|
||||
|
||||
return shared_struct;
|
||||
}
|
||||
|
||||
@@ -146,6 +182,7 @@ template <typename T> class SharedMemory {
|
||||
}
|
||||
|
||||
void removeSharedMemory() {
|
||||
invalidate();
|
||||
unmapSharedMemory();
|
||||
if (shm_unlink(name.c_str()) < 0) {
|
||||
// silent exit if shm did not exist anyway
|
||||
|
||||
Reference in New Issue
Block a user