From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-2.6 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id E54111F4B4 for ; Thu, 14 Jan 2021 13:06:47 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 8209F120B60; Thu, 14 Jan 2021 22:05:53 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 2042C120AF4 for ; Thu, 14 Jan 2021 22:05:50 +0900 (JST) Received: by filterdrecv-p3mdw1-5fdf7d7776-glmrl with SMTP id filterdrecv-p3mdw1-5fdf7d7776-glmrl-16-600041DE-B 2021-01-14 13:06:38.104662972 +0000 UTC m=+151671.125321224 Received: from herokuapp.com (unknown) by ismtpd0088p1mdw1.sendgrid.net (SG) with ESMTP id sljK2XwoRcuvnM3MkcM7uQ for ; Thu, 14 Jan 2021 13:06:38.022 +0000 (UTC) Date: Thu, 14 Jan 2021 13:06:38 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 77994 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 12607 X-Redmine-Issue-Author: shyouhei X-Redmine-Issue-Assignee: ko1 X-Redmine-Sender: Eregon X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr1E7HwixNdpk3EgJoNFgG?= =?us-ascii?Q?f1E9WPxRmhO4vDVk2jQaNCZlMWThrUkumTU8OVo?= =?us-ascii?Q?qCzxMUpeQxOBggX5D0aZrEt8tWii=2FESFgygdkGC?= =?us-ascii?Q?lvMHAQtQIABgHP6AWkElR1kIauctXJijBvmh=2FG5?= =?us-ascii?Q?0g0fKdOT3liProCl9eMHfiD6uSlV61Z=2FEY4V4u8?= =?us-ascii?Q?Robfc6haBLyHbF+l4=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 102091 Subject: [ruby-core:102091] [Ruby master Feature#12607] Ruby needs an atomic integer X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #12607 has been updated by Eregon (Benoit Daloze). There is also the well-known example of metrics in Sidekiq, and all these: https://github.com/search?l=Ruby&q=AtomicFixnum&type=Code > I really think a generator (like the above MyCounter.next) is a more appropriate tool than an atomic integer. And how do you implement that efficiently, and in a way that's thread-safe? To be clear, Enumerator.new {} is not efficient, so a generator is not good enough, and Fibers can't be resumed across threads. ko1 (Koichi Sasada) wrote in #note-26: > TVar proposed in https://bugs.ruby-lang.org/issues/17261 has `#increment` method and it is enough fast. I think that cannot be as efficient as an atomic integer. Reasoning: TVar#increment needs to also be atomic with other changes, including `Thread.atomically { tv.value = tv.value * 2 }`, for decent STM semantics. That implies extra tracking for TVar#increment besides just a single fetch-and-add, isn't it? For instance, it would be incorrect to execute that `atomically` block in parallel with the fetch-and-add (might result in the increment being lost). So TVar#increment needs to sync somehow with `Thread.atomically` and that's the overhead. ---------------------------------------- Feature #12607: Ruby needs an atomic integer https://bugs.ruby-lang.org/issues/12607#change-89948 * Author: shyouhei (Shyouhei Urabe) * Status: Feedback * Priority: Normal * Assignee: ko1 (Koichi Sasada) ---------------------------------------- (This one was derived from bug #12463) Although I don't think += would become atomic, at the same time I understand Rodrigo's needs of _easier_ counter variable that resists inter-thread tampering. I don't think ruby's Integer class can be used for that purpose for reasons (mainly because it is not designed with threads in mind). Rather we should introduce a integer class which is carefully designed. Why not import Concurrent::AtomicFixnum into core? -- https://bugs.ruby-lang.org/