From owner-freebsd-threads@FreeBSD.ORG Thu Oct 16 01:44:40 2008 Return-Path: Delivered-To: threads@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9B398106568C; Thu, 16 Oct 2008 01:44:40 +0000 (UTC) (envelope-from davidxu@freebsd.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 861918FC17; Thu, 16 Oct 2008 01:44:40 +0000 (UTC) (envelope-from davidxu@freebsd.org) Received: from apple.my.domain (root@localhost [127.0.0.1]) by freefall.freebsd.org (8.14.3/8.14.3) with ESMTP id m9G1icjh041026; Thu, 16 Oct 2008 01:44:39 GMT (envelope-from davidxu@freebsd.org) Message-ID: <48F69CF4.9050905@freebsd.org> Date: Thu, 16 Oct 2008 09:46:28 +0800 From: David Xu User-Agent: Thunderbird 2.0.0.9 (X11/20080612) MIME-Version: 1.0 To: Daniel Eischen References: <18668.10465.699531.162573@gromit.timing.com> <20081008045447.GY36572@elvis.mu.org> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: threads@freebsd.org, John Hein Subject: Re: pthread_cleanup_push & pthread_cleanup_pop usage X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Oct 2008 01:44:40 -0000 Daniel Eischen wrote: > On Tue, 7 Oct 2008, Alfred Perlstein wrote: > >> * John Hein [081007 21:45] wrote: >>> In June pthread_cleanup_push & pthread_cleanup_pop were changed to >>> macros >>> that look like so... >> >> Hey John, I found the same problem when working on QNX a while back, >> however that is really how it's supposed to be set up. >> >> I would suggest the following construct to fix the problem, >> make your own per-thread stack of destructors that are callable >> as functions and not macros. >> >> It's not too hard to do. >> >> Just use a pthread_key and pthread_once thingy to write a library >> to do it, shouldn't take more than a hundred lines of code. >> >> FWIW, OS X and QNX have the same set of macros, not sure about >> other OSes. > > Solaris as well. > > Just conditionally undef them before you use them. > > #ifdef pthread_cleanup_push > #undef pthread_cleanup_push > #endif > #ifdef pthread_cleanup_pop > #undef pthread_cleanup_pop > #endif > > The library versions are still there (they have to be in order > to be callable from non-C/C++ languages). > One of possible solutions is we define a C++ class in pthread.h: #ifdef __cplusplus class __pthread_cleanup_obj { void (*__f)(void *); void *__a; int __execeute; public: __pthread_cleanup_obj(void (*__cleanup_routine)(void *), void *__arg) { f = __cleanup_routine; a = __arg; __execute = 0; } ~__pthread_cleanup_obj() { if (__execute) __f(__a); } void __set_execute(int __e) { __execute = __e; } }; #define pthread_cleanup_push(f, a) { \ __pthread_cleanup_obj __cleanup(f, a); \ { #define pthread_cleanup_pop(e) \ __cleanup.__set_execute(e); \ } \ } #endif but because there is no specification for C++ and threading, it is unknown which behavior is desired. David Xu