From 7331352a30da15c92f942d07ed1a1a7db8e61250 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Fri, 29 Apr 2016 13:51:31 -0400 Subject: Update GTMDebugThreadValidation with general queue support. --- DebugUtils/GTMDebugThreadValidation.h | 59 ++++++++++++++--------------------- DebugUtils/GTMDebugThreadValidation.m | 22 ++----------- 2 files changed, 26 insertions(+), 55 deletions(-) (limited to 'DebugUtils') diff --git a/DebugUtils/GTMDebugThreadValidation.h b/DebugUtils/GTMDebugThreadValidation.h index 0636159..3f50f17 100644 --- a/DebugUtils/GTMDebugThreadValidation.h +++ b/DebugUtils/GTMDebugThreadValidation.h @@ -1,7 +1,7 @@ // // GTMDebugThreadValidation.h // -// Copyright 2008 Google Inc. +// Copyright 2016 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy @@ -16,40 +16,29 @@ // the License. // -#if DEBUG #import "GTMDefines.h" #import -// GTMAssertRunningOnMainThread will allow you to verify that you are -// currently running on the main thread. This can be useful for checking -// under DEBUG to make sure that code that requires being run on the main thread -// is doing so. Use the GTMAssertRunningOnMainThread macro, don't use -// the _GTMAssertRunningOnMainThread or _GTMIsRunningOnMainThread -// helper functions. - -// On Leopard and above we can just use NSThread functionality. -#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 -BOOL _GTMIsRunningOnMainThread(void); -#else // MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 -#import -GTM_INLINE BOOL _GTMIsRunningOnMainThread(void) { - return [NSThread isMainThread]; -} -#endif // MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 - -GTM_INLINE void _GTMAssertRunningOnMainThread(const char *func, - const char *file, - int lineNum) { - _GTMDevAssert(_GTMIsRunningOnMainThread(), - @"%s not being run on main thread (%s - %d)", - func, file, lineNum); -} - -#define GTMAssertRunningOnMainThread() \ - (_GTMAssertRunningOnMainThread(__func__, __FILE__, __LINE__)) - -#else // DEBUG - -#define GTMAssertRunningOnMainThread() do { } while (0) - -#endif // DEBUG +// GTMCheckCurrentQueue, GTMIsCurrentQueue +// +// GTMCheckCurrentQueue takes a target queue and uses _GTMDevAssert to +// report if that is not the currently executing queue. +// +// GTMIsCurrentQueue takes a target queue and returns true if the target queue +// is the currently executing dispatch queue. This can be passed to another +// assertion call in debug builds; it should never be used in release code. +// +// The dispatch queue must have a label. +#define GTMCheckCurrentQueue(targetQueue) \ + _GTMDevAssert(GTMIsCurrentQueue(targetQueue), \ + @"Current queue is %s (expected %s)", \ + _GTMQueueName(DISPATCH_CURRENT_QUEUE_LABEL), \ + _GTMQueueName(targetQueue)) + +#define GTMIsCurrentQueue(targetQueue) \ + (strcmp(_GTMQueueName(DISPATCH_CURRENT_QUEUE_LABEL), \ + _GTMQueueName(targetQueue)) == 0) + +#define _GTMQueueName(queue) \ + (strlen(dispatch_queue_get_label(queue)) > 0 ? \ + dispatch_queue_get_label(queue) : "unnamed") diff --git a/DebugUtils/GTMDebugThreadValidation.m b/DebugUtils/GTMDebugThreadValidation.m index 30ee757..f2af8a0 100644 --- a/DebugUtils/GTMDebugThreadValidation.m +++ b/DebugUtils/GTMDebugThreadValidation.m @@ -1,7 +1,7 @@ // // GTMDebugThreadValidation.m // -// Copyright 2008 Google Inc. +// Copyright 2016 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy @@ -16,22 +16,4 @@ // the License. // -#import "GTMDebugThreadValidation.h" - -#if DEBUG && MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 - -static NSThread *gGTMMainThread = nil; - -static __attribute__((constructor)) void _GTMInitThread(void) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - gGTMMainThread = [NSThread currentThread]; - [gGTMMainThread retain]; - [pool release]; -} - - -BOOL _GTMIsRunningOnMainThread(void) { - return [[NSThread currentThread] isEqual:gGTMMainThread]; -} - -#endif // DEBUG && MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 +// No implementation source currently needed. -- cgit v1.2.3