Improve thread ID and name code to match OSX. Add some initial getaddrinfo code

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@38442 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2015-03-31 09:14:01 +00:00
parent ef42c2f44c
commit 33688ccb99
8 changed files with 419 additions and 41 deletions

View file

@ -552,5 +552,17 @@ uint32_t
GSPrivateFinishHash(uint32_t s0, uint32_t s1, uint32_t totalLength)
GS_ATTRIB_PRIVATE;
/* Return the current thread ID as an unsigned long.
* Ideally, we use the operating-system's notion of a thread ID so
* that external process monitoring software will be using the same
* value that we log. If we don't know the system's mechanism, we
* use the address of the current NSThread object so that, even if
* it makes no sense externally, it can still be used to show that
* different threads generated different logs.
*/
unsigned long
GSPrivateThreadID()
GS_ATTRIB_PRIVATE;
#endif /* _GSPrivate_h_ */

View file

@ -63,7 +63,10 @@ static id null = nil;
@interface NSHost (Private)
- (void) _addName: (NSString*)name;
- (id) _initWithHostEntry: (struct hostent*)entry key: (NSString*)key;
#if defined(HAVE_GETADDRINFO)
- (id) _initWithAddrinfo: (struct addrinfo*)entry key: (NSString*)name;
#endif
- (id) _initWithHostEntry: (struct hostent*)entry key: (NSString*)name;
+ (NSMutableSet*) _localAddresses;
@end
@ -101,6 +104,125 @@ static id null = nil;
return self;
}
#if defined(HAVE_GETADDRINFO)
- (id) _initWithAddrinfo: (struct addrinfo*)entry key: (NSString*)name
{
NSMutableSet *names;
NSMutableSet *addresses;
NSMutableSet *extra;
if ((self = [super init]) == nil)
{
return nil;
}
if ([name isEqualToString: localHostName] == NO
&& entry == (struct addrinfo*)NULL)
{
NSLog(@"Host '%@' init failed - perhaps the name/address is wrong or "
@"networking is not set up on your machine", name);
DESTROY(self);
return nil;
}
else if (name == nil && entry != (struct addrinfo*)NULL)
{
NSLog(@"Nil hostname supplied but network database entry is not empty");
DESTROY(self);
return nil;
}
names = [NSMutableSet new];
addresses = [NSMutableSet new];
if ([name isEqualToString: localHostName] == YES)
{
extra = [hostClass _localAddresses];
}
else
{
extra = nil;
}
for (;;)
{
struct addrinfo *tmp;
/*
* We remove all the IP addresses that we have added to the host so
* far from the set of extra addresses available on the current host.
* Then we try to find a new network database entry for one of the
* remaining extra addresses, and loop round to add all the names
* and addresses for that entry.
*/
[extra minusSet: addresses];
while (entry == 0 && [extra count] > 0)
{
NSString *a = [extra anyObject];
struct addrinfo hints;
memset(&hints, '\0', sizeof(hints));
hints.ai_flags = AI_CANONNAME;
getaddrinfo([a UTF8String], 0, &hints, &entry);
if (0 == entry)
{
/*
* Can't find a database entry for this IP address, but since
* we know the address is valid, we add it to the list of
* addresses for this host anyway.
*/
[addresses addObject: a];
[extra removeObject: a];
}
}
if (0 == entry)
{
break;
}
for (tmp = entry; tmp != 0; tmp = tmp->ai_next)
{
if (tmp->ai_canonname && *tmp->ai_canonname)
{
NSString *n;
n = [[NSString alloc] initWithUTF8String: tmp->ai_canonname];
[names addObject: n];
[n release];
}
if (tmp->ai_addrlen > 0)
{
char host[NI_MAXHOST];
char port[NI_MAXSERV];
if (0 == getnameinfo(tmp->ai_addr, tmp->ai_addrlen,
host, NI_MAXHOST, port, NI_MAXSERV, NI_NUMERICSERV))
{
NSString *a;
a = [[NSString alloc] initWithUTF8String: host];
[addresses addObject: a];
[a release];
}
}
}
freeaddrinfo(entry);
entry = 0;
}
_names = [names copy];
RELEASE(names);
_addresses = [addresses copy];
RELEASE(addresses);
if (YES == _hostCacheEnabled)
{
[_hostCache setObject: self forKey: name];
}
return self;
}
#endif
- (id) _initWithHostEntry: (struct hostent*)entry key: (NSString*)name
{
int i;

View file

@ -37,32 +37,6 @@
#import "Foundation/NSThread.h"
#import "GNUstepBase/NSString+GNUstepBase.h"
#if defined(HAVE_GETTID)
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#endif
/* Return the current thread ID as an unsigned long.
* Ideally, we use the operating-system's notion of a thread ID so
* that external process monitoring software will be using the same
* value that we log. If we don't know the system's mechanism, we
* use the address of the current NSThread object so that, even if
* it makes no sense externally, it can still be used to show that
* different threads generated different logs.
*/
static unsigned long
GSThreadID()
{
#if defined(__MINGW__)
return (unsigned long)GetCurrentThreadId();
#elif defined(HAVE_GETTID)
return (unsigned long)syscall(SYS_gettid);
#else
return (unsigned long)GSCurrentThread();
#endif
}
// Some older BSD systems used a non-standard range of thread priorities.
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
@ -394,11 +368,12 @@ NSLogv(NSString* format, va_list args)
{
if (nil != threadName)
{
[prefix appendFormat: @"[thread:%lu,%@] ", GSThreadID(), threadName];
[prefix appendFormat: @"[thread:%lu,%@] ",
GSPrivateThreadID(), threadName];
}
else
{
[prefix appendFormat: @"[thread:%lu] ", GSThreadID()];
[prefix appendFormat: @"[thread:%lu] ", GSPrivateThreadID()];
}
}
else
@ -423,12 +398,12 @@ NSLogv(NSString* format, va_list args)
if (nil == threadName)
{
[prefix appendFormat: @"[%d:%lu] ",
pid, GSThreadID()];
pid, GSPrivateThreadID()];
}
else
{
[prefix appendFormat: @"[%d:%lu,%@] ",
pid, GSThreadID(), threadName];
pid, GSPrivateThreadID(), threadName];
}
}

