Cygwin: fifo: Utilize select_sem for fifo as well as pipe.

This commit is contained in:
Takashi Yano 2021-09-10 08:43:59 +09:00 committed by Corinna Vinschen
parent ba4c58299f
commit 0b538118b2
2 changed files with 41 additions and 13 deletions

View File

@ -1047,6 +1047,11 @@ writer_shmem:
ResetEvent (writer_opening); ResetEvent (writer_opening);
nwriters_unlock (); nwriters_unlock ();
success: success:
if (!select_sem)
{
__small_sprintf (npbuf, "semaphore.%08x.%016X", get_dev (), get_ino ());
select_sem = CreateSemaphore (sa_buf, 0, INT32_MAX, npbuf);
}
return 1; return 1;
err_close_reader: err_close_reader:
saved_errno = get_errno (); saved_errno = get_errno ();
@ -1233,9 +1238,7 @@ fhandler_fifo::raw_read (void *in_ptr, size_t& len)
if (io.Information > 0) if (io.Information > 0)
{ {
len = io.Information; len = io.Information;
fifo_client_unlock (); goto out;
reading_unlock ();
return;
} }
break; break;
case STATUS_PIPE_EMPTY: case STATUS_PIPE_EMPTY:
@ -1271,9 +1274,7 @@ fhandler_fifo::raw_read (void *in_ptr, size_t& len)
if (j < nhandlers) if (j < nhandlers)
fc_handler[j].last_read = false; fc_handler[j].last_read = false;
fc_handler[i].last_read = true; fc_handler[i].last_read = true;
fifo_client_unlock (); goto out;
reading_unlock ();
return;
} }
break; break;
case STATUS_PIPE_EMPTY: case STATUS_PIPE_EMPTY:
@ -1310,9 +1311,7 @@ fhandler_fifo::raw_read (void *in_ptr, size_t& len)
if (j < nhandlers) if (j < nhandlers)
fc_handler[j].last_read = false; fc_handler[j].last_read = false;
fc_handler[i].last_read = true; fc_handler[i].last_read = true;
fifo_client_unlock (); goto out;
reading_unlock ();
return;
} }
break; break;
case STATUS_PIPE_EMPTY: case STATUS_PIPE_EMPTY:
@ -1345,7 +1344,7 @@ maybe_retry:
else else
{ {
/* Allow interruption and don't hog the CPU. */ /* Allow interruption and don't hog the CPU. */
DWORD waitret = cygwait (NULL, 1, cw_cancel | cw_sig_eintr); DWORD waitret = cygwait (select_sem, 1, cw_cancel | cw_sig_eintr);
if (waitret == WAIT_CANCELED) if (waitret == WAIT_CANCELED)
pthread::static_cancel_self (); pthread::static_cancel_self ();
else if (waitret == WAIT_SIGNALED) else if (waitret == WAIT_SIGNALED)
@ -1368,6 +1367,12 @@ maybe_retry:
} }
errout: errout:
len = (size_t) -1; len = (size_t) -1;
return;
out:
fifo_client_unlock ();
reading_unlock ();
if (select_sem)
ReleaseSemaphore (select_sem, get_obj_handle_count (select_sem), NULL);
} }
int __reg2 int __reg2
@ -1569,6 +1574,11 @@ fhandler_fifo::close ()
NtClose (write_ready); NtClose (write_ready);
if (writer_opening) if (writer_opening)
NtClose (writer_opening); NtClose (writer_opening);
if (select_sem)
{
ReleaseSemaphore (select_sem, get_obj_handle_count (select_sem), NULL);
NtClose (select_sem);
}
if (nohandle ()) if (nohandle ())
return 0; return 0;
else else
@ -1634,6 +1644,14 @@ fhandler_fifo::dup (fhandler_base *child, int flags)
} }
if (fhf->reopen_shmem () < 0) if (fhf->reopen_shmem () < 0)
goto err_close_shmem_handle; goto err_close_shmem_handle;
if (select_sem &&
!DuplicateHandle (GetCurrentProcess (), select_sem,
GetCurrentProcess (), &fhf->select_sem,
0, !(flags & O_CLOEXEC), DUPLICATE_SAME_ACCESS))
{
__seterrno ();
goto err_close_shmem;
}
if (reader) if (reader)
{ {
/* Make sure the child starts unlocked. */ /* Make sure the child starts unlocked. */
@ -1648,7 +1666,7 @@ fhandler_fifo::dup (fhandler_base *child, int flags)
0, !(flags & O_CLOEXEC), DUPLICATE_SAME_ACCESS)) 0, !(flags & O_CLOEXEC), DUPLICATE_SAME_ACCESS))
{ {
__seterrno (); __seterrno ();
goto err_close_shmem; goto err_close_select_sem;
} }
if (fhf->reopen_shared_fc_handler () < 0) if (fhf->reopen_shared_fc_handler () < 0)
goto err_close_shared_fc_hdl; goto err_close_shared_fc_hdl;
@ -1696,6 +1714,8 @@ err_close_shared_fc_handler:
NtUnmapViewOfSection (GetCurrentProcess (), fhf->shared_fc_handler); NtUnmapViewOfSection (GetCurrentProcess (), fhf->shared_fc_handler);
err_close_shared_fc_hdl: err_close_shared_fc_hdl:
NtClose (fhf->shared_fc_hdl); NtClose (fhf->shared_fc_hdl);
err_close_select_sem:
NtClose (fhf->select_sem);
err_close_shmem: err_close_shmem:
NtUnmapViewOfSection (GetCurrentProcess (), fhf->shmem); NtUnmapViewOfSection (GetCurrentProcess (), fhf->shmem);
err_close_shmem_handle: err_close_shmem_handle:
@ -1720,6 +1740,8 @@ fhandler_fifo::fixup_after_fork (HANDLE parent)
fork_fixup (parent, shmem_handle, "shmem_handle"); fork_fixup (parent, shmem_handle, "shmem_handle");
if (reopen_shmem () < 0) if (reopen_shmem () < 0)
api_fatal ("Can't reopen shared memory during fork, %E"); api_fatal ("Can't reopen shared memory during fork, %E");
if (select_sem)
fork_fixup (parent, select_sem, "select_sem");
if (reader) if (reader)
{ {
/* Make sure the child starts unlocked. */ /* Make sure the child starts unlocked. */

View File

@ -963,7 +963,13 @@ start_thread_fifo (select_record *me, select_stuff *stuff)
{ {
pi->start = &stuff->start; pi->start = &stuff->start;
pi->stop_thread = false; pi->stop_thread = false;
pi->bye = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL); pi->bye = me->fh->get_select_sem ();
if (pi->bye)
DuplicateHandle (GetCurrentProcess (), pi->bye,
GetCurrentProcess (), &pi->bye,
0, 0, DUPLICATE_SAME_ACCESS);
else
pi->bye = CreateSemaphore (&sec_none_nih, 0, INT32_MAX, NULL);
pi->thread = new cygthread (thread_fifo, pi, "fifosel"); pi->thread = new cygthread (thread_fifo, pi, "fifosel");
me->h = *pi->thread; me->h = *pi->thread;
if (!me->h) if (!me->h)
@ -981,7 +987,7 @@ fifo_cleanup (select_record *, select_stuff *stuff)
if (pi->thread) if (pi->thread)
{ {
pi->stop_thread = true; pi->stop_thread = true;
SetEvent (pi->bye); ReleaseSemaphore (pi->bye, get_obj_handle_count (pi->bye), NULL);
pi->thread->detach (); pi->thread->detach ();
CloseHandle (pi->bye); CloseHandle (pi->bye);
} }