23 #include <sys/types.h> 28 #endif // HAVE_CONFIG_H 33 #if defined(__MINGW32__) 39 HANDLE g_mutex = NULL;
41 int msgget(key_t key,
int msgflg)
45 _stprintf(name,
"MessageQueueShm%d", (
int)key);
46 g_shm = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 8192, name);
48 g_shm_data = (
void *)MapViewOfFile(g_shm, FILE_MAP_ALL_ACCESS, 0, 0, 8192);
50 _stprintf(name,
"MessageQueueMutex%d", (
int)key);
51 if (msgflg & IPC_CREAT)
53 g_mutex = CreateMutex(NULL, FALSE, name);
54 *((int32_t *)g_shm_data) = 0;
58 g_mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, name);
68 int msgsnd(
int msqid,
const void *msgp,
size_t msgsz,
int msgflg)
72 if (WaitForSingleObject(g_mutex, INFINITE) == WAIT_FAILED)
79 msgsz +=
sizeof(long);
81 pos = (
char *)g_shm_data;
86 size = *(int32_t *)pos;
90 pos +=
sizeof(int32_t) + size;
93 if ((
char *)pos + msgsz +
sizeof(int32_t) - (
char *)g_shm_data > 8192)
96 *((int32_t *)pos) = (int32_t)msgsz;
97 pos += sizeof(int32_t);
98 memcpy(pos, msgp, msgsz);
100 *((int32_t *)pos) = 0;
102 ReleaseMutex(g_mutex);
107 ssize_t msgrcv(
int msqid,
void *msgp,
size_t msgsz,
long msgtyp,
int msgflg)
114 msgsz +=
sizeof(long);
119 if (WaitForSingleObject(g_mutex, INFINITE) == WAIT_FAILED)
121 CloseHandle(g_mutex);
126 pos = (
char *)g_shm_data;
133 size = *(int32_t *)pos;
138 ptype = (int32_t *)(pos_before +
sizeof(int32_t));
139 if (((*ptype == msgtyp && msgtyp > 0) || (*ptype <= -msgtyp && msgtyp < 0)) && !pos_target)
141 pos_target = pos_before;
143 pos +=
sizeof(int32_t) + size;
150 if (*((int32_t *)pos_target) < msgsz)
152 readsize = *((int32_t *)pos_target);
158 pos_next = pos_target +
sizeof(int32_t) + *((int32_t *)pos_target);
159 sizeleft = pos - pos_next + 1;
160 memcpy(msgp, pos_target + sizeof(int32_t), readsize);
161 memcpy(pos_target, pos_next, sizeleft);
164 ReleaseMutex(g_mutex);
166 if (!(msgflg & IPC_NOWAIT) && !pos_target)
179 int msgctl(
int msqid,
int cmd,
struct msqid_ds *buf)
187 CloseHandle(g_mutex);
198 #endif // defined(__MINGW32__)