Backported Pro shared_memory edits to OS edition
This commit is contained in:
parent
243595ff4d
commit
0d9108d1d6
2 changed files with 165 additions and 66 deletions
|
@ -12,8 +12,16 @@
|
|||
#include "shared_memory.h"
|
||||
#include "stream.h"
|
||||
#include "procs.h"
|
||||
#include "bitfields.h"
|
||||
#include "timing.h"
|
||||
|
||||
#if defined(__CYGWIN__) || defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#include <aclapi.h>
|
||||
#include <accctrl.h>
|
||||
#endif
|
||||
|
||||
|
||||
namespace IPC {
|
||||
|
||||
#if defined(__CYGWIN__) || defined(_WIN32)
|
||||
|
@ -107,7 +115,7 @@ namespace IPC {
|
|||
#if defined(__CYGWIN__) || defined(_WIN32)
|
||||
return mySem != 0;
|
||||
#else
|
||||
return mySem != SEM_FAILED;
|
||||
return mySem && mySem != SEM_FAILED;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -128,16 +136,27 @@ namespace IPC {
|
|||
if (oflag & O_CREAT) {
|
||||
if (oflag & O_EXCL) {
|
||||
//attempt opening, if succes, close handle and return false;
|
||||
HANDLE tmpSem = OpenSemaphore(0, false, semaName.c_str());
|
||||
HANDLE tmpSem = OpenMutex(SYNCHRONIZE, false, semaName.c_str());
|
||||
if (tmpSem) {
|
||||
CloseHandle(tmpSem);
|
||||
mySem = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
mySem = CreateSemaphore(0, value, 1 , semaName.c_str());
|
||||
SECURITY_ATTRIBUTES security = getSecurityAttributes();
|
||||
mySem = CreateMutex(&security, true, semaName.c_str());
|
||||
if (value){
|
||||
ReleaseMutex(mySem);
|
||||
}
|
||||
} else {
|
||||
mySem = OpenSemaphore(0, false, semaName.c_str());
|
||||
mySem = OpenMutex(SYNCHRONIZE, false, semaName.c_str());
|
||||
}
|
||||
if (!(*this)) {
|
||||
if (GetLastError() == ERROR_FILE_NOT_FOUND){//Error code 2
|
||||
Util::wait(500);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (oflag & O_CREAT) {
|
||||
|
@ -145,7 +164,6 @@ namespace IPC {
|
|||
} else {
|
||||
mySem = sem_open(name, oflag);
|
||||
}
|
||||
#endif
|
||||
if (!(*this)) {
|
||||
if (errno == ENOENT) {
|
||||
Util::wait(500);
|
||||
|
@ -153,9 +171,9 @@ namespace IPC {
|
|||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
if (!(*this)) {
|
||||
DEBUG_MSG(DLVL_VERYHIGH, "Attempt to open semaphore %s: %s", name, strerror(errno));
|
||||
}
|
||||
myName = (char *)name;
|
||||
}
|
||||
|
@ -176,7 +194,7 @@ namespace IPC {
|
|||
void semaphore::post() {
|
||||
if (*this) {
|
||||
#if defined(__CYGWIN__) || defined(_WIN32)
|
||||
ReleaseSemaphore(mySem, 1, 0);
|
||||
ReleaseMutex(mySem);
|
||||
#else
|
||||
sem_post(mySem);
|
||||
#endif
|
||||
|
@ -267,6 +285,32 @@ namespace IPC {
|
|||
}
|
||||
|
||||
|
||||
#if defined(__CYGWIN__) || defined(_WIN32)
|
||||
SECURITY_ATTRIBUTES semaphore::getSecurityAttributes() {
|
||||
///\todo We really should clean this up sometime probably
|
||||
///We currently have everything static, because the result basically depends on everything
|
||||
static SECURITY_ATTRIBUTES result;
|
||||
static bool resultValid = false;
|
||||
static SECURITY_DESCRIPTOR securityDescriptor;
|
||||
if (resultValid) {
|
||||
return result;
|
||||
}
|
||||
|
||||
InitializeSecurityDescriptor(&securityDescriptor, SECURITY_DESCRIPTOR_REVISION);
|
||||
if (!SetSecurityDescriptorDacl(&securityDescriptor, TRUE, NULL, FALSE)){
|
||||
FAIL_MSG("Failed to set pSecurityDescriptor: %u", GetLastError());
|
||||
return result;
|
||||
}
|
||||
|
||||
result.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||
result.lpSecurityDescriptor = &securityDescriptor;
|
||||
result.bInheritHandle = FALSE;
|
||||
|
||||
resultValid = true;
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
///brief Creates a shared page
|
||||
///\param name_ The name of the page to be created
|
||||
///\param len_ The size to make the page
|
||||
|
@ -910,6 +954,7 @@ namespace IPC {
|
|||
offsetOnPage = 0;
|
||||
}
|
||||
|
||||
|
||||
///\brief Copy constructor for sharedClients
|
||||
///\param rhs The client ro copy
|
||||
sharedClient::sharedClient(const sharedClient & rhs) {
|
||||
|
@ -966,6 +1011,7 @@ namespace IPC {
|
|||
DEBUG_MSG(DLVL_FAIL, "Creating semaphore %s failed: %s", baseName.c_str(), strerror(errno));
|
||||
return;
|
||||
}
|
||||
//Empty is used to compare for emptyness. This is not needed when the page uses a counter
|
||||
char * empty = 0;
|
||||
if (!hasCounter) {
|
||||
empty = (char *)malloc(payLen * sizeof(char));
|
||||
|
@ -1004,8 +1050,10 @@ namespace IPC {
|
|||
Util::wait(500);
|
||||
}
|
||||
}
|
||||
if (empty) {
|
||||
free(empty);
|
||||
}
|
||||
}
|
||||
|
||||
///\brief The deconstructor
|
||||
sharedClient::~sharedClient() {
|
||||
|
@ -1055,5 +1103,43 @@ namespace IPC {
|
|||
}
|
||||
return (myPage.mapped + offsetOnPage + (hasCounter ? 1 : 0));
|
||||
}
|
||||
|
||||
userConnection::userConnection(char * _data) {
|
||||
data = _data;
|
||||
}
|
||||
|
||||
unsigned long userConnection::getTrackId(size_t offset) const {
|
||||
if (offset >= SIMUL_TRACKS) {
|
||||
WARN_MSG("Trying to get track id for entry %lu, while there are only %d entries allowed", offset, SIMUL_TRACKS);
|
||||
return 0;
|
||||
}
|
||||
return Bit::btohl(data + (offset * 6));
|
||||
}
|
||||
|
||||
void userConnection::setTrackId(size_t offset, unsigned long trackId) const {
|
||||
if (offset >= SIMUL_TRACKS) {
|
||||
WARN_MSG("Trying to set track id for entry %lu, while there are only %d entries allowed", offset, SIMUL_TRACKS);
|
||||
return;
|
||||
}
|
||||
Bit::htobl(data + (offset * 6), trackId);
|
||||
|
||||
}
|
||||
|
||||
unsigned long userConnection::getKeynum(size_t offset) const {
|
||||
if (offset >= SIMUL_TRACKS) {
|
||||
WARN_MSG("Trying to get keynum for entry %lu, while there are only %d entries allowed", offset, SIMUL_TRACKS);
|
||||
return 0;
|
||||
}
|
||||
return Bit::btohs(data + (offset * 6) + 4);
|
||||
}
|
||||
|
||||
void userConnection::setKeynum(size_t offset, unsigned long keynum) {
|
||||
if (offset >= SIMUL_TRACKS) {
|
||||
WARN_MSG("Trying to set keynum for entry %lu, while there are only %d entries allowed", offset, SIMUL_TRACKS);
|
||||
return;
|
||||
}
|
||||
Bit::htobs(data + (offset * 6) + 4, keynum);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -69,6 +69,8 @@ namespace IPC {
|
|||
void unlink();
|
||||
private:
|
||||
#if defined(__CYGWIN__) || defined(_WIN32)
|
||||
///\todo Maybe sometime implement anything else than 777
|
||||
static SECURITY_ATTRIBUTES getSecurityAttributes();
|
||||
HANDLE mySem;
|
||||
#else
|
||||
sem_t * mySem;
|
||||
|
@ -228,4 +230,15 @@ namespace IPC {
|
|||
///\brief Whether the payload has a counter, if so, it is added in front of the payload
|
||||
bool hasCounter;
|
||||
};
|
||||
|
||||
class userConnection {
|
||||
public:
|
||||
userConnection(char * _data);
|
||||
unsigned long getTrackId(size_t offset) const;
|
||||
void setTrackId(size_t offset, unsigned long trackId) const;
|
||||
unsigned long getKeynum(size_t offset) const;
|
||||
void setKeynum(size_t offset, unsigned long keynum);
|
||||
private:
|
||||
char * data;
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue