[PATCH 21/21] Cygwin: FIFO: update commentary

2020-05-07 Thread Ken Brown via Cygwin-patches
The beginning of fhandler_fifo.cc contains a long comment giving an overview of the FIFO implementation. This is now updated to describe the support for multiple readers. --- winsup/cygwin/fhandler_fifo.cc | 58 -- 1 file changed, 35 insertions(+), 23 deletions(-)

[PATCH 11/21] Cygwin: FIFO: add shared memory

2020-05-07 Thread Ken Brown via Cygwin-patches
Even though we currently allow a FIFO to be opened for reading only once, we can still have more than one reader open because of dup and fork. Add a named shared memory section accessible to all readers of a given FIFO. In future commits we will add information needed by all readers to this secti

[PATCH 14/21] Cygwin: FIFO: designate one reader as owner

2020-05-07 Thread Ken Brown via Cygwin-patches
Among all the open readers of a FIFO, one is declared to be the owner. This is the only reader that listens for client connections, and it is the only one that has an accurate fc_handler list. Add shared data and methods for getting and setting the owner, as well as a lock to prevent more than one

[PATCH 17/21] Cygwin: FIFO: take ownership on exec

2020-05-07 Thread Ken Brown via Cygwin-patches
If fixup_after_exec is called on a non-close-on-exec reader whose parent is the owner, transfer ownership to the child. Otherwise the parent's pipe handles will be closed before any other reader can duplicate them. To help with this, make the cancel_evt and thr_sync_evt handles inheritable, so th

[PATCH 18/21] Cygwin: FIFO: find a new owner when closing

2020-05-07 Thread Ken Brown via Cygwin-patches
If the owning reader is closing, wait for another reader (if there is one) to take ownership before closing the owner's pipe handles. To synchronize the ownership transfer, add events owner_needed_evt and owner_found_evt, and add methods owner_needed and owner_found to set/reset them. Modify the

[PATCH 10/21] Cygwin: FIFO: use a cygthread instead of a homemade thread

2020-05-07 Thread Ken Brown via Cygwin-patches
This will simplify future work. Rename the thread from "listen_client_thread" to "fifo_reader_thread" because it will be used for more than just listening. Remove the fixup_before stuff, which won't be needed after future changes to fixup_after_fork and fixup_after_exec. --- winsup/cygwin/fhandl

[PATCH 19/21] Cygwin: FIFO: allow any reader to take ownership

2020-05-07 Thread Ken Brown via Cygwin-patches
Add a take_ownership method, used by raw_read and select.cc:peek_fifo. It wakes up all fifo_reader_threads and allows the caller to become owner. The work is done by the fifo_reader_threads. For synchronization we introduce several new fhandler_fifo data members and methods: - update_needed_evt

[PATCH 13/21] Cygwin: FIFO: introduce a new type, fifo_reader_id_t

2020-05-07 Thread Ken Brown via Cygwin-patches
This uniquely identifies an fhandler_fifo open for reading in any process. Add a new data member 'me' of this type, which is set in open, dup, fork, and exec. --- winsup/cygwin/fhandler.h | 23 +++ winsup/cygwin/fhandler_fifo.cc | 9 - 2 files changed, 31 insert

[PATCH 09/21] Cygwin: FIFO: make opening a writer more robust

2020-05-07 Thread Ken Brown via Cygwin-patches
- Make read_ready a manual-reset event. - Signal read_ready in open instead of in the listen_client_thread. - Don't reset read_ready when the listen_client thread terminates; instead do it in close(). - Rearrange open and change its error handling. - Add a wait_open_pipe method that waits for

[PATCH 04/21] Cygwin: FIFO: simplify the listen_client_thread code

2020-05-07 Thread Ken Brown via Cygwin-patches
Always return 0; no one is doing anything with the return value anyway. Remove the return value from stop_listen_client. Make the connection event auto-reset, so that we don't have to reset it later. Simplify the process of connecting a bogus client when thread termination is signaled. Make som

[PATCH 01/21] Cygwin: FIFO: minor change - use NtClose

2020-05-07 Thread Ken Brown via Cygwin-patches
Replace CloseHandle by NtClose since all handles are created by NT functions. --- winsup/cygwin/fhandler_fifo.cc | 32 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/winsup/cygwin/fhandler_fifo.cc b/winsup/cygwin/fhandler_fifo.cc index 19cd0e507..c

[PATCH 20/21] Cygwin: FIFO: support opening multiple readers

