Dev/shm free obsolete (#1274)
Some checks failed
Build on RHEL9 / build (push) Failing after 3m24s
Build on RHEL8 / build (push) Failing after 5m7s

* 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:
2025-08-21 14:32:15 +02:00
committed by GitHub
parent 72056ff813
commit 15cbaa509e
7 changed files with 139 additions and 33 deletions

View File

@@ -3,7 +3,16 @@
#include <vector>
namespace sls {
#define CTB_SHMAPIVERSION 0x250820
#define CTB_SHMVERSION 0x250820
class CtbConfig {
public:
/** fixed pattern */
int shmversion{CTB_SHMVERSION};
bool isValid{true}; // false if freed to block access from python or c++ api
/** end of fixed pattern */
private:
static constexpr size_t name_length = 20;
static constexpr size_t num_dacs = 18;
static constexpr size_t num_adcs = 32;
@@ -56,7 +65,6 @@ class CtbConfig {
std::string getSlowADCName(size_t index) const;
std::vector<std::string> getSlowADCNames() const;
static const char *shm_tag();
bool isValid{true}; // false if freed to block access from python or c++ api
};
} // namespace sls

View File

@@ -30,7 +30,6 @@ void freeSharedMemory(const int detectorIndex, const int moduleIndex) {
SharedMemory<sharedModule> moduleShm(detectorIndex, moduleIndex);
if (moduleShm.exists()) {
moduleShm.openSharedMemory(false);
moduleShm()->isValid = false;
moduleShm.removeSharedMemory();
}
return;
@@ -43,7 +42,6 @@ void freeSharedMemory(const int detectorIndex, const int moduleIndex) {
if (detectorShm.exists()) {
detectorShm.openSharedMemory(false);
numDetectors = detectorShm()->totalNumberOfModules;
detectorShm()->isValid = false;
detectorShm.removeSharedMemory();
}
@@ -51,16 +49,14 @@ void freeSharedMemory(const int detectorIndex, const int moduleIndex) {
SharedMemory<sharedModule> moduleShm(detectorIndex, i);
if (moduleShm.exists()) {
moduleShm.openSharedMemory(false);
moduleShm()->isValid = false;
moduleShm.removeSharedMemory();
}
moduleShm.removeSharedMemory();
}
// Ctb configuration
SharedMemory<CtbConfig> ctbShm(detectorIndex, -1, CtbConfig::shm_tag());
if (ctbShm.exists()) {
ctbShm.openSharedMemory(false);
ctbShm()->isValid = false;
ctbShm.removeSharedMemory();
}
}

View File

@@ -24,7 +24,7 @@ class detectorData;
class Module;
#define DETECTOR_SHMAPIVERSION 0x190809
#define DETECTOR_SHMVERSION 0x250729
#define DETECTOR_SHMVERSION 0x250820
#define SHORT_STRING_LENGTH 50
/**
@@ -51,17 +51,17 @@ struct sharedDetector {
int totalNumberOfModules;
slsDetectorDefs::detectorType detType;
bool isValid{true}; // false if freed to block access from python or c++ api
/** END OF FIXED PATTERN
* -----------------------------------------------*/
/** Number of modules operated at once */
slsDetectorDefs::xy numberOfModules;
/** max number of channels for complete detector*/
slsDetectorDefs::xy numberOfChannels;
bool isValid{true}; // false if freed to block access from python or c++ api
/** END OF FIXED PATTERN
* -----------------------------------------------*/
bool acquiringFlag;
bool initialChecks;
bool gapPixels;

View File

@@ -19,7 +19,7 @@ namespace sls {
class ServerInterface;
#define MODULE_SHMAPIVERSION 0x190726
#define MODULE_SHMVERSION 0x250729
#define MODULE_SHMVERSION 0x250820
/**
* @short structure allocated in shared memory to store Module settings for

View File

@@ -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