Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit f52275b

Browse files
nguyenhuyAdlai Holler
authored andcommitted
Annotate the code that records mutex owner and level of ownership in ASThread (#2809)
* Annotate the code that records mutex owner and level of ownership in ASThread * Fix typo in ASThread
1 parent bec299f commit f52275b

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

AsyncDisplayKit/Details/ASThread.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,12 @@ namespace ASDN {
199199
#if CHECK_LOCKING_SAFETY
200200
mach_port_t thread_id = pthread_mach_thread_np(pthread_self());
201201
if (thread_id != _owner) {
202+
// New owner. Since this mutex can't be acquired by another thread if there is an existing owner, _owner and _count must be 0.
202203
assert(0 == _owner);
203204
assert(0 == _count);
204205
_owner = thread_id;
205206
} else {
207+
// Existing owner tries to reacquire this (recursive) mutex. _count must already be positive.
206208
assert(_count > 0);
207209
}
208210
++_count;
@@ -212,10 +214,13 @@ namespace ASDN {
212214
void unlock () {
213215
#if CHECK_LOCKING_SAFETY
214216
mach_port_t thread_id = pthread_mach_thread_np(pthread_self());
217+
// Unlocking a mutex on an unowning thread causes undefined behaviour. Assert and fail early.
215218
assert(thread_id == _owner);
219+
// Current thread owns this mutex. _count must be positive.
216220
assert(_count > 0);
217221
--_count;
218222
if (0 == _count) {
223+
// Current thread is no longer the owner.
219224
_owner = 0;
220225
}
221226
#endif

0 commit comments

Comments
 (0)