Skip to content

Commit b00426f

Browse files
committed
Fix shared memory locking on Windows
* Correct type from long* to long * Removed volatile from lock cast (to avoid VC6 warning)
1 parent 673f2e5 commit b00426f

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

Modelica_DeviceDrivers/Resources/Include/MDDSharedMemory.h

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,29 @@
2121
#include <conio.h>
2222
#include <tchar.h>
2323

24-
struct sharedMemoryBuffer {
25-
char * sharedMemoryBuf;
26-
char * sharedMemoryBufExport;
24+
/** Shared memory object */
25+
typedef struct {
26+
char * smBuf;
27+
char * smBufExport;
2728
HANDLE hMapFile;
28-
};
29+
} MDDSharedMemory;
30+
31+
#define MDDSM_PLOCK ((long*)smb->smBuf)
32+
#define MDDSM_PLEN (MDDSM_PLOCK + sizeof(long))
33+
#define MDDSM_DATA (MDDSM_PLEN + sizeof(int))
34+
#define MDDSM_LOCK() while (InterlockedExchange(MDDSM_PLOCK, 1L) != 0) Sleep(0)
35+
#define MDDSM_UNLOCK() InterlockedExchange(MDDSM_PLOCK, 0)
2936

3037
DllExport void* MDD_SharedMemoryConstructor(const char * name, int bufSize) {
31-
struct sharedMemoryBuffer * smb = (struct sharedMemoryBuffer *)malloc(sizeof(struct sharedMemoryBuffer));
38+
MDDSharedMemory * smb = (MDDSharedMemory *)malloc(sizeof(MDDSharedMemory));
3239
smb->hMapFile = CreateFileMappingA(
3340
INVALID_HANDLE_VALUE, /* use paging file */
3441
NULL, /* default security */
3542
PAGE_READWRITE, /* read/write access */
3643
0, /* maximum object size (high-order DWORD) */
37-
sizeof(long*) + sizeof(int) + bufSize, /* maximum object size (low-order DWORD) */
44+
sizeof(long) + sizeof(int) + bufSize, /* maximum object size (low-order DWORD) */
3845
name); /* name of mapping object */
39-
if(GetLastError() == ERROR_ALREADY_EXISTS) {
46+
if (GetLastError() == ERROR_ALREADY_EXISTS) {
4047
smb->hMapFile = OpenFileMappingA(
4148
FILE_MAP_ALL_ACCESS, /* read/write access */
4249
FALSE, /* do not inherit the name */
@@ -49,71 +56,65 @@ DllExport void* MDD_SharedMemoryConstructor(const char * name, int bufSize) {
4956
ModelicaFormatError("MDDSharedMemory.h: Could not create file mapping object (%d).\n", GetLastError());
5057
return NULL;
5158
}
52-
smb->sharedMemoryBuf = (char*) MapViewOfFile(
59+
smb->smBuf = (char*) MapViewOfFile(
5360
smb->hMapFile, /* handle to map object */
5461
FILE_MAP_ALL_ACCESS, /* read/write permission */
5562
0,
5663
0,
57-
sizeof(long*) + sizeof(int) + bufSize);
64+
sizeof(long) + sizeof(int) + bufSize);
5865

59-
if (smb->sharedMemoryBuf == NULL) {
66+
if (smb->smBuf == NULL) {
6067
CloseHandle(smb->hMapFile);
6168
free(smb);
6269
ModelicaFormatError("MDDSharedMemory.h: Could not map view of file (%d).\n", GetLastError());
6370
return NULL;
6471
}
65-
smb->sharedMemoryBufExport = (char*) calloc(bufSize, 1);
72+
smb->smBufExport = (char*) calloc(bufSize, 1);
6673

6774
return smb;
6875
}
6976

7077
DllExport void MDD_SharedMemoryDestructor(void* p_smb) {
71-
struct sharedMemoryBuffer * smb = (struct sharedMemoryBuffer *) p_smb;
78+
MDDSharedMemory * smb = (MDDSharedMemory *) p_smb;
7279
if (smb) {
73-
if (smb->sharedMemoryBufExport) {
74-
free(smb->sharedMemoryBufExport);
75-
}
76-
UnmapViewOfFile(smb->sharedMemoryBuf);
80+
if (smb->smBufExport) {
81+
free(smb->smBufExport);
82+
}
83+
UnmapViewOfFile(smb->smBuf);
7784
CloseHandle(smb->hMapFile);
7885
free(smb);
7986
}
8087
}
8188

8289
DllExport int MDD_SharedMemoryGetDataSize(void * p_smb) {
8390
int len = 0;
84-
struct sharedMemoryBuffer * smb = (struct sharedMemoryBuffer *) p_smb;
91+
MDDSharedMemory * smb = (MDDSharedMemory *) p_smb;
8592
if (smb) {
86-
while (InterlockedExchange((volatile long*)smb->sharedMemoryBuf, 1L) != 0) {
87-
Sleep(0);
88-
}
89-
memcpy(&len, smb->sharedMemoryBuf + sizeof(long*), sizeof(int));
90-
InterlockedExchange((volatile long*)smb->sharedMemoryBuf, 0);
93+
MDDSM_LOCK();
94+
memcpy(&len, MDDSM_PLEN, sizeof(int));
95+
MDDSM_UNLOCK();
9196
}
9297
return len;
9398
}
9499

95100
DllExport const char * MDD_SharedMemoryRead(void * p_smb) {
96-
struct sharedMemoryBuffer * smb = (struct sharedMemoryBuffer *) p_smb;
101+
MDDSharedMemory * smb = (MDDSharedMemory *) p_smb;
97102
if (smb) {
98103
int len;
99-
while (InterlockedExchange((volatile long*)smb->sharedMemoryBuf, 1L) != 0) {
100-
Sleep(0);
101-
}
102-
memcpy(&len, smb->sharedMemoryBuf + sizeof(long*), sizeof(int));
103-
memcpy(smb->sharedMemoryBufExport, smb->sharedMemoryBuf + sizeof(long*) + sizeof(int), len);
104-
InterlockedExchange((volatile long*)smb->sharedMemoryBuf, 0);
104+
MDDSM_LOCK();
105+
memcpy(&len, MDDSM_PLEN, sizeof(int));
106+
memcpy(smb->smBufExport, MDDSM_DATA, len);
107+
MDDSM_UNLOCK();
105108
}
106-
return (const char*) smb->sharedMemoryBufExport;
109+
return (const char*) smb->smBufExport;
107110
}
108111

109112
DllExport void MDD_SharedMemoryWrite(void * p_smb, const char * buffer, int len) {
110-
struct sharedMemoryBuffer * smb = (struct sharedMemoryBuffer *) p_smb;
111-
while (InterlockedExchange((volatile long*)smb->sharedMemoryBuf, 1L) != 0) {
112-
Sleep(0);
113-
}
114-
memcpy(smb->sharedMemoryBuf + sizeof(long*) + sizeof(int), buffer, len);
115-
memcpy(smb->sharedMemoryBuf + sizeof(long*), &len, sizeof(int));
116-
InterlockedExchange((volatile long*)smb->sharedMemoryBuf, 0);
113+
MDDSharedMemory * smb = (MDDSharedMemory *) p_smb;
114+
MDDSM_LOCK();
115+
memcpy(MDDSM_DATA, buffer, len);
116+
memcpy(MDDSM_PLEN, &len, sizeof(int));
117+
MDDSM_UNLOCK();
117118
}
118119

119120
#elif defined(__linux__)

0 commit comments

Comments
 (0)