Thread: Defining "thread-safety" and maybe something that isn't "thread-safety"

  1. #31
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Quote Originally Posted by Angus View Post
    No kidding? Linux is the one I have in mind. How does a global like errno duplicate itself and make itself unique to each running thread? Globals are part of the data segment, right? And each thread uses the same data segment as every other thread in that process, right? So what am I missing?
    errno doesn't have to be a global, in the strict sense.

    Suppose:

    Code:
    extern int* my_errno_function(void);
    #define errno (*my_errno_function())
    When you call errno, it will expand to my_errno_function(), which returns a pointer to the current thread's errno variable. MS' CRT implements it this way; most POSIX platforms too.

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Another solution that I've seen in x86 is to use a segment register that holds a per-thread value, so each thread has it's own piece of memory for errno (and a few other bits and pieces, normally).

    A third option is that whilst errno is always in the same virtual memory, a different per-thread physical mapping allows errno to have different values.

    As stated, errno is almost never a direct global variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #33
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Ronix View Post
    When you call errno, it will expand to my_errno_function(), which returns a pointer to the current thread's errno variable. MS' CRT implements it this way; most POSIX platforms too.
    And this is why the old practice of manually declaring errno yourself:

    Code:
    extern int errno;
    Is not correct.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #34
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Just to say it, in general it's never correct to do this for any library function/variable/macro.

Popular pages Recent additions subscribe to a feed

Tags for this Thread