From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by sourceware.org (Postfix) with ESMTPS id BA4D5386F430 for ; Tue, 21 Apr 2020 22:48:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org BA4D5386F430 Received: by mail-qt1-x833.google.com with SMTP id i68so162622qtb.5 for ; Tue, 21 Apr 2020 15:48:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=94bHMTD3gtaHkdxK/TxrF5rJ+J5A8LaPXVJiUX43Hdg=; b=emIKMSIpAk/4c/34q7IjhUrdVNhWp27FKpmn/VHTtmHpU7S31r+C89iJ9qZxBKnKtD y7a5jjZsWg4ZL/C3v1KTGvuHzqtSZFPEn61VKkbnhM6rxFPuK8NbBFsqMVwNK0pyN8v9 053bPtJe+PINPFoLL4fmpFtCHNNYgZq507bwoFG9fIg01pFtBSg+R4Frgj1Kn+LbHA5V gp5aMcoEomx1YIYxOvRYaU2xk1QoWKDLK1CJacV8yay2yA00QFj5F6A1pEi/7mTyW9I0 /lep7+7nEGxVIsR5YhyppCX9VjnFWmoIbWGJa0KgzPxQjoMK7CoCYoqszGbH2NzhJ8BE 3qrw== X-Gm-Message-State: AGi0PuZAxjqQzww564S6D7AQ6XXGasfw6Tvv26z+s53raB1wH91LIwuW u9Rh4NI+7XOixfXV8ylKtYIzy/wRIiyTmTRO3CA= X-Google-Smtp-Source: APiQypIlYMmCdtekJUhYZVCYb8OZmnZJwi1mEhzchL7gLuYDf1VEWYE0F8kY5m2Tpm50+kjFazBzsPjW+JFCvV2fySw= X-Received: by 2002:ac8:19dd:: with SMTP id s29mr23952122qtk.164.1587509319317; Tue, 21 Apr 2020 15:48:39 -0700 (PDT) MIME-Version: 1.0 References: <871rogpld6.fsf@mid.deneb.enyo.de> <87v9lso5d1.fsf@mid.deneb.enyo.de> In-Reply-To: <87v9lso5d1.fsf@mid.deneb.enyo.de> From: Richard Weinberger Date: Wed, 22 Apr 2020 00:48:28 +0200 Message-ID: Subject: Re: Clarification of pthread_cleanup_push() needed To: Florian Weimer Cc: Richard Weinberger via Libc-help , linux-man Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-help@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-help mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Apr 2020 22:48:43 -0000 On Wed, Apr 22, 2020 at 12:12 AM Florian Weimer wrote: > > * Richard Weinberger: > > > On Tue, Apr 21, 2020 at 11:41 PM Florian Weimer wrote: > >> > >> * Richard Weinberger via Libc-help: > >> > >> > But NPTL implements thread cancellation with signals, if I'm not > >> > completely mistaken the cleanup routine will run in signal context > >> > then. > >> > >> Deferred cancellation only occurs in signal context if the > >> cancellation is acted upon from within a signal handler. For the > >> signal handler case, whether cancellation handlers are restricted to > >> async-signal-safe function calls depends on the type of signal (some > >> are synchronous, not asynchronous) and what is interrupted by the > >> signal (for asynchronous signals). > >> > >> Asynchronous cancellation has even more constraints than asynchronous > >> signal safety, but it is rarely used. > > > > I should have noted that I'm using asynchronous cancellation. > > Which constraints are these? > > See pthread_setcanceltype(3): > > Functions that can be safely asynchronously canceled are called > async-cancel-safe functions. POSIX.1-2001 and POSIX.1-2008 require > only that pthread_cancel(3), pthread_setcancelstate(), and > pthread_setcanceltype() be async-cancel-safe. In general, other > library functions can't be safely called from an asynchronously > cancelable thread. > > The manual pages and the glibc manual also contain information about > AC-safety, but you cannot rely on them. They depend on implementation > details which may change within release branches. Well, this manpage does not talk much about the cleanup routine, except for "Consequently, clean-up handlers cease to be useful", which is clear as mud. I'm well aware that async cancel is not nice at all and my code can be interrupted at any time. Back to the cleanup routine, with asynchronous cancellation: - it must not use any function which might got interrupted by cancel, - it may only use async-signal-safe function, except long jumps. Is this correct? To give you some background, I'm porting an application to NPTL which comes from an operating system where cancelling threads and automatic cleanup is the way to go. Completely rewriting the application is not an option, so I need to figure how far I get with pthread_cancel() and friends. -- Thanks, //richard