moving SharedMemory into namespace sls

This commit is contained in:
Erik Frojdh 2019-03-14 16:51:38 +01:00
parent 40d2f66146
commit bad44f5bf4
6 changed files with 20 additions and 11 deletions

View File

@ -26,6 +26,9 @@
#include <future> #include <future>
#include <vector> #include <vector>
using sls::SharedMemory;
using sls::SharedMemoryError;
multiSlsDetector::multiSlsDetector(int id, bool verify, bool update) multiSlsDetector::multiSlsDetector(int id, bool verify, bool update)
: detId(id) { : detId(id) {
setupMultiDetector(verify, update); setupMultiDetector(verify, update);

View File

@ -1959,7 +1959,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
int detId; int detId;
/** Shared Memory object */ /** Shared Memory object */
SharedMemory<sharedMultiSlsDetector> multi_shm{0, -1}; sls::SharedMemory<sharedMultiSlsDetector> multi_shm{0, -1};
/** pointers to the slsDetector structures */ /** pointers to the slsDetector structures */
std::vector<std::unique_ptr<slsDetector>> detectors; std::vector<std::unique_ptr<slsDetector>> detectors;

View File

@ -30,10 +30,11 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
using sls::SharedMemoryError; namespace sls {
template <typename T> template <typename T>
class SharedMemory { class SharedMemory {
public: public:
/** /**
* Constructor * Constructor
@ -166,7 +167,7 @@ class SharedMemory {
void UnmapSharedMemory() { void UnmapSharedMemory() {
if (shared_struct != nullptr) { if (shared_struct != nullptr) {
if (munmap(shared_struct, shmSize) < 0) { if (munmap(shared_struct, shmSize) < 0) {
std::string msg = "Unmapping shared memory " + name +" failed: " + strerror(errno); std::string msg = "Unmapping shared memory " + name + " failed: " + strerror(errno);
FILE_LOG(logERROR) << msg; FILE_LOG(logERROR) << msg;
close(fd); close(fd);
throw SharedMemoryError(msg); throw SharedMemoryError(msg);
@ -236,8 +237,7 @@ class SharedMemory {
std::string temp = ss.str(); std::string temp = ss.str();
if (temp.length() > NAME_MAX) { if (temp.length() > NAME_MAX) {
std::string msg = "Shared memory initialization failed. " + temp + " has " + std::to_string(temp.length()) + " characters. \n" std::string msg = "Shared memory initialization failed. " + temp + " has " + std::to_string(temp.length()) + " characters. \n" + "Maximum is " + std::to_string(NAME_MAX) + ". Change the environment variable " + SHM_ENV_NAME;
+ "Maximum is " + std::to_string(NAME_MAX) + ". Change the environment variable " + SHM_ENV_NAME;
FILE_LOG(logERROR) << msg; FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg); throw SharedMemoryError(msg);
} }
@ -272,8 +272,7 @@ class SharedMemory {
struct stat sb; struct stat sb;
// could not fstat // could not fstat
if (fstat(fd, &sb) < 0) { if (fstat(fd, &sb) < 0) {
std::string msg = "Could not verify existing shared memory " + name + " size match " std::string msg = "Could not verify existing shared memory " + name + " size match " + "(could not fstat): " + strerror(errno);
+ "(could not fstat): " + strerror(errno);
FILE_LOG(logERROR) << msg; FILE_LOG(logERROR) << msg;
close(fd); close(fd);
throw SharedMemoryError(msg); throw SharedMemoryError(msg);
@ -282,8 +281,7 @@ class SharedMemory {
//size does not match //size does not match
long unsigned int sz = (long unsigned int)sb.st_size; long unsigned int sz = (long unsigned int)sb.st_size;
if (sz != expectedSize) { if (sz != expectedSize) {
std::string msg = "Existing shared memory " + name + " size does not match" std::string msg = "Existing shared memory " + name + " size does not match" + "Expected " + std::to_string(expectedSize) + ", found " + std::to_string(sz);
+ "Expected " + std::to_string(expectedSize) + ", found " + std::to_string(sz);
FILE_LOG(logERROR) << msg; FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg); throw SharedMemoryError(msg);
return 1; return 1;
@ -302,3 +300,5 @@ class SharedMemory {
T *shared_struct{nullptr}; T *shared_struct{nullptr};
}; };
} // namespace sls

View File

@ -24,6 +24,10 @@
#include <sys/types.h> #include <sys/types.h>
#include <cassert> #include <cassert>
using sls::SharedMemory;
using sls::SharedMemoryError;
using sls::RuntimeError;
#define DEFAULT_HOSTNAME "localhost" #define DEFAULT_HOSTNAME "localhost"
slsDetector::slsDetector(detectorType type, int multiId, int id, bool verify) slsDetector::slsDetector(detectorType type, int multiId, int id, bool verify)
@ -290,7 +294,7 @@ void slsDetector::setDetectorSpecificParameters(detectorType type, detParameterL
break; break;
default: default:
FILE_LOG(logERROR) << "Unknown detector type! " << type; FILE_LOG(logERROR) << "Unknown detector type! " << type;
throw std::exception(); throw RuntimeError("Unknown detector type");
} }
} }

View File

@ -1736,7 +1736,7 @@ class slsDetector : public virtual slsDetectorDefs, public virtual errorDefs {
int detId; int detId;
/** Shared Memory object */ /** Shared Memory object */
SharedMemory<sharedSlsDetector> detector_shm{0,0}; sls::SharedMemory<sharedSlsDetector> detector_shm{0,0};
}; };

View File

@ -11,6 +11,8 @@ struct Data {
char mess[50]; char mess[50];
}; };
using namespace sls;
TEST_CASE("Create SharedMemory read and write") { TEST_CASE("Create SharedMemory read and write") {
SharedMemory<Data> shm(0, -1); SharedMemory<Data> shm(0, -1);