Cygwin: FIFO: fix hit_eof

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 the one
hand, it doesn't take into account writers that were connected but
have since closed.  On the other hand, it doesn't take into account
writers that are in the process of opening but haven't yet connected.

Fix this by introducing a maybe_eof method that tentatively assumes
EOF if there are no connected writers after polling.  Then check for
writers currently opening (via a new 'writer_opening' event), and wait
for the fifo_reader_thread to record any new connection that was made
while we were polling.

To handle the needs of peek_fifo, replace the get_fc_handle method
by a get_fc_handler method, and add a fifo_client_handler::get_state
method.

Remove the is_connected method, which was used only in peek_fifo and
is no longer needed.

Remove the nconnected data member, which was used only for the flawed
hit_eof.

Add some comments about events to fhandler.h.
This commit is contained in:
Ken Brown
2020-04-26 09:38:46 -04:00
parent 13c65c43c2
commit 301454f132
3 changed files with 98 additions and 49 deletions

View File

@ -1296,19 +1296,26 @@ struct fifo_client_handler
/* Returns FILE_PIPE_DISCONNECTED_STATE, FILE_PIPE_LISTENING_STATE,
FILE_PIPE_CONNECTED_STATE, FILE_PIPE_CLOSING_STATE,
FILE_PIPE_INPUT_AVAILABLE_STATE, or -1 on error. */
fifo_client_connect_state &get_state () { return state; }
int pipe_state ();
};
class fhandler_fifo: public fhandler_base
{
HANDLE read_ready;
HANDLE write_ready;
/* Handles to named events shared by all fhandlers for a given FIFO. */
HANDLE read_ready; /* A reader is open; OK for a writer to open. */
HANDLE write_ready; /* A writer is open; OK for a reader to open. */
HANDLE writer_opening; /* A writer is opening; no EOF. */
/* Non-shared handles needed for the listen_client_thread. */
HANDLE listen_client_thr;
HANDLE lct_termination_evt;
UNICODE_STRING pipe_name;
WCHAR pipe_name_buf[CYGWIN_FIFO_PIPE_NAME_LEN + 1];
bool _maybe_eof;
fifo_client_handler fc_handler[MAX_CLIENTS];
int nhandlers, nconnected;
int nhandlers;
af_unix_spinlock_t _fifo_client_lock;
bool reader, writer, duplexer;
size_t max_atomic_write;
@ -1326,10 +1333,10 @@ class fhandler_fifo: public fhandler_base
public:
fhandler_fifo ();
bool hit_eof ();
bool maybe_eof () const { return _maybe_eof; }
void maybe_eof (bool val) { _maybe_eof = val; }
int get_nhandlers () const { return nhandlers; }
HANDLE get_fc_handle (int i) const { return fc_handler[i].h; }
bool is_connected (int i) const
{ return fc_handler[i].state == fc_connected; }
fifo_client_handler get_fc_handler (int i) const { return fc_handler[i]; }
PUNICODE_STRING get_pipe_name ();
DWORD listen_client_thread ();
void fifo_client_lock () { _fifo_client_lock.lock (); }