summaryrefslogtreecommitdiffhomepage
path: root/src/sync/ntapi_tt_sync_block.c
blob: f05f6b481d1b433efb4c7b83f2c4685382e5004d (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
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntapi/nt_atomic.h>
#include <ntapi/nt_object.h>
#include <ntapi/nt_sync.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

static void __sync_block_memset(
	__in	nt_sync_block *	sync_block,
	__in	intptr_t	value)
{
	intptr_t * sptr = sync_block->cache_line;

	at_store(&sptr[0x0],value);
	at_store(&sptr[0x1],value);
	at_store(&sptr[0x2],value);
	at_store(&sptr[0x3],value);
	at_store(&sptr[0x4],value);
	at_store(&sptr[0x5],value);
	at_store(&sptr[0x6],value);
	at_store(&sptr[0x7],value);

	if (sizeof(intptr_t) == 4) {
		at_store(&sptr[0x8],value);
		at_store(&sptr[0x9],value);
		at_store(&sptr[0xa],value);
		at_store(&sptr[0xb],value);
		at_store(&sptr[0xc],value);
		at_store(&sptr[0xd],value);
		at_store(&sptr[0xe],value);
		at_store(&sptr[0xf],value);
	}
}

void __stdcall __ntapi_tt_sync_block_init(
	__in	nt_sync_block *	sync_block,
	__in	uint32_t	flags			__optional,
	__in	int32_t		srvtid			__optional,
	__in	int32_t		default_lock_tries	__optional,
	__in	int64_t		default_lock_wait	__optional,
	__in	void *		hsignal			__optional)
{
	__sync_block_memset(
		sync_block,0);

	at_store_32(
		&sync_block->lock_tries,
		default_lock_tries
			? default_lock_tries
			: __NT_SYNC_BLOCK_LOCK_TRIES);

	at_store_64(
		&sync_block->lock_wait.quad,
		default_lock_wait
			? default_lock_wait
			: (-1));

	at_store_32(
		(int32_t *)&sync_block->flags,
		flags);

	at_store_32(
		(int32_t *)&sync_block->srvtid,
		srvtid);

	at_store(
		(intptr_t *)&sync_block->hsignal,
		(intptr_t)hsignal);
}


int32_t __stdcall __ntapi_tt_sync_block_lock(
	__in	nt_sync_block *	sync_block,
	__in	int32_t		lock_tries	__optional,
	__in	int64_t		lock_wait	__optional,
	__in	uint32_t *	sig_flag	__optional)
{
	int32_t		status;
	int32_t		tid;
	intptr_t	lock;
	void *		hwait[2];
	nt_timeout	timeout;

	/* validation */
	if (sync_block->invalid)
		return NT_STATUS_INVALID_HANDLE;

	/* already owned? */
	if ((tid = pe_get_current_thread_id()) == sync_block->tid)
		return NT_STATUS_SUCCESS;

	/* yield to server? */
	if ((sync_block->flags & NT_SYNC_BLOCK_YIELD_TO_SERVER)
			&& ((uint32_t)tid != sync_block->srvtid)) {
		hwait[0] = sync_block->hserver;
		hwait[1] = sync_block->hsignal;

		/* signal support */
		if (sig_flag && *sig_flag)
			return NT_STATUS_ALERTED;

		/* wait */
		status = __ntapi->zw_wait_for_multiple_objects(
			2,hwait,
			NT_WAIT_ANY,
			NT_SYNC_NON_ALERTABLE,
			0);

		/* signal support */
		if (sig_flag && *sig_flag)
			return NT_STATUS_ALERTED;
	}

	/* first try */
	lock = at_locked_cas_32(
		&sync_block->tid,
		0,tid);

	if (lock && (lock_tries == 1))
		return NT_STATUS_NOT_LOCKED;

	/* first-time contended case? */
	if (lock && !sync_block->hwait) {
		if ((status = __ntapi->tt_create_inheritable_event(
				&hwait[0],
				NT_NOTIFICATION_EVENT,
				NT_EVENT_NOT_SIGNALED)))
			return status;

		lock = at_locked_cas(
			(intptr_t *)&sync_block->hwait,
			0,(intptr_t)hwait[0]);

		if (lock)
			__ntapi->zw_close(hwait[0]);

		/* try again without a wait */
		lock = at_locked_cas_32(
			&sync_block->tid,
			0,tid);
	}

	/* contended case? */
	if (lock) {
		hwait[0] = sync_block->hwait;
		hwait[1] = sync_block->hsignal;

		lock_tries = lock_tries
			? lock_tries
			: sync_block->lock_tries;

		timeout.quad = lock_wait
			? lock_wait
			: sync_block->lock_wait.quad;

		for (; lock && lock_tries; lock_tries--) {
			/* signal support */
			if (sig_flag && *sig_flag)
				return NT_STATUS_ALERTED;

			/* wait */
			status = __ntapi->zw_wait_for_multiple_objects(
				2,hwait,
				NT_WAIT_ANY,
				NT_SYNC_NON_ALERTABLE,
				&timeout);

			/* check status */
			if ((status >= NT_STATUS_WAIT_0) && (status < NT_STATUS_WAIT_CAP))
				(void)0;
			else if (status == NT_STATUS_TIMEOUT)
				(void)0;
			else
				return status;

			/* signal support */
			if (sig_flag && *sig_flag)
				return NT_STATUS_ALERTED;

			/* try again */
			lock = at_locked_cas_32(
				&sync_block->tid,
				0,tid);
		};
	}

	if (lock)
		return NT_STATUS_NOT_LOCKED;

	/* shared section support */
	at_store_32(
		&sync_block->pid,
		pe_get_current_process_id());

	return NT_STATUS_SUCCESS;
}


int32_t __stdcall __ntapi_tt_sync_block_server_lock(
	__in	nt_sync_block *	sync_block,
	__in	int32_t		lock_tries	__optional,
	__in	int64_t		lock_wait	__optional,
	__in	uint32_t *	sig_flag	__optional)
{
	int32_t status;

	/* validation */
	if (sync_block->invalid)
		return NT_STATUS_INVALID_HANDLE;

	else if (sync_block->srvtid != pe_get_current_thread_id())
		return NT_STATUS_RESOURCE_NOT_OWNED;

	/* try once without yield request */
	status = __ntapi_tt_sync_block_lock(
		sync_block,
		1,lock_wait,
		sig_flag);

	if (status == NT_STATUS_SUCCESS)
		return status;

	/* hserver */
	if (!sync_block->hserver) {
		if ((status = __ntapi->tt_create_inheritable_event(
				&sync_block->hserver,
				NT_NOTIFICATION_EVENT,
				NT_EVENT_NOT_SIGNALED)))
			return status;
	} else {
		if ((status = __ntapi->zw_reset_event(
				sync_block->hserver,0)))
			return status;
	}

	/* yield request: set */
	at_locked_or_32(
		(int32_t *)&sync_block->flags,
		NT_SYNC_BLOCK_YIELD_TO_SERVER);

	/* try again */
	status = __ntapi_tt_sync_block_lock(
		sync_block,
		lock_tries,
		lock_wait,
		sig_flag);

	/* yield request: unset */
	at_locked_xor_32(
		(int32_t *)&sync_block->flags,
		NT_SYNC_BLOCK_YIELD_TO_SERVER);

	__ntapi->zw_set_event(
		sync_block->hserver,
		0);

	/* (locking not guaranteed) */
	return status;
}


int32_t __stdcall __ntapi_tt_sync_block_unlock(
	__in	nt_sync_block *	sync_block)
{
	union {
		int64_t			i64;
		nt_large_integer	nti64;
	} cmp;

	if (sync_block->invalid)
		return NT_STATUS_INVALID_HANDLE;

	cmp.nti64.ihigh = pe_get_current_process_id();
	cmp.nti64.ulow  = pe_get_current_thread_id();

	if (cmp.i64 != at_locked_cas_64(
			(int64_t *)&sync_block->tid,
			cmp.i64,0))
		return NT_STATUS_RESOURCE_NOT_OWNED;

	return NT_STATUS_SUCCESS;
}


int32_t __stdcall __ntapi_tt_sync_block_discard(
	__in	nt_sync_block *	sync_block)
{
	if (sync_block->hwait)
		__ntapi->zw_close(
			sync_block->hwait);

	if (sync_block->hserver)
		__ntapi->zw_close(
			sync_block->hserver);

	__sync_block_memset(
		sync_block,-1);

	return NT_STATUS_SUCCESS;
}