summaryrefslogtreecommitdiff
path: root/3rdparty/plibsys/src/pshm-os2.c
blob: 92e0779f98bdd7c84da16706e8f11cf548495a87 (plain)
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
/*
 * The MIT License
 *
 * Copyright (C) 2017 Alexander Saprykin <saprykin.spb@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * 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 OR COPYRIGHT HOLDERS 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.
 */

#include "perror.h"
#include "pmem.h"
#include "pshm.h"
#include "perror-private.h"
#include "pipc-private.h"

#include <stdlib.h>
#include <string.h>

#define INCL_DOSMEMMGR
#define INCL_DOSSEMAPHORES
#define INCL_DOSERRORS
#include <os2.h>

#define P_SHM_MEM_PREFIX	"\\SHAREMEM\\"
#define P_SHM_SEM_PREFIX	"\\SEM32\\"
#define P_SHM_SUFFIX		"_p_shm_object"

struct PShm_ {
	pchar		*platform_key;
	ppointer	addr;
	psize		size;
	HMTX		sem;
	PShmAccessPerms	perms;
};

static pboolean pp_shm_create_handle (PShm *shm, PError **error);
static void pp_shm_clean_handle (PShm *shm);

static pboolean
pp_shm_create_handle (PShm	*shm,
		      PError	**error)
{
	pchar	*mem_name;
	pchar	*sem_name;
	APIRET	ulrc;
	ULONG	flags;

	if (P_UNLIKELY (shm == NULL || shm->platform_key == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IPC_INVALID_ARGUMENT,
				     0,
				     "Invalid input argument");
		return FALSE;
	}

	flags = PAG_COMMIT | PAG_READ;

	if (shm->perms != P_SHM_ACCESS_READONLY)
		flags |= PAG_WRITE;

	if (P_UNLIKELY ((mem_name = p_malloc0 (strlen (shm->platform_key) +
					       strlen (P_SHM_MEM_PREFIX) + 1)) == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IPC_NO_RESOURCES,
				     0,
				     "Failed to allocate memory for shared memory name");
		return FALSE;
	}

	strcpy (mem_name, P_SHM_MEM_PREFIX);
	strcat (mem_name, shm->platform_key);

	while ((ulrc = DosAllocSharedMem ((PPVOID) &shm->addr,
					  (PSZ) mem_name,
					  shm->size,
					  flags)) == ERROR_INTERRUPT)
		;

	if (P_UNLIKELY (ulrc != NO_ERROR && ulrc != ERROR_ALREADY_EXISTS)) {
		p_error_set_error_p (error,
				     (pint) p_error_get_ipc_from_system ((pint) ulrc),
				     (pint) ulrc,
				     "Failed to call DosAllocSharedMem() to allocate shared memory");
		p_free (mem_name);
		pp_shm_clean_handle (shm);
		return FALSE;
	}

	if (ulrc == ERROR_ALREADY_EXISTS) {
		ULONG real_size;
		ULONG real_flags;

		flags = (shm->perms == P_SHM_ACCESS_READONLY) ? PAG_READ : (PAG_WRITE | PAG_READ);

		while ((ulrc = DosGetNamedSharedMem ((PPVOID) &shm->addr,
						     (PSZ) mem_name,
						     flags)) == ERROR_INTERRUPT)
			;

		p_free (mem_name);

		if (P_UNLIKELY (ulrc != NO_ERROR)) {
			p_error_set_error_p (error,
					     (pint) p_error_get_ipc_from_system ((pint) ulrc),
					     (pint) ulrc,
					     "Failed to call DosGetNamedSharedMem() to get shared memory");
			pp_shm_clean_handle (shm);
			return FALSE;
		}

		real_size = (ULONG) shm->size;

		while ((ulrc = DosQueryMem ((PVOID) shm->addr,
					    &real_size,
					    &real_flags)) == ERROR_INTERRUPT)
			;

		if (P_UNLIKELY (ulrc != NO_ERROR)) {
			p_error_set_error_p (error,
					     (pint) p_error_get_ipc_from_system ((pint) ulrc),
					     (pint) ulrc,
					     "Failed to call DosQueryMem() to get memory info");
			pp_shm_clean_handle (shm);
			return FALSE;
		}

		shm->size = (psize) real_size;
	} else
		p_free (mem_name);

	if (P_UNLIKELY ((sem_name = p_malloc0 (strlen (shm->platform_key) +
					       strlen (P_SHM_SEM_PREFIX) + 1)) == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IPC_NO_RESOURCES,
				     0,
				     "Failed to allocate memory for shared memory name");
		pp_shm_clean_handle (shm);
		return FALSE;
	}

	strcpy (sem_name, P_SHM_SEM_PREFIX);
	strcat (sem_name, shm->platform_key);

	ulrc = DosCreateMutexSem ((PSZ) sem_name, &shm->sem, 0, FALSE);

	if (ulrc == ERROR_DUPLICATE_NAME)
		ulrc = DosOpenMutexSem ((PSZ) sem_name, &shm->sem);

	p_free (sem_name);

	if (P_UNLIKELY (ulrc != NO_ERROR)) {
		p_error_set_error_p (error,
				     (pint) p_error_get_ipc_from_system ((pint) ulrc),
				     (pint) ulrc,
				     "Failed to call DosCreateMutexSem() to create a lock");
		pp_shm_clean_handle (shm);
		return FALSE;
	}

	return TRUE;
}

