Added Mac support

This commit is contained in:
Thulinma 2014-04-17 16:49:00 +02:00
parent efa5d67231
commit 4de598054a
2 changed files with 65 additions and 8 deletions

View file

@ -37,7 +37,8 @@ namespace IPC {
static void btohll(char * p, long long & val) { static void btohll(char * p, long long & val) {
val = ((long long)p[0] << 56) | ((long long)p[1] << 48) | ((long long)p[2] << 40) | ((long long)p[3] << 32) | ((long long)p[4] << 24) | ((long long)p[5] << 16) | ((long long)p[6] << 8) | p[7]; val = ((long long)p[0] << 56) | ((long long)p[1] << 48) | ((long long)p[2] << 40) | ((long long)p[3] << 32) | ((long long)p[4] << 24) | ((long long)p[5] << 16) | ((long long)p[6] << 8) | p[7];
} }
#if !defined __APPLE__ && !defined __CYGWIN__
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), name(name_), len(len_), master(master_), mapped(NULL) {
handle = 0; handle = 0;
name = name_; name = name_;
@ -130,7 +131,35 @@ namespace IPC {
close(handle); close(handle);
} }
} }
#else
sharedPage::sharedPage(std::string name_, unsigned int len_, bool master_, bool autoBackoff) /*: handle(0), name(name_), len(len_), master(master_), mapped(NULL) */{
handle = 0;
name = name_;
len = len_;
master = master_;
mapped = 0;
init(name_,len_,master_, autoBackoff);
}
sharedPage::sharedPage(const sharedPage & rhs){
handle = 0;
name = "";
len = 0;
master = false;
mapped = 0;
init(rhs.name, rhs.len, rhs.master);
}
sharedPage::~sharedPage(){
if (mapped && len){
munmap(mapped,len);
}
if(master){
unlink(name.c_str());
}
if (handle > 0){
close(handle);
}
}
#endif
sharedFile::sharedFile(std::string name_, unsigned int len_, bool master_, bool autoBackoff) : handle(0), name(name_), len(len_), master(master_), mapped(NULL) { sharedFile::sharedFile(std::string name_, unsigned int len_, bool master_, bool autoBackoff) : handle(0), name(name_), len(len_), master(master_), mapped(NULL) {
handle = 0; handle = 0;
name = name_; name = name_;
@ -336,7 +365,7 @@ namespace IPC {
mySemaphore = sem_open(std::string("/" + baseName).c_str(), O_CREAT | O_RDWR, ACCESSPERMS, 1); mySemaphore = sem_open(std::string("/" + baseName).c_str(), O_CREAT | O_RDWR, ACCESSPERMS, 1);
} }
if (mySemaphore == SEM_FAILED) { if (mySemaphore == SEM_FAILED) {
perror("Creating semaphore failed"); DEBUG_MSG(DLVL_FAIL,"Creating semaphore failed: %s", strerror(errno));
return; return;
} }
newPage(); newPage();
@ -503,9 +532,14 @@ namespace IPC {
baseName = rhs.baseName; baseName = rhs.baseName;
payLen = rhs.payLen; payLen = rhs.payLen;
hasCounter = rhs.hasCounter; hasCounter = rhs.hasCounter;
#ifdef __APPLE__
//note: O_CREAT is only needed for mac, probably
mySemaphore = sem_open(std::string("/" + baseName).c_str(), O_RDWR | O_CREAT, 0);
#else
mySemaphore = sem_open(std::string("/" + baseName).c_str(), O_RDWR); mySemaphore = sem_open(std::string("/" + baseName).c_str(), O_RDWR);
#endif
if (mySemaphore == SEM_FAILED) { if (mySemaphore == SEM_FAILED) {
perror("Creating semaphore failed"); DEBUG_MSG(DLVL_FAIL,"Creating semaphore failed: %s", strerror(errno));
return; return;
} }
semGuard tmpGuard(mySemaphore); semGuard tmpGuard(mySemaphore);
@ -517,20 +551,30 @@ namespace IPC {
baseName = rhs.baseName; baseName = rhs.baseName;
payLen = rhs.payLen; payLen = rhs.payLen;
hasCounter = rhs.hasCounter; hasCounter = rhs.hasCounter;
#ifdef __APPLE__
//note: O_CREAT is only needed for mac, probably
mySemaphore = sem_open(std::string("/" + baseName).c_str(), O_RDWR | O_CREAT, 0);
#else
mySemaphore = sem_open(std::string("/" + baseName).c_str(), O_RDWR); mySemaphore = sem_open(std::string("/" + baseName).c_str(), O_RDWR);
#endif
if (mySemaphore == SEM_FAILED) { if (mySemaphore == SEM_FAILED) {
perror("Creating semaphore failed"); DEBUG_MSG(DLVL_FAIL,"Creating semaphore failed: %s", strerror(errno));
return; return;
} }
semGuard tmpGuard(mySemaphore); semGuard tmpGuard(mySemaphore);
myPage.init(rhs.myPage.name,rhs.myPage.len,rhs.myPage.master); myPage.init(rhs.myPage.name,rhs.myPage.len,rhs.myPage.master);
offsetOnPage = rhs.offsetOnPage; offsetOnPage = rhs.offsetOnPage;
} }
sharedClient::sharedClient(std::string name, int len, bool withCounter) : baseName(name), payLen(len), offsetOnPage(-1), hasCounter(withCounter) { sharedClient::sharedClient(std::string name, int len, bool withCounter) : baseName(name), payLen(len), offsetOnPage(-1), hasCounter(withCounter) {
#ifdef __APPLE__
//note: O_CREAT is only needed for mac, probably
mySemaphore = sem_open(std::string("/" + baseName).c_str(), O_RDWR | O_CREAT, 0);
#else
mySemaphore = sem_open(std::string("/" + baseName).c_str(), O_RDWR); mySemaphore = sem_open(std::string("/" + baseName).c_str(), O_RDWR);
#endif
if (mySemaphore == SEM_FAILED) { if (mySemaphore == SEM_FAILED) {
perror("Creating semaphore failed"); DEBUG_MSG(DLVL_FAIL,"Creating semaphore failed: %s", strerror(errno));
return; return;
} }
semGuard tmpGuard(mySemaphore); semGuard tmpGuard(mySemaphore);

View file

@ -38,6 +38,7 @@ namespace IPC {
sem_t * mySemaphore; sem_t * mySemaphore;
}; };
#if !defined __APPLE__ && !defined __CYGWIN__
class sharedPage{ class sharedPage{
public: public:
sharedPage(std::string name_ = "", unsigned int len_ = 0, bool master_ = false, bool autoBackoff = true); sharedPage(std::string name_ = "", unsigned int len_ = 0, bool master_ = false, bool autoBackoff = true);
@ -55,6 +56,9 @@ namespace IPC {
bool master; bool master;
char * mapped; char * mapped;
}; };
#else
class sharedPage;
#endif
class sharedFile{ class sharedFile{
public: public:
@ -73,7 +77,16 @@ namespace IPC {
bool master; bool master;
char * mapped; char * mapped;
}; };
#if defined __APPLE__ || defined __CYGWIN__
class sharedPage: public sharedFile{
public:
sharedPage(std::string name_ = "", unsigned int len_ = 0, bool master_ = false, bool autoBackoff = true);
sharedPage(const sharedPage & rhs);
~sharedPage();
};
#endif
class sharedServer{ class sharedServer{
public: public:
sharedServer(); sharedServer();