Skip to content

Commit

Permalink
Add a verbose log for locking operations. Has no effect in production (
Browse files Browse the repository at this point in the history
…#1436)

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

* Need check
  • Loading branch information
Adlai-Holler authored Apr 1, 2019
1 parent 4534655 commit 19e1a34
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
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

0 comments on commit 19e1a34

Please sign in to comment.