Cygwin: AF_UNIX: Add create_event helper and use throughout

Minimize overhead in creating a nameless event object.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2018-03-07 16:00:36 +01:00
parent e94fa4ebf3
commit d69bcdd671
1 changed files with 25 additions and 15 deletions

View File

@ -148,6 +148,24 @@ sun_name_t::sun_name_t (const struct sockaddr *name, socklen_t namelen)
_nul[sizeof (struct sockaddr_un)] = '\0'; _nul[sizeof (struct sockaddr_un)] = '\0';
} }
static HANDLE
create_event ()
{
NTSTATUS status;
OBJECT_ATTRIBUTES attr;
HANDLE evt = NULL;
InitializeObjectAttributes (&attr, NULL, 0, NULL, NULL);
status = NtCreateEvent (&evt, EVENT_ALL_ACCESS, &attr,
NotificationEvent, FALSE);
if (!NT_SUCCESS (status))
__seterrno_from_nt_status (status);
return evt;
}
/* Character length of pipe name, excluding trailing NUL. */
#define CYGWIN_PIPE_SOCKET_NAME_LEN 47
/* Character position encoding the socket type in a pipe name. */ /* Character position encoding the socket type in a pipe name. */
#define CYGWIN_PIPE_SOCKET_TYPE_POS 29 #define CYGWIN_PIPE_SOCKET_TYPE_POS 29
@ -555,6 +573,7 @@ fhandler_socket_unix::send_my_name ()
int int
fhandler_socket_unix::recv_peer_name () fhandler_socket_unix::recv_peer_name ()
{ {
HANDLE evt;
NTSTATUS status; NTSTATUS status;
IO_STATUS_BLOCK io; IO_STATUS_BLOCK io;
af_unix_pkt_hdr_t *packet; af_unix_pkt_hdr_t *packet;
@ -562,10 +581,12 @@ fhandler_socket_unix::recv_peer_name ()
ULONG len; ULONG len;
int ret = 0; int ret = 0;
if (!(evt = create_event ()))
return ENOBUFS;
len = sizeof *packet + sizeof *un; len = sizeof *packet + sizeof *un;
packet = (af_unix_pkt_hdr_t *) alloca (len); packet = (af_unix_pkt_hdr_t *) alloca (len);
set_pipe_non_blocking (false); set_pipe_non_blocking (false);
status = NtReadFile (get_handle (), NULL, NULL, NULL, &io, packet, len, status = NtReadFile (get_handle (), evt, NULL, NULL, &io, packet, len,
NULL, NULL); NULL, NULL);
if (status == STATUS_PENDING) if (status == STATUS_PENDING)
{ {
@ -573,7 +594,7 @@ fhandler_socket_unix::recv_peer_name ()
LARGE_INTEGER timeout; LARGE_INTEGER timeout;
timeout.QuadPart = -20 * NS100PERSEC; /* 20 secs */ timeout.QuadPart = -20 * NS100PERSEC; /* 20 secs */
ret = cygwait (connect_wait_thr, &timeout, cw_sig_eintr); ret = cygwait (evt, &timeout, cw_sig_eintr);
switch (ret) switch (ret)
{ {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
@ -835,22 +856,11 @@ fhandler_socket_unix::listen_pipe ()
{ {
NTSTATUS status; NTSTATUS status;
IO_STATUS_BLOCK io; IO_STATUS_BLOCK io;
OBJECT_ATTRIBUTES attr;
HANDLE evt = NULL; HANDLE evt = NULL;
io.Status = STATUS_PENDING; io.Status = STATUS_PENDING;
if (!is_nonblocking ()) if (!is_nonblocking () && !(evt = create_event ()))
{ return -1;
/* Create event object and set APC context pointer. */
InitializeObjectAttributes (&attr, NULL, 0, NULL, NULL);
status = NtCreateEvent (&evt, EVENT_ALL_ACCESS, &attr,
NotificationEvent, FALSE);
if (!NT_SUCCESS (status))
{
__seterrno_from_nt_status (status);
return -1;
}
}
status = NtFsControlFile (get_handle (), evt, NULL, NULL, &io, status = NtFsControlFile (get_handle (), evt, NULL, NULL, &io,
FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0); FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
if (status == STATUS_PENDING) if (status == STATUS_PENDING)