From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 2E6A33858D1E for ; Mon, 10 Apr 2023 19:42:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2E6A33858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681155761; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=4vyjQ2EAmDKdCQgG+Djq0+/YYRQPBUcXV0kvU+8NAaE=; b=R8s7v5LGZzg8zUP9OjVjyZhDa5JBJ4tSd8kMcfUVcWha3RpwfdIjJNHRXgo3H4Co/AqXWG y49wrU8bJjB5aR/hSxKjINJUVo3vQhN5Ffcm2ThBvG+eJE6ccn5D/kFubwzbNunxgjWHKZ eK0M7wECyROUgFeihdxWbJlx+vcn3eE= Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-180-FmstiLPoPoertVvfyr7A6A-1; Mon, 10 Apr 2023 15:42:40 -0400 X-MC-Unique: FmstiLPoPoertVvfyr7A6A-1 Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-24677168b32so190089a91.0 for ; Mon, 10 Apr 2023 12:42:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681155759; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4vyjQ2EAmDKdCQgG+Djq0+/YYRQPBUcXV0kvU+8NAaE=; b=fvwXR+wh+OJDL/D6/Jx9LXGZSBG3jedoWwrmC2IBJ0HmzKG2QBcQE5Fig3alFg0gbj hM6vKzK6Oe556vNnbQ1geHlf8+9MEshi+0DPATWuJGv50COtfxUsrQPyPpJnwhBp0Wed TD1DNzh2MvuA3PryuZIScAfnelowK/fCZmCuIyQO17C5gGoSi1Hj569yHYrSF+Yw71Ol +lEbXvLrf7VVLYb61N28nwDoLlow1BmmRRxkSP4t4KguGho2gLTr1oC76WVe2Yuo1lPj e5kyqafmbNUC2f47ns4WJwml1lk/tQ5w3I+375LlXQFZrw2sen7yk1xtVCSGs+WEqlFq it8A== X-Gm-Message-State: AAQBX9e0s61cIB65Cpr67N7l2NWqowjUsnmrUXV17Pump7PGqASsZoVk nVmefbtngh/YNVtRTAZ+ko5mcy/QGz0jpKLX4aueqdtGlenbaC9XVRP3rQ7P+eXyj5Ge1lymzQi U9IWT08oI+PDmVyIcRlOHC3YbdFcKbmM= X-Received: by 2002:a05:6a00:140e:b0:625:a545:3292 with SMTP id l14-20020a056a00140e00b00625a5453292mr5335376pfu.0.1681155759026; Mon, 10 Apr 2023 12:42:39 -0700 (PDT) X-Google-Smtp-Source: AKy350b3YsTRIgImS4e+efAFFM/Tu3YSJBkNJtjADtHeaPD2K8ynPgByQjBnZ8D+ptmc2HGDGfRPTAtw+3k4PniZ5+o= X-Received: by 2002:a05:6a00:140e:b0:625:a545:3292 with SMTP id l14-20020a056a00140e00b00625a5453292mr5335369pfu.0.1681155758766; Mon, 10 Apr 2023 12:42:38 -0700 (PDT) MIME-Version: 1.0 References: <20210323163937.GA3008@redhat.com> In-Reply-To: From: Thomas Rodgers Date: Mon, 10 Apr 2023 12:42:28 -0700 Message-ID: Subject: Re: [PATCH] libstdc++: Implementation of class strand To: Jonathan Wakely Cc: "Alessio G. B." , libstdc++@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/alternative; boundary="000000000000d0529b05f9009353" X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000d0529b05f9009353 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable IIRC, the Asio implementation checks to see if the current thread already holds a lock, and if so, adopts the current lock and enqueues any child tasks that are created and processes them after the user-provided function returns, to avoid deadlock in the case that Jonathan outlines here. On Wed, Mar 22, 2023 at 7:32=E2=80=AFAM Jonathan Wakely via Libstdc++ < libstdc++@gcc.gnu.org> wrote: > On Tue, 23 Mar 2021 at 16:39, Jonathan Wakely wrote: > > > On 12/03/21 13:21 +0100, Alessio G. B. via Libstdc++ wrote: > > >I expanded the implementation of the class strand of the Networking > > >TS. Essentially, I > > >implemented a token system so each thread knows when it can execute; > > >the system is organized > > >with 2 integers moving as a clock. > > > > Thanks for this patch. I'm not sure when I'll have time to review it, > > and it might not be in time for the upcoming GCC 11 release. But the > > patch has been received and will get reviewed, thanks. > > > > Well I didn't think it would take me two years, sorry about that :-( > > + template > + void > + invoke(unsigned int token, _Func&& __f) > + { > + std::unique_lock __lock(_M_mutex); > + > + _M_cv.wait(__lock, > + [token, next_token =3D _M_next_token]() > + { return token =3D=3D next_token; }); > + > + try { decay_t<_Func>{std::forward<_Func>(__f)}(); } > + catch(...) { } > + > + _M_next_token++; > + > + __lock.unlock(); > + > + _M_cv.notify_all(); > + } > > It looks like this will run a user-provided function while holding the > mutex lock. Won't that deadlock if the task added to the strand adds > another task to the same strand? Is that forbidden by some requirement in > the TS that I've forgotten? > > --000000000000d0529b05f9009353--