From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id C+QzLb1vYGrGHSQAWB0awg (envelope-from ) for ; Wed, 22 Jul 2026 03:22:37 -0400 Authentication-Results: simark.ca; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mail.ru header.i=@mail.ru header.a=rsa-sha256 header.s=mail4 header.b=PMoYwnml; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 9249F1E166; Wed, 22 Jul 2026 03:22:37 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.1 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 4E71E1E099 for ; Wed, 22 Jul 2026 03:22:34 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 941874BA2E0B for ; Wed, 22 Jul 2026 07:22:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 941874BA2E0B Authentication-Results: sourceware.org; dkim=fail reason="signature verification failed" (2048-bit key, unprotected) header.d=mail.ru header.i=@mail.ru header.a=rsa-sha256 header.s=mail4 header.b=PMoYwnml Received: from send104.i.mail.ru (send104.i.mail.ru [89.221.237.199]) by sourceware.org (Postfix) with ESMTPS id 445844BA2E0B for ; Wed, 22 Jul 2026 07:21:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 445844BA2E0B Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=mail.ru Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mail.ru ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 445844BA2E0B Authentication-Results: sourceware.org; arc=none smtp.remote-ip=89.221.237.199 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1784704918; cv=none; b=WgiNReeEE/nNjn49401DHzUIFjwK/AJpkF7TdMIPliyWt6fGGO/561bI5AwOLqN2gDtbYZUwDAzH+x058PWeLzduXzJ6+9TqrOZLYnjwu/NHP3GDZ9/Hmv6flombFcABepqobHiekJpJHboD+lpgO/dxCMSIM0HRKfqgnyHoGsU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1784704918; c=relaxed/simple; bh=B8w1nntvlwFou9hEUHiNwqztUxnXXIcZzNdYAvaCxao=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=xiewRMsKGi16Vr0FEsG11G0//pPeh99hRmE1phVK12hqEG8ZIEgmC0CpLzgKlL73QD0JaDkfmxGnSC0JCdIcUHW7751Su/zWc6oWeTF602zEdDtUYvQeONdbnKA5FLKugE7Sv+gvmI7fnlESpMX7Uu/0L+TWDGiUx7mokniytq8= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=mail.ru header.i=@mail.ru header.a=rsa-sha256 header.s=mail4 header.b=PMoYwnml DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 445844BA2E0B DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mail.ru; s=mail4; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:From:Sender:Reply-To:To:Cc:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive: X-Cloud-Ids:Disposition-Notification-To; bh=dwZNxjMXlwlxw05VAQx4+BXBBJeA/K2T8FQBt6K5vDA=; t=1784704918; x=1784794918; b=PMoYwnml7ed8CxKT5uJ8f1u2Cp0puFI67pEXUIs49utPq5EahTOBG5JD9Lf+RMdOXkQMr+/625u pitklDs/IgoEe6yB1XSQgUtwHMpiteKGlErgjf3L4cr9Ti2jjmlnby9JRXtd4Ar2M9HjE4E5K0tgV aIoDy64GWe2e4PaQt9g0yuTn/OxHBG8/Jpxp+p5RdEuHwE0aSeGF8y424aV5l/fOSfowhAcnRiOMm BdQ+IBnwk+R8Jfif2AeCHjbqtjlgW8cUnmXmMumb+3rx8r8vMq5pbOjzIdPob+XZ74XQxskKev8YN L+mFOPyUwz7hdhspPcG4+0sZpq+yiYVdNnzA==; Received: by exim-smtp-86556fd747-z2kwn with esmtpa (envelope-from ) id 1wmRH8-00000000J5c-3EF2; Wed, 22 Jul 2026 10:21:55 +0300 From: kurku To: gdb-patches@sourceware.org Cc: kurku Subject: [RFC][PATCH 1/2][PR cli/34409] gdb: event-loop: Rework poll's version of round robin Date: Wed, 22 Jul 2026 10:21:36 +0300 Message-ID: <20260722072137.1397888-2-sandovin@mail.ru> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260722072137.1397888-1-sandovin@mail.ru> References: <20260722072137.1397888-1-sandovin@mail.ru> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Authentication-Results: exim-smtp-86556fd747-z2kwn; auth=pass smtp.auth=sandovin@mail.ru smtp.mailfrom=sandovin@mail.ru X-Mailru-Src: smtp X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD93ED3C6BF6FFA701F3FCAD39C75259F61D8D076B0B3ED9EBE182A05F5380850402A9BBBD7731825AE3DE06ABAFEAF6705D1320CC63DFF1A9AE766CFF18C03850CA9248E35AFBCB78E X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE736691C7D10565E03C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6759CC434672EE6371C2A783ECEC0211ADC4224003CC836476D5A39DEEDB180909611E41BBFE2FEB2BA6BF4441E102E7DB325FB8F1580952A0BC97C0F54986FE6693DE02AA3DDB41F20C819B32DFABC24A725E5C173C3A84C3E478A468B35FE767117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCED943DBD20860CC2A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352026055571C92BF10FF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA73AA81AA40904B5D9A18204E546F3947C6089696B24BB1D199735652A29929C6C4AD6D5ED66289B523666184CF4C3C14F6136E347CC761E07725E5C173C3A84C39E5FF5549954B0F1BA3038C0950A5D36B5C8C57E37DE458B330BD67F2E7D9AF16D1867E19FE14079C09775C1D3CA48CF3D321E7403792E342EB15956EA79C166A417C69337E82CC275ECD9A6C639B01B78DA827A17800CE79643CE8115F4EDFC731C566533BA786AA5CC5B56E945C8DA X-C1DE0DAB: 0D63561A33F958A51337ED535A0256635002B1117B3ED6963102F01C45648FD1E99897350C7C491E823CB91A9FED034534781492E4B8EEADABF80F987DAEDACBC79554A2A72441328621D336A7BC284946AD531847A6065A535571D14F44ED41 X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE1918E10F71CB4DF9F9677DD89D51EBB774225B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D659D8EC3AC51E9B0DEF3E6CC3BEABC35011E785360F28155F462B58CE3155C541A10BE81F19637363E5B8341EE9D5BE9A0A7E7D602F6F4E08FC2B754F01ED50E44FAD611C3436E42E8C8CD93680B12512CF4C41F94D744909CEAE0DAB166F7E912C0B4C1C554206E9ACF0A6D2C91ED28CB6 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXMZebaIdHP2ghjoIc/363UZI6Kf1ptIMVWmxowtcrDwU9v3zxg5/wws= X-Mailru-Sender: DA7A906D9E8450B159FDEA386FB503F732C6B6DC0C0135F98E7FD94675F3356502ED3971842C99F2FD08481A78328DC6B4EC7484A32A15E91269F1ADE14071E628A0B4807B87142D548AED299D6609AA574ADF34F9FF3B2B5FEEDEB644C299C0ED14614B50AE0675 X-Mras: Ok X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org GDB had 2 implementations for round robin over file descriptors. version for poll version for select poll version uses: - std::vector index in gdb_notifier - linked list from gdb_notifier it does: - circular scan over std::vector to find a slot with revents filled in by the kernel - additional linear scan of the linked list to find the matched file_handler with the same fd select version uses: - linked list in gdb_notifier it does: - circular scan of the linked list to find the node which has events I can't find no clear reason to distinguish the two, or make the shape of the code to be so vastly different. In both cases we do circular linear scan over something and if flag of the fired event has been set - we stop the scan and handle the event. The only difference is the source of events. One comes directly from the linked list. The other is from poll_fds std::vector. Thus I propose to unify the shape of code paths. And unification would be made on select's version because it already does the tracking of events for select, we just need to extend it so that now revents issued by poll api are accessible through the linked list. ( the real use case to do this change is due to many readline macros hazards I was trying to fix: gdb/34409 ) --- gdbsupport/event-loop.cc | 72 ++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/gdbsupport/event-loop.cc b/gdbsupport/event-loop.cc index c6e1b5b4634..deca3d86e5d 100644 --- a/gdbsupport/event-loop.cc +++ b/gdbsupport/event-loop.cc @@ -77,6 +77,11 @@ struct file_handler /* Next registered file descriptor. */ struct file_handler *next_file; + +#ifdef HAVE_POLL + /* Reverse index from linked list into vector poll_fds vector */ + int poll_fds_index; +#endif }; #ifdef HAVE_POLL @@ -104,7 +109,7 @@ static struct /* Ptr to head of file handler list. */ file_handler *first_file_handler; - /* Next file handler to handle, for the select variant. To level + /* Next file handler to handle. To level the fairness across event sources, we serve file handlers in a round-robin-like fashion. The number and order of the polled file handlers may change between invocations, but this is good @@ -115,13 +120,6 @@ static struct /* Descriptors to poll. */ std::vector poll_fds; - /* Next file descriptor to handle, for the poll variant. To level - the fairness across event sources, we poll the file descriptors - in a round-robin-like fashion. The number and order of the - polled file descriptors may change between invocations, but - this is good enough. */ - int next_poll_fds_index; - /* Timeout in milliseconds for calls to poll(). */ int poll_timeout; #endif @@ -335,6 +333,7 @@ create_file_handler (int fd, int mask, handler_func * proc, #ifdef HAVE_POLL if (use_poll) { + file_ptr->poll_fds_index = gdb_notifier.num_fds; gdb_notifier.num_fds++; struct pollfd new_fd; new_fd.fd = fd; @@ -420,13 +419,30 @@ delete_file_handler (int fd) #ifdef HAVE_POLL if (use_poll) { - auto iter = std::remove_if (gdb_notifier.poll_fds.begin (), - gdb_notifier.poll_fds.end (), - [=] (const pollfd &item) - { - return item.fd == fd; - }); - gdb_notifier.poll_fds.erase (iter, gdb_notifier.poll_fds.end()); + /* Swap last with the provided file_ptr in poll_fds vector + then do repair of a reverse index */ + + int i = file_ptr->poll_fds_index; + int j = gdb_notifier.num_fds - 1; + + file_handler *i_ptr = file_ptr; + file_handler *j_ptr; + + for (j_ptr = gdb_notifier.first_file_handler; + j_ptr != NULL; + j_ptr = j_ptr->next_file) + { + if (j_ptr->poll_fds_index == j) + break; + } + + gdb_assert (i_ptr != NULL); + gdb_assert (j_ptr != NULL); + + std::swap(gdb_notifier.poll_fds[i], gdb_notifier.poll_fds[j]); + std::swap(j_ptr->poll_fds_index, i_ptr->poll_fds_index); + + gdb_notifier.poll_fds.pop_back(); gdb_notifier.num_fds--; } else @@ -645,30 +661,20 @@ gdb_wait_for_event (int block) #ifdef HAVE_POLL if (use_poll) { - int i; - int mask; + int mask = 0; - while (1) + do { - if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds) - gdb_notifier.next_poll_fds_index = 0; - i = gdb_notifier.next_poll_fds_index++; + file_ptr = get_next_file_handler_to_handle_and_advance (); - gdb_assert (i < gdb_notifier.num_fds); - if (gdb_notifier.poll_fds[i].revents) - break; - } + int i = file_ptr->poll_fds_index; + short revents = gdb_notifier.poll_fds[i].revents; - for (file_ptr = gdb_notifier.first_file_handler; - file_ptr != NULL; - file_ptr = file_ptr->next_file) - { - if (file_ptr->fd == gdb_notifier.poll_fds[i].fd) - break; + if (revents) + mask |= revents; } - gdb_assert (file_ptr != NULL); + while (mask == 0); - mask = gdb_notifier.poll_fds[i].revents; handle_file_event (file_ptr, mask); return 1; } -- 2.43.0