bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSProcessInfo globallyUniqueString


From: Richard Frith-Macdonald
Subject: Re: NSProcessInfo globallyUniqueString
Date: Thu, 8 Aug 2002 10:18:38 +0100

On Thursday, August 8, 2002, at 09:12 AM, Roland Schwingel wrote:


Hi...

(I couldn't find David Ayers patch in my gnustep mailinglist mails so I decided to also post my solution - I only had the mail of Richard Frith-Macdonald
forwarded below)

I faced the very same problems a while ago also. I use
this method very frequently for temporary filenames etc.

So to be unique again (and to avoid the : problem - which
awfully breaks on windows) I placed a category to that
method doing this a long time ago:

{
        //        static time_t                 startTime = 0;
        static NSString        *hostName = nil;
        static long                        counter = 1;
        static pid_t                 pid = 0;
       
        if (!hostName)
        {
                hostName = [[NSProcessInfo processInfo] hostName];
               
                if (![hostName length])
                        hostName = @"localhost";
                else
                {
                        NSArray *components = [hostName componentsSeparatedByString:@"."];
                       
                        hostName = [[components objectAtIndex:0] retain];
                }
                       
                #ifdef __MINGW__
                pid = GetCurrentProcessId();
                #else
                pid = getpid();
                #endif
               
                // startTime = time(NULL);
        }
       
        //        return [NSString stringWithFormat:@"tmp-%@-%x-%x-%x",hostName,pid,time(NULL)-startTime,counter+
+];
        return [NSString stringWithFormat:@"tmp-%@-%x-%x",hostName,pid,counter++];
}

I decided that a timestamp is not really necessary when you have a counter which is incremented on every call, so I omitted that (but still available commented out). OS4.2 is also always adding a counter to the string.

This version now runs for over a year for me perfectly on windows and unix...

Thanks ... the counter increment is not thread-safe (though problems would be *EXTREMELY* rare), and using a single component of the host name also renders the string non-unique. However I like the idea of replacing '.' with '_' to make the result a legal filename under windoze
and I've updated the version in CVS to do this.

NB. The time stamp is necessary to ensure that the string is globally unique since the counter value is only unique (assuming locking is used to prevent two threads accessing it at once)
within a single process.

I guess you are only concerned with uniqueness within a single process ... but I think the intent of the method is to be truly, globally unique (ie across all processes and systems). I think the code now in cvs ensures this ... except for machines which are not in the public DNS system on the internet
and may therefore have non-unique host names.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]