View file

@ -34,20 +34,20 @@
#import "common.h"
#define EXPOSE_NSThread_IVARS 1
#ifdef HAVE_NANOSLEEP
#include <time.h>
# include <time.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
# include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
# include <sys/resource.h>
#endif
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
# include <pthread.h>
#endif
#if defined(HAVE_SYS_FILE_H)
# include <sys/file.h>
# include <sys/file.h>
#endif
#if defined(HAVE_SYS_FCNTL_H)
@ -79,19 +79,50 @@
#import "GSRunLoopCtxt.h"
#if GS_WITH_GC
#include <gc/gc.h>
# include <gc/gc.h>
#endif
#if __OBJC_GC__
#include <objc/objc-auto.h>
# include <objc/objc-auto.h>
#endif
#if defined(HAVE_PTHREAD_NP_H)
# include <pthread_np.h>
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__)
# include <pthread_np.h>
# define IS_MAIN_PTHREAD (pthread_main_np() == 1)
#else
# define IS_MAIN_PTHREAD (1)
#endif
#if defined(HAVE_GETTID)
# include <unistd.h>
# include <sys/syscall.h>
# include <sys/types.h>
#endif
/* Return the current thread ID as an unsigned long.
* Ideally, we use the operating-system's notion of a thread ID so
* that external process monitoring software will be using the same
* value that we log. If we don't know the system's mechanism, we
* use the address of the current NSThread object so that, even if
* it makes no sense externally, it can still be used to show that
* different threads generated different logs.
*/
unsigned long
GSPrivateThreadID()
{
#if defined(__MINGW__)
return (unsigned long)GetCurrentThreadId();
#elif defined(HAVE_GETTID)
return (unsigned long)syscall(SYS_gettid);
#elif defined(HAVE_PTHREAD_GETTHREADID_NP)
return pthread_getthreadid_np();
#else
return (unsigned long)GSCurrentThread();
#endif
}
#if 0
/*
* NSThread setName: method for windows.
@ -759,6 +790,12 @@ unregisterActiveThread(NSThread *thread)
[super dealloc];
}
- (NSString*) description
{
return [NSString stringWithFormat: @"%@{name = %@, num = %lu}",
[super description], _name, GSPrivateThreadID()];
}
- (id) init
{
init_autorelease_thread_vars(&_autorelease_vars);