From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.devel Subject: Re: Catch arbitrary signal and throw it later in C Date: Thu, 13 Apr 2023 15:18:19 -0700 Message-ID: References: <7EA1F972-ADC7-4362-9E75-60C481FA8B2D@gmail.com> <87edookx7s.fsf@yahoo.com> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.500.231\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5634"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Ruijie Yu , emacs-devel@gnu.org To: Po Lu Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Apr 14 00:19:42 2023 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pn5I4-0001GV-Bl for ged-emacs-devel@m.gmane-mx.org; Fri, 14 Apr 2023 00:19:40 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pn5H2-00043z-5v; Thu, 13 Apr 2023 18:18:36 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pn5H1-00043q-3h for emacs-devel@gnu.org; Thu, 13 Apr 2023 18:18:35 -0400 Original-Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pn5Gz-0005qN-Dn for emacs-devel@gnu.org; Thu, 13 Apr 2023 18:18:34 -0400 Original-Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1a66b9bd7dfso5650575ad.2 for ; Thu, 13 Apr 2023 15:18:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681424312; x=1684016312; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=MQW8hLlQ52O3SWts3fh6nB4EuVeTAghd/vM7i220D10=; b=bAJ9ATWes0VNVZwfz4LHs3jjNNlsUrlUWvuSBD1F9q989dBsRHkXpyxA1lJ6bDAWlD z8rytShSIYcd1Q/oAxRzJWDofsSRex+zoN6iPqAa4bAGkN1WG0wCVvmtT4rZTmhC88un E0ZSCC8rIrr1Uk9sgR3Y6C2daZwfJNzPd5OxJTPtBPjvw0A0pIyVAnrE+Gn8RqTUlpfa p09F/2xA56i634nRnJZL8n/7Qew7GJrmJ3AVygLKgw4upwh763PAMvLrxTdhpLYRxCpC cK/yGwwrGW/zklMJzrMyjVN7LJ5FfAvMzIucJnCo5dMAiLv4DhhmGCCIh1XmVNVc2YvX HHIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681424312; x=1684016312; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MQW8hLlQ52O3SWts3fh6nB4EuVeTAghd/vM7i220D10=; b=TAo7EmiZRNoluAePpnybyeVHji/IK5CQAaYKZ8IfXRyxtXRqZJJhBAFkkp8dVmQ30v 19biEsLbQ9mp4STbwvAM9lg7AXRb7KYixovl/fkadKi7+AEP2BVkf5P53dORxKEpc3Ie 7apV874DEE0Sc5rCA3Ic7V+kDUnH+2Ph8NEWNMLTwHqz5vqc8LkSKvAhZRzSt7mkGl0C t2GzWyLrPKvnxX2cT7qJ2Maz+NqZjML4pTnh9/Ooio8ILrHEBGR19c7jDUAj50CqAdtu iOuT83AJSPA75ahmefXz47CVV5vCC+FMkj2vRi/2e+Y0TKsY5bUDrukqLMFOCGNkZmZB hjCA== X-Gm-Message-State: AAQBX9f8JLJaioKJbzSQr7hGGXTVv8Q4debN+Vg47/5n3VNNWJsRtzGb zvPdaQ1TsZbP1TZElWoYuqk= X-Google-Smtp-Source: AKy350Y+lTi2Txfq/HHt8P1MZHBYOJx783RVF2BHsyvxHJVHmUdQz0o0Ub7aHa9v9te7BUBugHBfHQ== X-Received: by 2002:a05:6a00:2441:b0:63a:ea82:b7b7 with SMTP id d1-20020a056a00244100b0063aea82b7b7mr5002864pfj.28.1681424311747; Thu, 13 Apr 2023 15:18:31 -0700 (PDT) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id p4-20020a63c144000000b0050bf1d1cdc8sm1821493pgi.21.2023.04.13.15.18.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 13 Apr 2023 15:18:31 -0700 (PDT) In-Reply-To: <87edookx7s.fsf@yahoo.com> X-Mailer: Apple Mail (2.3731.500.231) Received-SPF: pass client-ip=2607:f8b0:4864:20::62d; envelope-from=casouri@gmail.com; helo=mail-pl1-x62d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:305297 Archived-At: > On Apr 13, 2023, at 1:22 AM, Po Lu wrote: >=20 > Yuan Fu writes: >=20 >> unwind-protect (and its internal C counterpart) pushes the cleanup >> form onto specpdl. Supposingly that form is later eval=E2=80=99ed? I = want to >> run some C code which obviously it doesn=E2=80=99t support. I need to >> understand how this whole unwinding thing work before I can maybe >> modify or write something that does what I want. Hopefully some one >> can enlighten me :-) >=20 > Typically, you do this: >=20 > specpdl_ref count; > struct localcontext context; >=20 > count =3D SPECPDL_INDEX (); > record_unwind_protect_ptr (cleanup_func, &context); >=20 > /* Code which may signal. */ >=20 > unbind_to (count, Qnil); >=20 > where cleanup_func is: >=20 > void > cleanup_func (void *arg) >=20 > Once a signal happens between the `record_unwind_protect_ptr', and > `unbind_to', `cleanup_func' will be called with `&context' as an > argument. This happens before any longjmp, so it is safe to use > references to variables with automatic storage duration within unwind > protects you record. >=20 > Note that `record_unwind_protect_ptr' itself may also signal if the > specpdl is full, calling the unwind function in the process. >=20 > If your cleanup function needs arguments that aren't pointers, you can > also use `record_unwind_protect_int', `record_unwind_protect', or > `record_unwind_protect_void'. Thanks, this is very clear and helpful. So the signal routine will = unwind the pdl and run my cleanup function, right? And if no signal = happens, my cleanup function is removed from the pdl by unbind_to. >=20 > Saving a signal, only to throw it again later, is not something we do = in > Emacs. I suggest you make your cleanup code work as described above. Right. What you=E2=80=99ve shown is actually exactly what I need. As = long as the cleanup runs and the signal is thrown, I=E2=80=99m good. Yuan=