From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26081 invoked by alias); 25 Apr 2008 05:01:20 -0000 Received: (qmail 26062 invoked by uid 22791); 25 Apr 2008 05:01:18 -0000 X-Spam-Check-By: sourceware.org Received: from bender.grapevine.net.au (HELO bender.grapevine.net.au) (203.129.32.139) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 25 Apr 2008 05:00:58 +0000 Received: from localhost (localhost [127.0.0.1]) by bender.grapevine.net.au (Postfix) with ESMTP id 2C3C0C02BA; Fri, 25 Apr 2008 15:00:50 +1000 (EST) Received: from bender.grapevine.net.au ([127.0.0.1]) by localhost (bender.grapevine.net.au [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id S-LwUDT4f04S; Fri, 25 Apr 2008 15:00:50 +1000 (EST) Received: from localhost.localdomain (unknown [121.127.213.230]) by bender.grapevine.net.au (Postfix) with ESMTP id 43DE1C02B8; Fri, 25 Apr 2008 15:00:49 +1000 (EST) Message-ID: <4811657E.40608@homemail.com.au> Date: Fri, 25 Apr 2008 05:01:00 -0000 From: Ross Johnson User-Agent: Thunderbird 2.0.0.12 (X11/20080226) MIME-Version: 1.0 To: Ramiro Polla , Pthreads-Win32 list Subject: Re: Static libraries initialization References: <481082AE.3030904@lisha.ufsc.br> <4810ABB9.10106@homemail.com.au> <4810D5C3.2070306@lisha.ufsc.br> In-Reply-To: <4810D5C3.2070306@lisha.ufsc.br> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact pthreads-win32-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sourceware.org X-SW-Source: 2008/txt/msg00020.txt.bz2 Hi Ramiro, Re not replying to the list - sorry, I didn't notice that I'd hit the wrong button. Ramiro Polla wrote: > Hello Ross, > > Ross Johnson wrote: >> Hi Ramiro, >> >> I agree that requiring the attach/detach routines is clunky, >> however...see my comments inline. >> >> Ramiro Polla wrote: >>> pthreads-win32 requires programs that use the library statically to >>> call some (de)initialization code, which would be the same code >>> called by DllMain(). There are plenty of messages regarding this on >>> the mailinglist. >>> >>> 1- Why isn't this initialization done by pthreads-win32 itself? All >>> pthread_* functions could have an if(!initialized) {...} block >>> before >>> running any code that depends on it. >> It could be done this way for initialisation, but you'd still need to >> run the process detach routine explicitly before exiting > > Can't this be done with atexit()? I wasn't aware of that function, or had forgotten that it exits. I don't do a lot of C programming. Now there is only the thread detach cleanup to deal with. I haven't found an equivalent to atexit() for win32 native threads. Is there one, or is there another way to make this automatic? The problem is as follows:- Threads created via pthread_create() are cleaned up automatically, even when the library is statically linked (something I keep forgetting was added). However, one feature of pthreads-win32 is that it allows win32 native threads to call pthreads routines, which means fully symmetric interaction is possible between win32 and POSIX threads. I don't want this to be configurable or optional or dependent upon using the dll. To make this feature possible, a [detached] POSIX thread id is automatically created for the win32 thread the first time it's needed and is retained for the life of the thread. When statically linked, win32 threads must call pthread_win32_thread_detach_np() explicitly before exiting in order to cleanup any "implicit" POSIX handles or other POSIX resources such as TSD etc. >> and I may have thought a long time ago that if you need one you may >> as well have the other, if only to make it more obvious and "in your >> face". > >> It also allows the developer to choose when initialisation occurs >> and, although most may not care, some might. > > It could be configurable. Ignore that issue, I was just brainstorming reasons to maintain the status quo. > >> For some routines, the additional overhead is not negligible, and >> they tend to be the most heavily used routines. > > The overhead of one if()? If the branch prediction sets the if() to be > unlikely, there should be 1 memory access and 1 cmp function overhead. > It shouldn't even disturb the instruction pipeline. Yes, it's almost negligible and I use the same method to initialise static POSIX mutexes, cond vars, etc., although this one is slightly worse in that the data isn't necessarily in cache already or local. IIRC, we need to use an Interlocked routine to compare the value, not for atomicity necessarily but so that we get the memory barrier effects right. And I've found that these calls can be relatively slow due to bus locking. I'm also still assuming that every single routine needs this overhead, but that may not be necessary in the end. Nevertheless, if you want to make this overhead configurable as you've described below that's ok with me. > >> There are a couple of other impediments but nothing too major - they >> just all add up. One was the thread detach already mentioned earlier and another, a function defined as a macro, I no longer consider a problem because it need not trigger a process attach. > >>> 2- Static pthreads-win32 libraries should at least check if they were >>> properly initialized before allowing pthread_* functions to run. >>> Returning an error is far better than having the program crash. >> Ideally yes, I agree, but presumably the crash occurs pretty soon in >> development and once fixed is fixed forever. So, in the interests of >> keeping overheads within the library to a minimum I think it's >> reasonable not to check. This is one of those APIs that absolutely >> needs to be as efficient as possible. >> >> One last thing: the library was originally intended to be used only >> as a dll, and static linking requirements have been given only >> minimal attention. > > Would it be ok to add another build option such as GC-static-autoinit? > Users should expect it to have a small overhead, at the price of not > needing to alter their code nor the build system to work with static > or shared libraries. If you're offering to provide the patches I'll certainly try to include them. Perhaps you could also add an option to build the benchmarks in the tests folder using static linking and perhaps also compare with the dll versions. > > Oh, and apparently you only replied to me. Could this be discussed on > the mailinglist? Oops. Regards. Ross > > Ramiro Polla