Shared memory fixes.

This commit is contained in:
Thulinma 2014-06-16 14:41:00 +02:00
parent 0cf74425f9
commit 19e79c1315

View file

@ -177,26 +177,30 @@ namespace IPC {
///\brief Unmaps a shared page if allowed ///\brief Unmaps a shared page if allowed
void sharedPage::unmap() { void sharedPage::unmap() {
if (mapped && len) { if (mapped && len) {
#ifdef __CYGWIN__ #ifdef __CYGWIN__
UnmapViewOfFile(mapped); UnmapViewOfFile(mapped);
#else #else
munmap(mapped, len); munmap(mapped, len);
#endif #endif
mapped = 0;
len = 0;
} }
mapped = 0;
len = 0;
} }
///\brief Closes a shared page if allowed ///\brief Closes a shared page if allowed
void sharedPage::close() { void sharedPage::close() {
unmap();
if (handle > 0) { if (handle > 0) {
#ifdef __CYGWIN__ #ifdef __CYGWIN__
CloseHandle(handle); CloseHandle(handle);
#else #else
::close(handle); ::close(handle);
#endif if (master && name != "") {
shm_unlink(name.c_str());
}
#endif
handle = 0;
} }
handle = 0;
} }
@ -205,12 +209,7 @@ namespace IPC {
///\param len_ The size to make the page ///\param len_ The size to make the page
///\param master_ Whether to create or merely open the page ///\param master_ Whether to create or merely open the page
///\param autoBackoff When only opening the page, wait for it to appear or fail ///\param autoBackoff When only opening the page, wait for it to appear or fail
sharedPage::sharedPage(std::string name_, unsigned int len_, bool master_, bool autoBackoff) : handle(0), name(name_), len(len_), master(master_), mapped(NULL) { sharedPage::sharedPage(std::string name_, unsigned int len_, bool master_, bool autoBackoff) : handle(0), len(0), master(false), mapped(0) {
handle = 0;
name = name_;
len = len_;
master = master_;
mapped = 0;
init(name_, len_, master_, autoBackoff); init(name_, len_, master_, autoBackoff);
} }
@ -218,7 +217,6 @@ namespace IPC {
///\param rhs The page to copy ///\param rhs The page to copy
sharedPage::sharedPage(const sharedPage & rhs) { sharedPage::sharedPage(const sharedPage & rhs) {
handle = 0; handle = 0;
name = "";
len = 0; len = 0;
master = false; master = false;
mapped = 0; mapped = 0;
@ -237,20 +235,19 @@ namespace IPC {
} }
#ifdef __CYGWIN__ ///\brief Initialize a page, de-initialize before if needed
///\brief Initialize a page, de-initialize before if needed ///\param name_ The name of the page to be created
///\param name_ The name of the page to be created ///\param len_ The size to make the page
///\param len_ The size to make the page ///\param master_ Whether to create or merely open the page
///\param master_ Whether to create or merely open the page ///\param autoBackoff When only opening the page, wait for it to appear or fail
///\param autoBackoff When only opening the page, wait for it to appear or fail void sharedPage::init(std::string name_, unsigned int len_, bool master_, bool autoBackoff) {
void sharedPage::init(std::string name_, unsigned int len_, bool master_, bool autoBackoff) { close();
unmap(); name = name_;
close(); len = len_;
name = name_; master = master_;
len = len_; mapped = 0;
master = master_; if (name.size()) {
mapped = 0; #ifdef __CYGWIN__
if (name.size()) {
if (master){ if (master){
handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, len, name.c_str()); handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, len, name.c_str());
}else{ }else{
@ -271,25 +268,7 @@ namespace IPC {
if (!mapped) { if (!mapped) {
return; return;
} }
} #else
}
#else
///\brief Initialize a page, de-initialize before if needed
///\param name_ The name of the page to be created
///\param len_ The size to make the page
///\param master_ Whether to create or merely open the page
///\param autoBackoff When only opening the page, wait for it to appear or fail
void sharedPage::init(std::string name_, unsigned int len_, bool master_, bool autoBackoff) {
unmap();
if (master) {
shm_unlink(name.c_str());
}
close();
name = name_;
len = len_;
master = master_;
mapped = 0;
if (name.size()) {
handle = shm_open(name.c_str(), (master ? O_CREAT | O_EXCL : 0) | O_RDWR, ACCESSPERMS); handle = shm_open(name.c_str(), (master ? O_CREAT | O_EXCL : 0) | O_RDWR, ACCESSPERMS);
if (handle == -1) { if (handle == -1) {
if (master) { if (master) {
@ -330,18 +309,12 @@ namespace IPC {
mapped = 0; mapped = 0;
return; return;
} }
} #endif
} }
#endif }
///\brief Default destructor ///\brief Default destructor
sharedPage::~sharedPage() { sharedPage::~sharedPage() {
unmap();
if (master) {
#ifndef __CYGWIN__
shm_unlink(name.c_str());
#endif
}
close(); close();
} }
@ -354,9 +327,8 @@ namespace IPC {
///\param autoBackoff When only opening the page, wait for it to appear or fail ///\param autoBackoff When only opening the page, wait for it to appear or fail
sharedPage::sharedPage(std::string name_, unsigned int len_, bool master_, bool autoBackoff) { sharedPage::sharedPage(std::string name_, unsigned int len_, bool master_, bool autoBackoff) {
handle = 0; handle = 0;
name = name_; len = 0;
len = len_; master = false;
master = master_;
mapped = 0; mapped = 0;
init(name_, len_, master_, autoBackoff); init(name_, len_, master_, autoBackoff);
} }
@ -365,7 +337,6 @@ namespace IPC {
///\param rhs The page to copy ///\param rhs The page to copy
sharedPage::sharedPage(const sharedPage & rhs) { sharedPage::sharedPage(const sharedPage & rhs) {
handle = 0; handle = 0;
name = "";
len = 0; len = 0;
master = false; master = false;
mapped = 0; mapped = 0;
@ -375,10 +346,10 @@ namespace IPC {
///\brief Default destructor ///\brief Default destructor
sharedPage::~sharedPage() { sharedPage::~sharedPage() {
unmap(); unmap();
if (master) { close(handle);
if (master && name != "") {
unlink(name.c_str()); unlink(name.c_str());
} }
close(handle);
} }
#endif #endif