2020-05-07 Thread Ken Brown via Cygwin-patches
Although we can have multiple readers open because of dup/fork/exec, the current code does not support multiple readers opening a FIFO by explicitly calling 'open'. The main complication in supporting this is that when a blocking reader tries to open and there's already one open, it has to check w

[PATCH 08/21] Cygwin: FIFO: fix hit_eof

2020-05-07 Thread Ken Brown via Cygwin-patches
According to Posix, a FIFO open for reading is at EOF if it is empty and there are no writers open. The only way to test this is to poll the fifo_client_handlers as in raw_read and select.cc:peek_fifo. The current hit_eof instead relies on the value of nconnected, which can be out of date. On th

[PATCH 00/21] FIFO: Support multiple readers

2020-05-07 Thread Ken Brown via Cygwin-patches
This project began as a an attempt to allow a FIFO to be opened multiple times for reading. The initial motivation was that Midnight Commander running under tcsh does this (unsuccessfully on Cygwin). See https://sourceware.org/pipermail/cygwin/2019-December/243317.html It quickly became appar

[PATCH 16/21] Cygwin: FIFO: add a shared fifo_client_handler list

2020-05-07 Thread Ken Brown via Cygwin-patches
This is in a new shared memory section. We will use it for temporary storage of the owner's fc_handler list when we need to change owner. The new owner can then duplicate the pipe handles from that list before taking ownership. Add several shared data members and methods that are needed for the d

[PATCH 12/21] Cygwin: FIFO: keep track of the number of readers

2020-05-07 Thread Ken Brown via Cygwin-patches
Add data and methods to the shared memory that keep track of the number of open readers. Increment this number in open, dup, fork, and exec. Decrement it in close. Reset read_ready if there are no readers left. --- winsup/cygwin/fhandler.h | 8 winsup/cygwin/fhandler_fifo.cc | 2

[PATCH 05/21] Cygwin: FIFO: remove the arm method

2020-05-07 Thread Ken Brown via Cygwin-patches
There's no reason to check for errors when we set read_ready or write_ready. We don't do that for other events. --- winsup/cygwin/fhandler.h | 1 - winsup/cygwin/fhandler_fifo.cc | 34 +++--- 2 files changed, 3 insertions(+), 32 deletions(-) diff --git a/winsup

[PATCH 02/21] Cygwin: FIFO: simplify the fifo_client_handler structure

2020-05-07 Thread Ken Brown via Cygwin-patches
Replace the 'fhandler_base *' member by a HANDLE to the server side of the Windows named pipe instance. Make the corresponding simplifications throughout. --- winsup/cygwin/fhandler.h | 19 +++--- winsup/cygwin/fhandler_fifo.cc | 65 -- 2 files changed, 1

[PATCH 06/21] Cygwin: FIFO: honor the flags argument in dup

2020-05-07 Thread Ken Brown via Cygwin-patches
Also improve the error handling. --- winsup/cygwin/fhandler_fifo.cc | 60 +++--- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/winsup/cygwin/fhandler_fifo.cc b/winsup/cygwin/fhandler_fifo.cc index 44919c19e..f61e2fe72 100644 --- a/winsup/cygwin/fhandle

[PATCH 07/21] Cygwin: FIFO: dup/fork/exec: make sure child starts unlocked

2020-05-07 Thread Ken Brown via Cygwin-patches
There can be deadlocks if the child starts with its fifo_client_lock in the locked state. --- winsup/cygwin/fhandler_fifo.cc | 31 +++ 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/winsup/cygwin/fhandler_fifo.cc b/winsup/cygwin/fhandler_fifo.cc index f6

[PATCH 03/21] Cygwin: FIFO: change the fifo_client_connect_state enum

2020-05-07 Thread Ken Brown via Cygwin-patches
Make the values correspond to the possible return values of fifo_client_handler::pipe_state(). When cleaning up the fc_handler list in listen_client_thread(), don't delete handlers in the fc_closing state. I think the pipe might still have input to be read in that case. Set the state to fc_closi

[PATCH 15/21] Cygwin: FIFO: allow fc_handler list to grow dynamically

2020-05-07 Thread Ken Brown via Cygwin-patches
Make fc_handler a pointer to malloc'd memory instead of a fixed-size array. The size is now a new data member 'shandlers'. Call realloc in add_client_handler if we need to grow the array. free fc_handler in close. As long as we're touching that code, also remove an unneeded lock. --- winsup/cy