IPCMutex needed to be rewritten for Linux due to an annoying limitation of posix semaphores that makes encapping them moronic

This commit is contained in:
KimLS 2013-02-22 23:40:16 -08:00
parent 8c425ff9d8
commit 86c8b11102

View File

@ -20,11 +20,9 @@
#ifdef _WINDOWS #ifdef _WINDOWS
#include <Windows.h> #include <Windows.h>
#else #else
#include <fcntl.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <semaphore.h> #include <fcntl.h>
#include <unistd.h>
#include <time.h>
#endif #endif
#include "types.h" #include "types.h"
#include "eqemu_exception.h" #include "eqemu_exception.h"
@ -35,7 +33,7 @@ namespace EQEmu {
#ifdef _WINDOWS #ifdef _WINDOWS
HANDLE mut_; HANDLE mut_;
#else #else
sem_t *sem_; int fd_;
#endif #endif
}; };
@ -53,11 +51,14 @@ namespace EQEmu {
EQ_EXCEPT("IPC Mutex", "Could not create mutex."); EQ_EXCEPT("IPC Mutex", "Could not create mutex.");
} }
#else #else
std::string final_name = "/EQEmuMutex_"; std::string final_name = name;
final_name += name; final_name += ".lock";
imp_->sem_ = sem_open(final_name.c_str(), O_CREAT, S_IRUSR | S_IWUSR, 1); imp_->fd_ = open(final_name.c_str(),
if(imp_->sem_ == SEM_FAILED) { O_RDWR | O_CREAT | O_CLOEXEC,
S_IRUSR | S_IWUSR);
if(imp_->fd_ == -1) {
EQ_EXCEPT("IPC Mutex", "Could not create mutex."); EQ_EXCEPT("IPC Mutex", "Could not create mutex.");
} }
#endif #endif
@ -65,19 +66,17 @@ namespace EQEmu {
IPCMutex::~IPCMutex() { IPCMutex::~IPCMutex() {
#ifdef _WINDOWS #ifdef _WINDOWS
if(imp_->mut_) { if(locked_) {
if(locked_) { ReleaseMutex(imp_->mut_);
ReleaseMutex(imp_->mut_);
}
CloseHandle(imp_->mut_);
} }
CloseHandle(imp_->mut_);
#else #else
if(imp_->sem_) { if(locked_) {
if(locked_) { lockf(imp_->fd_, F_ULOCK, 0);
sem_post(imp_->sem_);
}
sem_close(imp_->sem_);
} }
close(imp_->fd_);
#endif #endif
delete imp_; delete imp_;
} }
@ -93,9 +92,7 @@ namespace EQEmu {
return false; return false;
} }
#else #else
if(sem_wait(imp_->sem_) == -1) { lockf(imp_->fd_, F_TLOCK, 0);
return false;
}
#endif #endif
locked_ = true; locked_ = true;
return true; return true;
@ -110,9 +107,7 @@ namespace EQEmu {
return false; return false;
} }
#else #else
if(sem_post(imp_->sem_) == -1) { lockf(imp_->fd_, F_ULOCK, 0);
return false;
}
#endif #endif
locked_ = false; locked_ = false;
return true; return true;