static void
pp_shm_clean_handle (PShm *shm)
{
	APIRET ulrc;

	if (P_UNLIKELY (shm->addr != NULL)) {
		while ((ulrc = DosFreeMem ((PVOID) shm->addr)) == ERROR_INTERRUPT)
			;

		if (P_UNLIKELY (ulrc != NO_ERROR))
			P_ERROR ("PShm::pp_shm_clean_handle: DosFreeMem() failed");

		shm->addr = NULL;
	}

	if (P_LIKELY (shm->sem != NULLHANDLE)) {
		if (P_UNLIKELY (DosCloseMutexSem (shm->sem) != NO_ERROR))
			P_ERROR ("PShm::pp_shm_clean_handle: DosCloseMutexSem() failed");

		shm->sem = NULLHANDLE;
	}

	shm->size = 0;
}

P_LIB_API PShm *
p_shm_new (const pchar		*name,
	   psize		size,
	   PShmAccessPerms	perms,
	   PError		**error)
{
	PShm	*ret;
	pchar	*new_name;

	if (P_UNLIKELY (name == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IPC_INVALID_ARGUMENT,
				     0,
				     "Invalid input argument");
		return NULL;
	}

	if (P_UNLIKELY ((ret = p_malloc0 (sizeof (PShm))) == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IPC_NO_RESOURCES,
				     0,
				     "Failed to allocate memory for shared segment");
		return NULL;
	}

	if (P_UNLIKELY ((new_name = p_malloc0 (strlen (name) + strlen (P_SHM_SUFFIX) + 1)) == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IPC_NO_RESOURCES,
				     0,
				     "Failed to allocate memory for segment name");
		p_shm_free (ret);
		return NULL;
	}

	strcpy (new_name, name);
	strcat (new_name, P_SHM_SUFFIX);

	ret->platform_key = p_ipc_get_platform_key (new_name, FALSE);
	ret->perms        = perms;
	ret->size         = size;

	p_free (new_name);

	if (P_UNLIKELY (pp_shm_create_handle (ret, error) == FALSE)) {
		p_shm_free (ret);
		return NULL;
	}

	if (P_LIKELY (ret->size > size && size != 0))
		ret->size = size;

	return ret;
}

P_LIB_API void
p_shm_take_ownership (PShm *shm)
{
	P_UNUSED (shm);
}

P_LIB_API void
p_shm_free (PShm *shm)
{
	if (P_UNLIKELY (shm == NULL))
		return;

	pp_shm_clean_handle (shm);

	if (P_LIKELY (shm->platform_key != NULL))
		p_free (shm->platform_key);

	p_free (shm);
}

P_LIB_API pboolean
p_shm_lock (PShm	*shm,
	    PError	**error)
{
	APIRET ulrc;

	if (P_UNLIKELY (shm == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IPC_INVALID_ARGUMENT,
				     0,
				     "Invalid input argument");
		return FALSE;
	}

	while ((ulrc = DosRequestMutexSem (shm->sem,
					   (ULONG) SEM_INDEFINITE_WAIT)) == ERROR_INTERRUPT)
		;

	if (P_UNLIKELY (ulrc != NO_ERROR)) {
		p_error_set_error_p (error,
				     (pint) p_error_get_ipc_from_system ((pint) ulrc),
				     (pint) ulrc,
				     "Failed to lock memory segment");
		return FALSE;
	}

	return TRUE;
}

P_LIB_API pboolean
p_shm_unlock (PShm	*shm,
	      PError	**error)
{
	APIRET ulrc;

	if (P_UNLIKELY (shm == NULL)) {
		p_error_set_error_p (error,
				     (pint) P_ERROR_IPC_INVALID_ARGUMENT,
				     0,
				     "Invalid input argument");
		return FALSE;
	}

	ulrc = DosReleaseMutexSem (shm->sem);

	if (P_UNLIKELY (ulrc != NO_ERROR)) {
		p_error_set_error_p (error,
				     (pint) p_error_get_ipc_from_system ((pint) ulrc),
				     (pint) ulrc,
				     "Failed to unlock memory segment");
		return FALSE;
	}

	return TRUE;
}

P_LIB_API ppointer
p_shm_get_address (const PShm *shm)
{
	if (P_UNLIKELY (shm == NULL))
		return NULL;

	return shm->addr;
}

P_LIB_API psize
p_shm_get_size (const PShm *shm)
{
	if (P_UNLIKELY (shm == NULL))
		return 0;

	return shm->size;
}