From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f678f,239b910cab54fd83,start X-Google-Attributes: gidf678f,public X-Google-ArrivalTime: 1994-11-17 16:33:42 PST Path: nntp.gmd.de!xlink.net!howland.reston.ans.net!news2.near.net!sunfish.hi.com!clam.hi.com!not-for-mail From: wright@clam.hi.com (David Wright) Newsgroups: comp.unix.osf.misc Subject: More on threads and errno Date: 17 Nov 1994 13:57:12 -0500 Organization: Hitachi Computer Products (America), Inc. Message-ID: <3ag928$47n@clam.hi.com> NNTP-Posting-Host: clam.hi.com My thanks to all who have responded to my questions about handing errno in a threaded environment. I figured I'd just respond to you all at one time, and add some extra information describing what we know and don't know so far, plus a bit more about the problem. Sorry for the length of this; I've tried to keep it as short as possible. This will be posted to comp.unix.osf.misc and to dce-talk. As a quick summary, here are the issues: 1) errno is a global variable, so it can't be shared properly by multiple threads. 2) new libraries and code can be written to use a per-thread errno, 3) but that doesn't allow mixing of new and old libraries (communicating errno values), particularly 3rd-party libraries for which we may not have the source, 4) and there is still the question of how to invoke non-thread-safe code in a multi-threaded environment. I don't propose to do a lot to address this last point. Unless some sort of standard locking emerges, about all you can do is acquire a single global lock before invoking a non-thread-safe routine. The pthread_lock_global_np() function might be useful here. The rest of this post will be devoted to considering what to do about errno. I realize that it's sometimes possible to use the global errno, and copy it into a per-thread storage location upon context switch. However, this is only practical if you are using user threads, not kernel threads. We're using kernel threads, and we're also using multiprocessors, so that solution won't work for us. An email respondent from Transarc told me that SunSoft had changed all their libraries to be thread-safe. Well, that's one possibility. But it doesn't address the problem of 3rd-party libraries. It's also a lot of work, and we are not a large shop here. lehotsky@max.tiac.net (Alan Lehotsky) wrote: Don't mix thread-safe and non-threadsafe code. You will need to rebuild libm and libX using appropriate header files so that you get the special definition of errno. I'd need to redo every single system library this way. Also, the code should presumably check whether it's multithreaded -- if it never has been, it should just use the global variable (much cheaper). And it still doesn't address how to use 3rd-party libraries. Trying to maintain locks around this data will be a thankless chore. Interestingly, the OSF/1 solution was to modify libc (creating libc_r) to do exactly this; for example, the stdio functions had locking introduced into them. And while you're at it, don't forget that there's a LOT of libc functions that aren't thread-safe. In fact, just about everything that returns a pointer is suspect [excluding the str functions...] Believe me, I *know* there are a lot of such functions. I've done considerable analysis of the code to see how many there are and what their problems are. Andrew Hastings of Transarc was (unfairly, IMO) drawn into a bit of a flamefest; his follow-up is what I'll address here. I've already dealt with his points on user threads; on kernel threads, he said: If you have kernel threads, your kernel should arrange to map errno separately for each thread. Maybe. You'd need to define errno as some special address, presumably its own page, and mark that page absent, generating a SIGSEGV when referencing it. The SIGSEGV handler could recognize the situation and access the per-thread errno. This would work but it's expensive. Not all OS's support this, either. Proposed solution: I'm presuming that in thread-safe code, references to errno are replaced (either explicitly or by macro) with references to a function that returns a pointer to the desired errno location. Usually, this is the per-thread errno. Now, let me tell you what we're proposing to do. We'll add a pair of wrapper functions, say ENTER() and EXIT() (not their real names), that are invoked as follows: ENTER(); non_thread_safe_function(); EXIT(); The ENTER() function will cause the issuing thread's errno-location function to reference the global errno, rather the per-thread errno. The EXIT() function reverses this behavior. Locking is still up to the application, and there are a few nuances, like ENTER and EXIT need to nest (i.e. if you issue ENTER(); ENTER(); ... ; EXIT(); EXIT(); the behavior doesn't revert until the second EXIT() function is complete. Also, you need to save the nesting level as part of setjmp processing and restore it on longjmp (a la signal masks). So, there's the idea. You may now take potshots at it. -- David Wright, Hitachi Computer Products (America), Inc. Waltham, MA wright@hi.com :: DCE Project Leader