Skip to content

Commit

Permalink
Moving more Darwin stuff to std::lock_guard lock(_lock); (project-chi…
Browse files Browse the repository at this point in the history
…p#32558)

* Initial commit

* Restyled by whitespace

* Reverting this one

* Addressing feedback

* Adding a few more

* Update src/darwin/Framework/CHIP/MTRAsyncCallbackWorkQueue.mm

* Removing this one

* Restyled by clang-format

* Reverting these

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
woody-apple and restyled-commits authored Mar 19, 2024
1 parent a0ba592 commit f4f1e19
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 97 deletions.
30 changes: 10 additions & 20 deletions src/darwin/Framework/CHIP/MTRAsyncCallbackWorkQueue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#import <os/lock.h>

#import "MTRLogging_Internal.h"
#import "MTRUnfairLock.h"

#import <Matter/MTRAsyncCallbackWorkQueue.h>

#pragma mark - Class extensions
Expand Down Expand Up @@ -72,14 +74,10 @@ - (instancetype)initWithContext:(id)context queue:(dispatch_queue_t)queue

- (NSString *)description
{
os_unfair_lock_lock(&_lock);
std::lock_guard lock(_lock);

auto * desc = [NSString
return [NSString
stringWithFormat:@"MTRAsyncCallbackWorkQueue context: %@ items count: %lu", self.context, (unsigned long) self.items.count];

os_unfair_lock_unlock(&_lock);

return desc;
}

- (void)enqueueWorkItem:(MTRAsyncCallbackQueueWorkItem *)item
Expand All @@ -91,12 +89,11 @@ - (void)enqueueWorkItem:(MTRAsyncCallbackQueueWorkItem *)item

[item markEnqueued];

os_unfair_lock_lock(&_lock);
std::lock_guard lock(_lock);
item.workQueue = self;
[self.items addObject:item];

[self _callNextReadyWorkItem];
os_unfair_lock_unlock(&_lock);
}

- (void)invalidate
Expand All @@ -115,11 +112,10 @@ - (void)invalidate
// called after executing a work item
- (void)_postProcessWorkItem:(MTRAsyncCallbackQueueWorkItem *)workItem retry:(BOOL)retry
{
os_unfair_lock_lock(&_lock);
std::lock_guard lock(_lock);
// sanity check if running
if (!self.runningWorkItemCount) {
// something is wrong with state - nothing is currently running
os_unfair_lock_unlock(&_lock);
MTR_LOG_ERROR("MTRAsyncCallbackWorkQueue endWork: no work is running on work queue");
return;
}
Expand All @@ -129,7 +125,6 @@ - (void)_postProcessWorkItem:(MTRAsyncCallbackQueueWorkItem *)workItem retry:(BO
MTRAsyncCallbackQueueWorkItem * firstWorkItem = self.items.firstObject;
if (firstWorkItem != workItem) {
// something is wrong with this work item - should not be currently running
os_unfair_lock_unlock(&_lock);
MTR_LOG_ERROR("MTRAsyncCallbackWorkQueue endWork: work item is not first on work queue");
return;
}
Expand All @@ -142,7 +137,6 @@ - (void)_postProcessWorkItem:(MTRAsyncCallbackQueueWorkItem *)workItem retry:(BO
// when "concurrency width" is implemented this will be decremented instead
self.runningWorkItemCount = 0;
[self _callNextReadyWorkItem];
os_unfair_lock_unlock(&_lock);
}

- (void)endWork:(MTRAsyncCallbackQueueWorkItem *)workItem
Expand Down Expand Up @@ -203,34 +197,30 @@ - (void)_invalidate

- (void)invalidate
{
os_unfair_lock_lock(&_lock);
std::lock_guard lock(_lock);
[self _invalidate];
os_unfair_lock_unlock(&_lock);
}

- (void)markEnqueued
{
os_unfair_lock_lock(&_lock);
std::lock_guard lock(_lock);
_enqueued = YES;
os_unfair_lock_unlock(&_lock);
}

- (void)setReadyHandler:(MTRAsyncCallbackReadyHandler)readyHandler
{
os_unfair_lock_lock(&_lock);
std::lock_guard lock(_lock);
if (!_enqueued) {
_readyHandler = readyHandler;
}
os_unfair_lock_unlock(&_lock);
}

- (void)setCancelHandler:(dispatch_block_t)cancelHandler
{
os_unfair_lock_lock(&_lock);
std::lock_guard lock(_lock);
if (!_enqueued) {
_cancelHandler = cancelHandler;
}
os_unfair_lock_unlock(&_lock);
}

- (void)endWork
Expand Down
14 changes: 6 additions & 8 deletions src/darwin/Framework/CHIP/MTRAsyncWorkQueue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import "MTRAsyncWorkQueue.h"
#import "MTRDefines_Internal.h"
#import "MTRLogging_Internal.h"
#import "MTRUnfairLock.h"

#import <atomic>
#import <os/lock.h>
Expand Down Expand Up @@ -273,13 +274,12 @@ - (void)enqueueWorkItem:(MTRAsyncWorkItem *)item
- (void)invalidate
{
ContextSnapshot context(_context); // outside of lock
os_unfair_lock_lock(&_lock);
std::lock_guard lock(_lock);
MTR_LOG_INFO("MTRAsyncWorkQueue<%@> invalidate %tu items", context.description, _items.count);
for (MTRAsyncWorkItem * item in _items) {
[item cancel];
}
[_items removeAllObjects];
os_unfair_lock_unlock(&_lock);
}

- (void)_postProcessWorkItem:(MTRAsyncWorkItem *)workItem
Expand Down Expand Up @@ -376,8 +376,7 @@ - (void)_callNextReadyWorkItemWithContext:(ContextSnapshot const &)context

- (BOOL)hasDuplicateForTypeID:(NSUInteger)opaqueDuplicateTypeID workItemData:(id)opaqueWorkItemData
{
BOOL hasDuplicate = NO;
os_unfair_lock_lock(&_lock);
std::lock_guard lock(_lock);
// Start from the last item
for (MTRAsyncWorkItem * item in [_items reverseObjectEnumerator]) {
auto duplicateCheckHandler = item.duplicateCheckHandler;
Expand All @@ -386,13 +385,12 @@ - (BOOL)hasDuplicateForTypeID:(NSUInteger)opaqueDuplicateTypeID workItemData:(id
BOOL isDuplicate = NO;
duplicateCheckHandler(opaqueWorkItemData, &isDuplicate, &stop);
if (stop) {
hasDuplicate = isDuplicate;
break;
return isDuplicate;
}
}
}
os_unfair_lock_unlock(&_lock);
return hasDuplicate;

return NO;
}

@end
Expand Down
Loading

0 comments on commit f4f1e19

Please sign in to comment.