summaryrefslogtreecommitdiffhomepage
path: root/src/socket/ntapi_sc_wait.c
blob: 5d207e2e90784b6e0387e301c7ab47991cfce2b5 (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
/********************************************************/
/*  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_sync.h>
#include <ntapi/nt_socket.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

int32_t __cdecl __ntapi_sc_wait(
	nt_socket *		hssocket,
	nt_iosb volatile *	iosb,
	nt_timeout *		timeout)
{
	nt_iosb cancel;

	timeout = (timeout && timeout->quad)
		? timeout
		: 0;

	if (hssocket->hevent && (hssocket->iostatus == NT_STATUS_PENDING)) {
		hssocket->waitstatus = __ntapi->zw_wait_for_single_object(
				hssocket->hevent,
				!!(hssocket->ntflags & NT_FILE_SYNCHRONOUS_IO_ALERT),
				timeout);

		switch (hssocket->waitstatus) {
			case NT_STATUS_SUCCESS:
				hssocket->iostatus = iosb->status;
				break;

			case NT_STATUS_ALERTED:
				hssocket->iostatus = NT_STATUS_ALERTED;
				__ntapi->zw_cancel_io_file(
					hssocket->hsocket,
					&cancel);
				break;
		}
	}

	return hssocket->iostatus;
}