[6/7] ntdll: Try to merge threadpool wait queue buckets if possible.

Sebastian Lackner sebastian at fds-team.de
Sat Jul 4 19:25:32 CDT 2015


When the number of elements per bucket is too small, then try to reduce
the number of threads by merging buckets. This is to ensure that the
number of running wait queue threads doesn't get too big.

---
 dlls/ntdll/threadpool.c |   40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/dlls/ntdll/threadpool.c b/dlls/ntdll/threadpool.c
index 1931276..a15558c 100644
--- a/dlls/ntdll/threadpool.c
+++ b/dlls/ntdll/threadpool.c
@@ -1528,6 +1528,46 @@ static void CALLBACK waitqueue_thread_proc( void *param )
                 tp_object_release( wait );
             }
         }
+
+        /* Try to merge bucket with other threads. */
+        if (waitqueue.num_buckets > 1 && bucket->objcount &&
+            bucket->objcount <= MAXIMUM_WAITQUEUE_OBJECTS * 1 / 3)
+        {
+            struct waitqueue_bucket *other_bucket;
+            LIST_FOR_EACH_ENTRY( other_bucket, &waitqueue.buckets, struct waitqueue_bucket, bucket_entry )
+            {
+                if (other_bucket != bucket && other_bucket->objcount &&
+                    other_bucket->objcount + bucket->objcount <= MAXIMUM_WAITQUEUE_OBJECTS * 2 / 3)
+                {
+                    other_bucket->objcount += bucket->objcount;
+                    bucket->objcount = 0;
+
+                    /* Update reserved list. */
+                    LIST_FOR_EACH_ENTRY( wait, &bucket->reserved, struct threadpool_object, u.wait.wait_entry )
+                    {
+                        assert( wait->type == TP_OBJECT_TYPE_WAIT );
+                        wait->u.wait.bucket = other_bucket;
+                    }
+                    list_move_tail( &other_bucket->reserved, &bucket->reserved );
+
+                    /* Update waiting list. */
+                    LIST_FOR_EACH_ENTRY( wait, &bucket->waiting, struct threadpool_object, u.wait.wait_entry )
+                    {
+                        assert( wait->type == TP_OBJECT_TYPE_WAIT );
+                        wait->u.wait.bucket = other_bucket;
+                    }
+                    list_move_tail( &other_bucket->waiting, &bucket->waiting );
+
+                    /* Move bucket to the end, to keep the probability of
+                     * newly added wait objects as small as possible. */
+                    list_remove( &bucket->bucket_entry );
+                    list_add_tail( &waitqueue.buckets, &bucket->bucket_entry );
+
+                    NtSetEvent( other_bucket->update_event, NULL );
+                    break;
+                }
+            }
+        }
     }
 
     /* Remove this bucket from the list. */
-- 
2.4.4



More information about the wine-patches mailing list