From: Aleksandr Malikov <schn27@gmail.com>
To: cygwin-patches@cygwin.com
Subject: [PATCH] fix race condition in List_insert
Date: Mon, 23 Aug 2021 17:27:48 +0300 [thread overview]
Message-ID: <20210823142748.1012-1-schn27@gmail.com> (raw)
From: Aleksand Malikov <schn27@gmail.com>
Revert mx parameter and mutex lock while operating the list.
Mutex was removed with 94d24160 informing that:
'Use InterlockedCompareExchangePointer to ensure race safeness
without using a mutex.'
But it does not.
Calling pthread_mutex_init and pthread_mutex_destroy from two or
more threads occasionally leads to hang in pthread_mutex_destroy.
To not change the behaviour of other cases where List_insert was called,
List_insert_nolock is added.
---
winsup/cygwin/thread.cc | 8 ++++----
winsup/cygwin/thread.h | 17 +++++++++++++++--
2 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
index 4d0ea27..7c6a919 100644
--- a/winsup/cygwin/thread.cc
+++ b/winsup/cygwin/thread.cc
@@ -1595,7 +1595,7 @@ pthread_rwlock::add_reader ()
{
RWLOCK_READER *rd = new RWLOCK_READER;
if (rd)
- List_insert (readers, rd);
+ List_insert_nolock (readers, rd);
return rd;
}
@@ -2165,7 +2165,7 @@ pthread::atfork (void (*prepare)(void), void (*parent)(void), void (*child)(void
if (prepcb)
{
prepcb->cb = prepare;
- List_insert (MT_INTERFACE->pthread_prepare, prepcb);
+ List_insert_nolock (MT_INTERFACE->pthread_prepare, prepcb);
}
if (parentcb)
{
@@ -2174,7 +2174,7 @@ pthread::atfork (void (*prepare)(void), void (*parent)(void), void (*child)(void
while (*t)
t = &(*t)->next;
/* t = pointer to last next in the list */
- List_insert (*t, parentcb);
+ List_insert_nolock (*t, parentcb);
}
if (childcb)
{
@@ -2183,7 +2183,7 @@ pthread::atfork (void (*prepare)(void), void (*parent)(void), void (*child)(void
while (*t)
t = &(*t)->next;
/* t = pointer to last next in the list */
- List_insert (*t, childcb);
+ List_insert_nolock (*t, childcb);
}
return 0;
}
diff --git a/winsup/cygwin/thread.h b/winsup/cygwin/thread.h
index 6b699cc..ddb2d7d 100644
--- a/winsup/cygwin/thread.h
+++ b/winsup/cygwin/thread.h
@@ -111,7 +111,20 @@ typedef enum
} verifyable_object_state;
template <class list_node> inline void
-List_insert (list_node *&head, list_node *node)
+List_insert (fast_mutex &mx, list_node *&head, list_node *node)
+{
+ if (!node)
+ return;
+ mx.lock ();
+ do
+ node->next = head;
+ while (InterlockedCompareExchangePointer ((PVOID volatile *) &head,
+ node, node->next) != node->next);
+ mx.unlock ();
+}
+
+template <class list_node> inline void
+List_insert_nolock (list_node *&head, list_node *node)
{
if (!node)
return;
@@ -163,7 +176,7 @@ template <class list_node> class List
void insert (list_node *node)
{
- List_insert (head, node);
+ List_insert (mx, head, node);
}
void remove (list_node *node)
--
2.32.0.windows.2
next reply other threads:[~2021-08-23 14:28 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-23 14:27 Aleksandr Malikov [this message]
2021-08-23 15:02 ` Corinna Vinschen
2021-08-23 15:48 ` Александр Маликов
2021-08-23 18:08 ` Corinna Vinschen
2021-08-23 19:05 ` Александр Маликов
2021-08-24 7:57 ` Corinna Vinschen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210823142748.1012-1-schn27@gmail.com \
--to=schn27@gmail.com \
--cc=cygwin-patches@cygwin.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).