From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2210) id 9E39A385ED4B; Tue, 4 Aug 2020 15:29:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9E39A385ED4B Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Ken Brown To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: FIFO: lock fixes X-Act-Checkin: newlib-cygwin X-Git-Author: Ken Brown X-Git-Refname: refs/heads/master X-Git-Oldrev: cb7fba2f3e30e2b62092000bfe0ea34c4a887be0 X-Git-Newrev: e319fd0e627292f2083d7c8365186d175a7578fd Message-Id: <20200804152916.9E39A385ED4B@sourceware.org> Date: Tue, 4 Aug 2020 15:29:16 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 04 Aug 2020 15:29:16 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=e319fd0e627292f2083d7c8365186d175a7578fd commit e319fd0e627292f2083d7c8365186d175a7578fd Author: Ken Brown Date: Fri Jul 31 13:55:17 2020 -0400 Cygwin: FIFO: lock fixes Add some missing locks and remove one extra unlock. Clarify for some functions whether caller or callee acquires lock, and add appropriate comments. Diff: --- winsup/cygwin/fhandler_fifo.cc | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/winsup/cygwin/fhandler_fifo.cc b/winsup/cygwin/fhandler_fifo.cc index e9d0187d4..ee7f47c0c 100644 --- a/winsup/cygwin/fhandler_fifo.cc +++ b/winsup/cygwin/fhandler_fifo.cc @@ -316,6 +316,7 @@ fhandler_fifo::wait_open_pipe (HANDLE& ph) return status; } +/* Always called with fifo_client_lock in place. */ int fhandler_fifo::add_client_handler (bool new_pipe_instance) { @@ -345,6 +346,7 @@ fhandler_fifo::add_client_handler (bool new_pipe_instance) return 0; } +/* Always called with fifo_client_lock in place. */ void fhandler_fifo::delete_client_handler (int i) { @@ -354,7 +356,8 @@ fhandler_fifo::delete_client_handler (int i) (nhandlers - i) * sizeof (fc_handler[i])); } -/* Delete handlers that we will never read from. */ +/* Delete handlers that we will never read from. Always called with + fifo_client_lock in place. */ void fhandler_fifo::cleanup_handlers () { @@ -369,6 +372,7 @@ fhandler_fifo::cleanup_handlers () } } +/* Always called with fifo_client_lock in place. */ void fhandler_fifo::record_connection (fifo_client_handler& fc, fifo_client_connect_state s) @@ -398,6 +402,7 @@ fhandler_fifo::update_my_handlers () if (!prev_proc) api_fatal ("Can't open process of previous owner, %E"); + fifo_client_lock (); for (int i = 0; i < get_shared_nhandlers (); i++) { if (add_client_handler (false) < 0) @@ -419,10 +424,12 @@ fhandler_fifo::update_my_handlers () fc.last_read = shared_fc_handler[i].last_read; } } + fifo_client_unlock (); set_prev_owner (null_fr_id); return ret; } +/* Always called with fifo_client_lock and owner_lock in place. */ int fhandler_fifo::update_shared_handlers () { @@ -435,9 +442,7 @@ fhandler_fifo::update_shared_handlers () set_shared_nhandlers (nhandlers); memcpy (shared_fc_handler, fc_handler, nhandlers * sizeof (fc_handler[0])); shared_fc_handler_updated (true); - owner_lock (); set_prev_owner (me); - owner_unlock (); return 0; } @@ -509,7 +514,6 @@ fhandler_fifo::fifo_reader_thread_func () if (update_my_handlers () < 0) debug_printf ("error updating my handlers, %E"); owner_found (); - owner_unlock (); /* Fall through to owner_listen. */ } } @@ -602,8 +606,13 @@ owner_listen: } if (ph) NtClose (ph); - if (update && update_shared_handlers () < 0) - api_fatal ("Can't update shared handlers, %E"); + if (update) + { + owner_lock (); + if (get_owner () == me && update_shared_handlers () < 0) + api_fatal ("Can't update shared handlers, %E"); + owner_unlock (); + } fifo_client_unlock (); if (cancel) goto canceled; @@ -1402,9 +1411,11 @@ fhandler_fifo::fstatvfs (struct statvfs *sfs) void fhandler_fifo::close_all_handlers () { + fifo_client_lock (); for (int i = 0; i < nhandlers; i++) fc_handler[i].close (); nhandlers = 0; + fifo_client_unlock (); } fifo_client_connect_state