Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a verbose log for locking operations. Has no effect in production #1436

Merged
merged 2 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Source/ASDisplayNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ - (void)_staticInitialize
- (void)_initializeInstance
{
[self _staticInitialize];
__instanceLock__.SetDebugNameWithObject(self);

#if ASEVENTLOG_ENABLE
_eventLog = [[ASEventLog alloc] initWithObject:self];
Expand Down
3 changes: 3 additions & 0 deletions Source/Base/ASLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ AS_EXTERN os_log_t ASImageLoadingLog(void);
#define ASMainThreadDeallocationLogEnabled 0
AS_EXTERN os_log_t ASMainThreadDeallocationLog(void);

#define ASLockingLogEnabled 0
AS_EXTERN os_log_t ASLockingLog(void);

/**
* The activity tracing system changed a lot between iOS 9 and 10.
* In iOS 10, the system was merged with logging and became much more powerful
Expand Down
4 changes: 4 additions & 0 deletions Source/Base/ASLog.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ os_log_t ASImageLoadingLog() {
os_log_t ASMainThreadDeallocationLog() {
return (ASMainThreadDeallocationLogEnabled && ASLoggingIsEnabled()) ? ASCreateOnce(as_log_create("org.TextureGroup.Texture", "MainDealloc")) : OS_LOG_DISABLED;
}

os_log_t ASLockingLog() {
return (ASLockingLogEnabled && ASLoggingIsEnabled()) ? ASCreateOnce(as_log_create("org.TextureGroup.Texture", "Locking")) : OS_LOG_DISABLED;
}
22 changes: 22 additions & 0 deletions Source/Details/ASThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#import <AsyncDisplayKit/ASAvailability.h>
#import <AsyncDisplayKit/ASBaseDefines.h>
#import <AsyncDisplayKit/ASConfigurationInternal.h>
#import <AsyncDisplayKit/ASLog.h>
#import <AsyncDisplayKit/ASObjectDescriptionHelpers.h>
#import <AsyncDisplayKit/ASRecursiveUnfairLock.h>

ASDISPLAYNODE_INLINE AS_WARN_UNUSED_RESULT BOOL ASDisplayNodeThreadIsMain()
Expand Down Expand Up @@ -117,6 +119,12 @@ namespace AS {
/// Constructs a plain mutex (the default).
Mutex () : Mutex (false) {}

void SetDebugNameWithObject(id object) {
#if ASEnableVerboseLogging && ASDISPLAYNODE_ASSERTIONS_ENABLED
_debug_name = std::string(ASObjectDescriptionMakeTiny(object).UTF8String);
#endif
}

~Mutex () {
// Manually destroy since unions can't do it.
switch (_type) {
Expand Down Expand Up @@ -243,6 +251,11 @@ namespace AS {

void WillUnlock() {
#if ASDISPLAYNODE_ASSERTIONS_ENABLED
#if ASEnableVerboseLogging
if (!_debug_name.empty()) {
as_log_verbose(ASLockingLog(), "unlock %s, count is %d", _debug_name.c_str(), (int)(_count - 1));
}
#endif
if (--_count == 0) {
_owner = std::thread::id();
}
Expand All @@ -251,6 +264,11 @@ namespace AS {

void DidLock() {
#if ASDISPLAYNODE_ASSERTIONS_ENABLED
#if ASEnableVerboseLogging
if (!_debug_name.empty()) {
as_log_verbose(ASLockingLog(), "lock %s, count is %d", _debug_name.c_str(), (int)(_count + 1));
}
#endif
if (++_count == 1) {
// New owner.
_owner = std::this_thread::get_id();
Expand All @@ -265,6 +283,10 @@ namespace AS {
std::mutex _plain;
std::recursive_mutex _recursive;
};
#if ASEnableVerboseLogging
std::string _debug_name;
#endif

#if ASDISPLAYNODE_ASSERTIONS_ENABLED
std::thread::id _owner = std::thread::id();
int _count = 0;
Expand Down