-
Notifications
You must be signed in to change notification settings - Fork 20
/
ipc.h
400 lines (314 loc) · 9.77 KB
/
ipc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* ipc.h - v0.2 - public domain cross platform inter process communication
no warranty implied; use at your own risk
by Jari Komppa, http://iki.fi/sol/
INCLUDE
Do this:
#define IPC_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation.
// i.e. it should look like this:
#include ...
#include ...
#include ...
#define IPC_IMPLEMENTATION
#include "ipc.h"
You can #define IPC_MALLOC, and IPC_FREE to avoid using malloc,free
LICENSE
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
USAGE
Shared memory API:
void ipc_mem_init(ipc_sharedmemory *mem, unsigned char *name, size_t size);
- Initialize ipc_sharedmemory structure for use. Call this first.
int ipc_mem_open_existing(ipc_sharedmemory *mem);
- Try to open existing shared memory. Returns 0 for success.
int ipc_mem_create(ipc_sharedmemory *mem);
- Try to create shared memory. Returns 0 for success.
void ipc_mem_close(ipc_sharedmemory *mem);
- Close shared memory and free allocated stuff. Shared memory will only get
destroyed when nobody is accessing it. Call this last.
unsigned char *ipc_mem_access(ipc_sharedmemory *mem);
- Access the shared memory. Same as poking mem->data directly, but some people
like accessors.
Shared semaphore API:
void ipc_sem_init(ipc_sharedsemaphore *sem, unsigned char *name);
- Initialize ipc_sharedsemaphore for use. Call this first.
int ipc_sem_create(ipc_sharedsemaphore *sem, int initialvalue);
- Try to create semaphore. Returns 0 for success.
void ipc_sem_close(ipc_sharedsemaphore *sem);
- Close the semaphore and deallocate. Shared semaphore will only go away after
nobody is using it. Call this last.
void ipc_sem_increment(ipc_sharedsemaphore *sem);
- Increment the semaphore.
void ipc_sem_decrement(ipc_sharedsemaphore *sem);
- Decrement the semaphore, waiting forever if need be.
int ipc_sem_try_decrement(ipc_sharedsemaphore *sem);
- Try to decrement the semaphore, returns 1 for success, 0 for failure.
TROUBLESHOOTING
- Thread programming issues apply; don't mess with the memory someone else might
be reading, make sure you release semaphore after use not to hang someone else,
etc.
- If process crashes while holding a semaphore, the others may end up waiting
forever for them to release.
- On Linux, named items will remain after your application closes unless you
close them. This is particularly fun if your application crashes. Having a
commandline mode that just tries to create and then close all your shared
resources may be convenient. Saves on rebooting, at least.. On windows, if
nobody is around to use the resource, they disappear.
*/
#ifndef IPC_H_INCLUDE_GUARD
#define IPC_H_INCLUDE_GUARD
#if defined(_WIN32)
typedef void *HANDLE;
#else
#include <semaphore.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ipc_sharedmemory_
{
char *name;
unsigned char *data;
size_t size;
#if defined(_WIN32)
HANDLE handle;
#else
int fd;
#endif
} ipc_sharedmemory;
extern void ipc_mem_init(ipc_sharedmemory *mem, char *name, size_t size);
extern int ipc_mem_open_existing(ipc_sharedmemory *mem);
extern int ipc_mem_create(ipc_sharedmemory *mem);
extern void ipc_mem_close(ipc_sharedmemory *mem);
extern unsigned char *ipc_mem_access(ipc_sharedmemory *mem);
typedef struct ipc_sharedsemaphore_
{
char *name;
#if defined(_WIN32)
HANDLE handle;
#else
sem_t *semaphore;
#endif
} ipc_sharedsemaphore;
extern void ipc_sem_init(ipc_sharedsemaphore *sem, char *name);
extern int ipc_sem_create(ipc_sharedsemaphore *sem, int initialvalue);
extern void ipc_sem_close(ipc_sharedsemaphore *sem);
extern void ipc_sem_increment(ipc_sharedsemaphore *sem);
extern void ipc_sem_decrement(ipc_sharedsemaphore *sem);
extern int ipc_sem_try_decrement(ipc_sharedsemaphore *sem);
#ifdef __cplusplus
}
#endif
//////// end of header ////////
#ifdef IPC_IMPLEMENTATION
#if defined(_WIN32)
#include <windows.h>
#else // !_WIN32
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#endif // !_WIN32
#ifndef IPC_ASSERT
#define IPC_ASSERT(x) assert(x)
#endif
#ifndef IPC_MALLOC
#define IPC_MALLOC(x) malloc(x)
#endif
#ifndef IPC_FREE
#define IPC_FREE(x) free(x)
#endif
static char * ipc_strdup(char *src)
{
int i, len;
char *dst = NULL;
len = 0;
while (src[len]) len++;
#if !defined(_WIN32)
len++;
#endif
dst = (char *)IPC_MALLOC(len+1);
if (!dst) return NULL;
dst[len] = 0;
#if defined(_WIN32)
for (i = 0; i < len; i++)
dst[i] = src[i];
#else
dst[0] = '/';
for (i = 0; i < len-1; i++)
dst[i+1] = src[i];
#endif
return dst;
}
void ipc_mem_init(ipc_sharedmemory *mem, char *name, size_t size)
{
mem->name = ipc_strdup(name);
mem->size = size;
mem->data = NULL;
#if defined(_WIN32)
mem->handle = 0;
#else
mem->fd = -1;
#endif
}
unsigned char *ipc_mem_access(ipc_sharedmemory *mem)
{
return mem->data;
}
void ipc_sem_init(ipc_sharedsemaphore *sem, char *name)
{
sem->name = ipc_strdup(name);
#if defined(_WIN32)
sem->handle = 0;
#else
sem->semaphore = NULL;
#endif
}
#if defined(_WIN32)
int ipc_mem_open_existing(ipc_sharedmemory *mem)
{
mem->handle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, mem->name);
if (!mem->handle)
return -1;
mem->data = (unsigned char*)MapViewOfFile(mem->handle, FILE_MAP_ALL_ACCESS, 0, 0, mem->size);
if (!mem->data)
return -1;
return 0;
}
int ipc_mem_create(ipc_sharedmemory *mem)
{
mem->handle = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, mem->size, mem->name);
if (!mem->handle)
return -1;
mem->data = (unsigned char*)MapViewOfFile(mem->handle, FILE_MAP_ALL_ACCESS, 0, 0, mem->size);
if (!mem->data)
return -1;
return 0;
}
void ipc_mem_close(ipc_sharedmemory *mem)
{
if (mem->data != NULL)
{
UnmapViewOfFile(mem->data);
mem->data = NULL;
}
IPC_FREE(mem->name);
mem->name = NULL;
mem->size = 0;
}
int ipc_sem_create(ipc_sharedsemaphore *sem, int initialvalue)
{
sem->handle = CreateSemaphoreA(NULL, initialvalue, 0x7fffffff, sem->name);
if (!sem->handle)
return -1;
return 0;
}
void ipc_sem_close(ipc_sharedsemaphore *sem)
{
CloseHandle(sem->handle);
IPC_FREE(sem->name);
sem->handle = 0;
}
void ipc_sem_increment(ipc_sharedsemaphore *sem)
{
ReleaseSemaphore(sem->handle, 1, NULL);
}
void ipc_sem_decrement(ipc_sharedsemaphore *sem)
{
WaitForSingleObject(sem->handle, INFINITE);
}
int ipc_sem_try_decrement(ipc_sharedsemaphore *sem)
{
DWORD ret = WaitForSingleObject(sem->handle, 0);
if (ret == WAIT_OBJECT_0)
return 1;
return 0;
}
#else // !defined(_WIN32)
int ipc_mem_open_existing(ipc_sharedmemory *mem)
{
mem->fd = shm_open(mem->name, O_RDWR, 0755);
if (mem->fd < 0)
return -1;
mem->data = (unsigned char *)mmap(NULL, mem->size, PROT_READ | PROT_WRITE, MAP_SHARED, mem->fd, 0);
if (!mem->data)
return -1;
return 0;
}
int ipc_mem_create(ipc_sharedmemory *mem)
{
int ret;
ret = shm_unlink(mem->name);
if (ret < 0 && errno != ENOENT)
return -1;
mem->fd = shm_open(mem->name, O_CREAT | O_RDWR, 0755);
if (mem->fd < 0)
return -1;
ftruncate(mem->fd, mem->size);
mem->data = (unsigned char *)mmap(NULL, mem->size, PROT_READ | PROT_WRITE, MAP_SHARED, mem->fd, 0);
if (!mem->data)
return -1;
return 0;
}
void ipc_mem_close(ipc_sharedmemory *mem)
{
if (mem->data != NULL)
{
munmap(mem->data, mem->size);
close(mem->fd);
shm_unlink(mem->name);
}
IPC_FREE(mem->name);
mem->name = NULL;
mem->size = 0;
}
int ipc_sem_create(ipc_sharedsemaphore *sem, int initialvalue)
{
sem->semaphore = sem_open(sem->name, O_CREAT, 0700, initialvalue);
if (sem->semaphore == SEM_FAILED)
return -1;
return 0;
}
void ipc_sem_close(ipc_sharedsemaphore *sem)
{
sem_close(sem->semaphore);
sem_unlink(sem->name);
IPC_FREE(sem->name);
}
void ipc_sem_increment(ipc_sharedsemaphore *sem)
{
sem_post(sem->semaphore);
}
void ipc_sem_decrement(ipc_sharedsemaphore *sem)
{
sem_wait(sem->semaphore);
}
int ipc_sem_try_decrement(ipc_sharedsemaphore *sem)
{
int res = sem_trywait(sem->semaphore);
if (res == 0)
return 1;
return 0;
}
#endif // !_WIN32
#endif // IPC_IMPLEMENTATION
#endif // IPC_H_INCLUDE_GUARD