mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
Thread diagnositc changes
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@38768 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
dffcd0bb1c
commit
999fda788a
7 changed files with 110 additions and 28 deletions
|
@ -1,3 +1,11 @@
|
|||
2015-07-08 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
* Headers/Foundation/NSLock.h:
|
||||
* Source/NSLock.m:
|
||||
Add extension method -isLockedByCurrentThread for debug purposes.
|
||||
Also make description report the OS thread id of the thread which
|
||||
holds the lock (if any).
|
||||
|
||||
2015-07-08 Riccardo Mottola <rm@gnu.org>
|
||||
|
||||
* Headers/GNUstepBase/GSConfig.h.in
|
||||
|
|
|
@ -78,10 +78,12 @@ extern "C" {
|
|||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to acquire lock and return immediately, YES if succeeded, NO if not.
|
||||
#if !NO_GNUSTEP
|
||||
/** Report whether this lock is held by the current thread.<br />
|
||||
* Raises an exception if this is not supported by the system lock mechanism.
|
||||
*/
|
||||
- (BOOL) tryLock;
|
||||
- (BOOL) isLockedByCurrentThread;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Try to acquire lock and return before limit, YES if succeeded, NO if not.
|
||||
|
@ -93,6 +95,11 @@ extern "C" {
|
|||
*/
|
||||
- (void) lock;
|
||||
|
||||
/**
|
||||
* Try to acquire lock and return immediately, YES if succeeded, NO if not.
|
||||
*/
|
||||
- (BOOL) tryLock;
|
||||
|
||||
/**
|
||||
* Relinquish lock.
|
||||
*/
|
||||
|
@ -183,6 +190,13 @@ extern "C" {
|
|||
*/
|
||||
- (NSInteger) condition;
|
||||
|
||||
#if !NO_GNUSTEP
|
||||
/** Report whether this lock is held by the current thread.<br />
|
||||
* Raises an exception if this is not supported by the system lock mechanism.
|
||||
*/
|
||||
- (BOOL) isLockedByCurrentThread;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Acquiring and releasing the lock.
|
||||
*/
|
||||
|
@ -265,6 +279,13 @@ extern "C" {
|
|||
#endif
|
||||
}
|
||||
|
||||
#if !NO_GNUSTEP
|
||||
/** Report whether this lock is held by the current thread.<br />
|
||||
* Raises an exception if this is not supported by the system lock mechanism.
|
||||
*/
|
||||
- (BOOL) isLockedByCurrentThread;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Try to acquire lock regardless of condition and return immediately, YES if
|
||||
* succeeded, NO if not.
|
||||
|
|
|
@ -571,7 +571,7 @@ uint32_t
|
|||
GSPrivateFinishHash(uint32_t s0, uint32_t s1, uint32_t totalLength)
|
||||
GS_ATTRIB_PRIVATE;
|
||||
|
||||
/* Return the current thread ID as an unsigned long.
|
||||
/* Return the current thread ID as an NSUInteger.
|
||||
* 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
|
||||
|
@ -579,7 +579,7 @@ GSPrivateFinishHash(uint32_t s0, uint32_t s1, uint32_t totalLength)
|
|||
* it makes no sense externally, it can still be used to show that
|
||||
* different threads generated different logs.
|
||||
*/
|
||||
unsigned long
|
||||
NSUInteger
|
||||
GSPrivateThreadID()
|
||||
GS_ATTRIB_PRIVATE;
|
||||
|
||||
|
|
|
@ -58,6 +58,7 @@
|
|||
}
|
||||
|
||||
#if defined(HAVE_PTHREAD_MUTEX_OWNER)
|
||||
|
||||
#define MDESCRIPTION \
|
||||
- (NSString*) description\
|
||||
{\
|
||||
|
@ -65,11 +66,11 @@
|
|||
{\
|
||||
if (_name == nil)\
|
||||
{\
|
||||
return [NSString stringWithFormat: @"%@ (locked by %d)",\
|
||||
[super description], (int)_mutex.__data.__owner];\
|
||||
return [NSString stringWithFormat: @"%@ (locked by %llu)",\
|
||||
[super description], (NSUInteger)_mutex.__data.__owner];\
|
||||
}\
|
||||
return [NSString stringWithFormat: @"%@ '%@' (locked by %d)",\
|
||||
[super description], _name, (int)_mutex.__data.__owner];\
|
||||
return [NSString stringWithFormat: @"%@ '%@' (locked by %llu)",\
|
||||
[super description], _name, (NSUInteger)_mutex.__data.__owner];\
|
||||
}\
|
||||
else\
|
||||
{\
|
||||
|
@ -81,7 +82,18 @@
|
|||
[super description], _name];\
|
||||
}\
|
||||
}
|
||||
|
||||
#define MISLOCKED \
|
||||
- (BOOL) isLockedByCurrentThread\
|
||||
{\
|
||||
if (GSPrivateThreadID() == (NSUInteger)_mutex.__data.__owner)\
|
||||
return YES;\
|
||||
else\
|
||||
return NO; \
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define MDESCRIPTION \
|
||||
- (NSString*) description\
|
||||
{\
|
||||
|
@ -92,6 +104,14 @@
|
|||
return [NSString stringWithFormat: @"%@ '%@'",\
|
||||
[super description], _name];\
|
||||
}
|
||||
|
||||
#define MISLOCKED \
|
||||
- (BOOL) isLockedByCurrentThread\
|
||||
{\
|
||||
[NSException raise: NSGenericException format: @"Not supported"];\
|
||||
return NO;\
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#define MFINALIZE \
|
||||
|
@ -235,6 +255,7 @@ MFINALIZE
|
|||
return self;
|
||||
}
|
||||
|
||||
MISLOCKED
|
||||
MLOCK
|
||||
|
||||
- (BOOL) lockBeforeDate: (NSDate*)limit
|
||||
|
@ -283,6 +304,7 @@ MFINALIZE
|
|||
return self;
|
||||
}
|
||||
|
||||
MISLOCKED
|
||||
MLOCK
|
||||
MLOCKBEFOREDATE
|
||||
MNAME
|
||||
|
@ -328,6 +350,7 @@ MDESCRIPTION
|
|||
return self;
|
||||
}
|
||||
|
||||
MISLOCKED
|
||||
MLOCK
|
||||
MLOCKBEFOREDATE
|
||||
MNAME
|
||||
|
@ -414,6 +437,11 @@ MUNLOCK
|
|||
return self;
|
||||
}
|
||||
|
||||
- (BOOL) isLockedByCurrentThread
|
||||
{
|
||||
return [_condition isLockedByCurrentThread];
|
||||
}
|
||||
|
||||
- (void) lock
|
||||
{
|
||||
[_condition lock];
|
||||
|
|
|
@ -368,12 +368,13 @@ NSLogv(NSString* format, va_list args)
|
|||
{
|
||||
if (nil != threadName)
|
||||
{
|
||||
[prefix appendFormat: @"[thread:%lu,%@] ",
|
||||
GSPrivateThreadID(), threadName];
|
||||
[prefix appendFormat: @"[thread:%llu,%@] ",
|
||||
(unsigned long long)GSPrivateThreadID(), threadName];
|
||||
}
|
||||
else
|
||||
{
|
||||
[prefix appendFormat: @"[thread:%lu] ", GSPrivateThreadID()];
|
||||
[prefix appendFormat: @"[thread:%llu] ",
|
||||
(unsigned long long)GSPrivateThreadID()];
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -397,13 +398,13 @@ NSLogv(NSString* format, va_list args)
|
|||
[prefix appendString: [[NSProcessInfo processInfo] processName]];
|
||||
if (nil == threadName)
|
||||
{
|
||||
[prefix appendFormat: @"[%d:%lu] ",
|
||||
pid, GSPrivateThreadID()];
|
||||
[prefix appendFormat: @"[%d:%llu] ",
|
||||
pid, (unsigned long long)GSPrivateThreadID()];
|
||||
}
|
||||
else
|
||||
{
|
||||
[prefix appendFormat: @"[%d:%lu,%@] ",
|
||||
pid, GSPrivateThreadID(), threadName];
|
||||
[prefix appendFormat: @"[%d:%llu,%@] ",
|
||||
pid, (unsigned long long)GSPrivateThreadID(), threadName];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -111,17 +111,17 @@
|
|||
* it makes no sense externally, it can still be used to show that
|
||||
* different threads generated different logs.
|
||||
*/
|
||||
unsigned long
|
||||
NSUInteger
|
||||
GSPrivateThreadID()
|
||||
{
|
||||
#if defined(__MINGW__)
|
||||
return (unsigned long)GetCurrentThreadId();
|
||||
return (NSUInteger)GetCurrentThreadId();
|
||||
#elif defined(HAVE_GETTID)
|
||||
return (unsigned long)syscall(SYS_gettid);
|
||||
return (NSUInteger)syscall(SYS_gettid);
|
||||
#elif defined(HAVE_PTHREAD_GETTHREADID_NP)
|
||||
return pthread_getthreadid_np();
|
||||
#else
|
||||
return (unsigned long)GSCurrentThread();
|
||||
return (NSUInteger)GSCurrentThread();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -796,8 +796,8 @@ unregisterActiveThread(NSThread *thread)
|
|||
|
||||
- (NSString*) description
|
||||
{
|
||||
return [NSString stringWithFormat: @"%@{name = %@, num = %lu}",
|
||||
[super description], _name, GSPrivateThreadID()];
|
||||
return [NSString stringWithFormat: @"%@{name = %@, num = %llu}",
|
||||
[super description], _name, (unsigned long long)GSPrivateThreadID()];
|
||||
}
|
||||
|
||||
- (id) init
|
||||
|
|
|
@ -6,24 +6,48 @@ int main()
|
|||
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
||||
BOOL ret;
|
||||
NSLock *lock = [NSRecursiveLock new];
|
||||
|
||||
ret = [lock tryLock];
|
||||
if (ret)
|
||||
[lock unlock];
|
||||
PASS(ret, "NSRecursiveLock with tryLock, then unlocking");
|
||||
|
||||
ASSIGN(lock,[NSRecursiveLock new]);
|
||||
ret = [lock lockBeforeDate:[NSDate dateWithTimeIntervalSinceNow:1]];
|
||||
ret = [lock lockBeforeDate: [NSDate dateWithTimeIntervalSinceNow:1]];
|
||||
if (ret)
|
||||
[lock unlock];
|
||||
PASS(ret, "NSRecursiveLock lockBeforeDate: works");
|
||||
|
||||
ASSIGN(lock,[NSRecursiveLock new]);
|
||||
[lock tryLock];
|
||||
ret = [lock lockBeforeDate:[NSDate dateWithTimeIntervalSinceNow:1]];
|
||||
ret = [lock tryLock];
|
||||
if (ret)
|
||||
[lock unlock];
|
||||
{
|
||||
ret = [lock lockBeforeDate: [NSDate dateWithTimeIntervalSinceNow:1]];
|
||||
if (ret)
|
||||
{
|
||||
[lock unlock];
|
||||
}
|
||||
[lock unlock];
|
||||
}
|
||||
PASS(ret, "NSRecursiveLock lockBeforeDate: with NSRecursiveLock returns YES");
|
||||
|
||||
#if defined(GNUSTEP_BASE_LIBRARY)
|
||||
NS_DURING
|
||||
{
|
||||
PASS([lock isLockedByCurrentThread] == NO,
|
||||
"NSRecursiveLock isLockedByCurrentThread returns NO when not locked");
|
||||
[lock lock];
|
||||
PASS([lock isLockedByCurrentThread] == YES,
|
||||
"NSRecursiveLock isLockedByCurrentThread returns YES when not locked");
|
||||
[lock unlock];
|
||||
PASS([lock isLockedByCurrentThread] == NO,
|
||||
"NSRecursiveLock isLockedByCurrentThread returns NO when unlocked");
|
||||
}
|
||||
NS_HANDLER
|
||||
{
|
||||
NSLog(@"-isLockedByCurrentThread not supported");
|
||||
}
|
||||
NS_ENDHANDLER
|
||||
#endif
|
||||
|
||||
[arp release]; arp = nil;
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue