[newlib-cygwin/cygwin-3_6-branch] cygwin: pthread: Improve the performance of the destructor
Takashi Yano
tyan0@sourceware.org
Thu Nov 26 00:56:59 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=2dc93eb0a905d557067028e3d5113c9a8fa87be4
commit 2dc93eb0a905d557067028e3d5113c9a8fa87be4
Author: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Tue Nov 24 10:01:30 2026 +0900
cygwin: pthread: Improve the performance of the destructor
Deleting pthread-related objects, such as pthread_mutex, becomes very
slow when the number of the objects is large. Currently, these objects
are managed in a singly linked list, where deletion requires O(n) time.
As a result, deleting all objects takes O(n^2) time overall. Due to
this issue, freeing a decoder in libde265 can sometime take several
tens of seconds, because libde265 uses around 100,000 pthread_mutex
objects depending on the video being decoded.
This patch changes the list to a doubly linked, reducing the overall
deletion comlexity from O(n^2) to O(n).
Fixes: 5c83f260b87b ("(pthread_key_destructor_list::Remove): New function.")
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Reviewed-by: Mark Geisert <mark@maxrnd.com>
(cherry picked from commit d4a1bc15e3c03806cf94ff467b1819489ad8dfb2)
Diff:
---
winsup/cygwin/local_includes/thread.h | 44 ++++++++++++++++++++++-------------
winsup/cygwin/thread.cc | 14 +++++++----
2 files changed, 37 insertions(+), 21 deletions(-)
diff --git a/winsup/cygwin/local_includes/thread.h b/winsup/cygwin/local_includes/thread.h
index 025bfa2fc..dd6c02a70 100644
--- a/winsup/cygwin/local_includes/thread.h
+++ b/winsup/cygwin/local_includes/thread.h
@@ -129,7 +129,12 @@ List_insert (fast_mutex &mx, list_node *&head, list_node *node)
return;
mx.lock ();
do
- node->next = head;
+ {
+ node->next = head;
+ node->prev = NULL;
+ if (head)
+ head->prev = node;
+ }
while (InterlockedCompareExchangePointer ((PVOID volatile *) &head,
node, node->next) != node->next);
mx.unlock ();
@@ -141,7 +146,12 @@ List_insert_nolock (list_node *&head, list_node *node)
if (!node)
return;
do
- node->next = head;
+ {
+ node->next = head;
+ node->prev = NULL;
+ if (head)
+ head->prev = node;
+ }
while (InterlockedCompareExchangePointer ((PVOID volatile *) &head,
node, node->next) != node->next);
}
@@ -152,19 +162,12 @@ List_remove (fast_mutex &mx, list_node *&head, list_node *node)
if (!node)
return;
mx.lock ();
- if (head)
- {
- if (InterlockedCompareExchangePointer ((PVOID volatile *) &head,
- node->next, node) != node)
- {
- list_node *cur = head;
-
- while (cur->next && node != cur->next)
- cur = cur->next;
- if (node == cur->next)
- cur->next = cur->next->next;
- }
- }
+ if (InterlockedCompareExchangePointer ((PVOID volatile *) &head,
+ node->next, node) != node)
+ if (node->prev)
+ node->prev->next = node->next;
+ if (node->next)
+ node->next->prev = node->prev;
mx.unlock ();
}
@@ -334,6 +337,7 @@ public:
~pthread_mutex ();
class pthread_mutex *next;
+ class pthread_mutex *prev;
static void fixup_after_fork ()
{
mutexes.fixup_after_fork ();
@@ -453,6 +457,7 @@ public:
/* List support calls */
class pthread *next;
+ class pthread *prev;
static void fixup_after_fork ()
{
threads.fixup_after_fork ();
@@ -554,6 +559,7 @@ public:
~pthread_cond ();
class pthread_cond * next;
+ class pthread_cond * prev;
static void fixup_after_fork ()
{
conds.fixup_after_fork ();
@@ -627,9 +633,11 @@ public:
struct RWLOCK_READER
{
struct RWLOCK_READER *next;
+ struct RWLOCK_READER *prev;
pthread_t thread;
uint32_t n;
- RWLOCK_READER (): next (NULL), thread (pthread::self ()), n (0) {}
+ RWLOCK_READER ():
+ next (NULL), prev (NULL), thread (pthread::self ()), n (0) {}
} *readers;
fast_mutex readers_mx;
@@ -649,6 +657,7 @@ public:
~pthread_rwlock ();
class pthread_rwlock * next;
+ class pthread_rwlock * prev;
static void fixup_after_fork ()
{
rwlocks.fixup_after_fork ();
@@ -724,6 +733,7 @@ public:
~semaphore ();
class semaphore * next;
+ class semaphore * prev;
static void fixup_before_fork ()
{
semaphores.for_each (&semaphore::_fixup_before_fork);
@@ -755,8 +765,10 @@ private:
class callback
{
public:
+ callback (): next (NULL), prev (NULL) {}
void (*cb)(void);
class callback * next;
+ class callback * prev;
};
struct MTinterface
diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
index d410b9575..82879aa4b 100644
--- a/winsup/cygwin/thread.cc
+++ b/winsup/cygwin/thread.cc
@@ -412,7 +412,8 @@ List<pthread> pthread::threads;
pthread::pthread ():verifyable_object (PTHREAD_MAGIC), win32_obj_id (0),
valid (false), suspended (false), canceled (false),
cancelstate (0), canceltype (0), cancel_event (0),
- joiner (NULL), next (NULL), cleanup_stack (NULL)
+ joiner (NULL), next (NULL), prev (NULL),
+ cleanup_stack (NULL)
{
if (this != pthread_null::get_null_pthread ())
threads.insert (this);
@@ -1157,7 +1158,7 @@ pthread_cond::init_mutex ()
pthread_cond::pthread_cond (pthread_condattr *attr) :
verifyable_object (PTHREAD_COND_MAGIC),
shared (0), clock_id (CLOCK_REALTIME), waiting (0), pending (0),
- sem_wait (NULL), mtx_cond(NULL), next (NULL)
+ sem_wait (NULL), mtx_cond(NULL), next (NULL), prev (NULL)
{
pthread_mutex *verifyable_mutex_obj;
@@ -1373,7 +1374,7 @@ pthread_rwlock::pthread_rwlock (pthread_rwlockattr *attr) :
verifyable_object (PTHREAD_RWLOCK_MAGIC),
shared (0), waiting_readers (0), waiting_writers (0), writer (NULL),
readers (NULL), readers_mx (), mtx (NULL), cond_readers (NULL), cond_writers (NULL),
- next (NULL)
+ next (NULL), prev (NULL)
{
pthread_mutex *verifyable_mutex_obj = &mtx;
pthread_cond *verifyable_cond_obj;
@@ -1761,6 +1762,7 @@ pthread_mutex::init_mutex ()
pthread_mutex::pthread_mutex (pthread_mutexattr *attr) :
verifyable_object (0), /* set magic to zero initially */
+ next (NULL), prev (NULL),
lock_counter (0),
win32_obj_id (NULL), owner (_new_mutex),
#ifdef DEBUGGING
@@ -2519,7 +2521,8 @@ semaphore::semaphore (int pshared, unsigned int value)
startvalue (value),
fd (-1),
hash (0ULL),
- sem (NULL)
+ sem (NULL),
+ next (NULL), prev (NULL)
{
SECURITY_ATTRIBUTES sa = (pshared != PTHREAD_PROCESS_PRIVATE)
? sec_all : sec_none_nih;
@@ -2539,7 +2542,8 @@ semaphore::semaphore (unsigned long long shash, LUID sluid, int sfd,
fd (sfd),
hash (shash),
luid (sluid),
- sem (ssem)
+ sem (ssem),
+ next (NULL), prev (NULL)
{
char name[MAX_PATH];
More information about the Cygwin-cvs
mailing list