Skip to content

Commit

Permalink
fixed warnings for github
Browse files Browse the repository at this point in the history
  • Loading branch information
jtung-apple committed Jul 29, 2022
1 parent 29ad017 commit 5574dfe
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/darwin/Framework/CHIP/MTRAsyncCallbackWorkQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

NS_ASSUME_NONNULL_BEGIN

@class MTRDevice;
@class MTRAsyncCallbackQueueWorkItem;

typedef void (^MTRAsyncCallbackReadyHandler)(MTRDevice * device, NSUInteger retryCount);
typedef void (^MTRAsyncCallbackReadyHandler)(id context, NSUInteger retryCount);

// How to queue a new work item:
// - Create MTRAsyncCallbackQueueWorkItem object
Expand Down
14 changes: 7 additions & 7 deletions src/darwin/Framework/CHIP/MTRAsyncCallbackWorkQueue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

@interface MTRAsyncCallbackWorkQueue ()
@property (nonatomic, readonly) os_unfair_lock lock;
@property (nonatomic, strong, readonly) MTRDevice * device;
@property (nonatomic, strong, readonly) id context;
@property (nonatomic, strong, readonly) dispatch_queue_t queue;
@property (nonatomic, strong, readonly) NSMutableArray<MTRAsyncCallbackQueueWorkItem *> * items;
@property (nonatomic, readwrite) NSUInteger runningWorkItemCount;
Expand All @@ -40,18 +40,18 @@ @interface MTRAsyncCallbackQueueWorkItem ()
@property (nonatomic, readwrite) NSUInteger retryCount;
@property (nonatomic, strong) MTRAsyncCallbackWorkQueue * workQueue;
// Called by the queue
- (void)callReadyHandlerWithDevice:(MTRDevice *)device;
- (void)callReadyHandlerWithContext:(id)context;
- (void)cancel;
@end

#pragma mark - Class implementations

@implementation MTRAsyncCallbackWorkQueue
- (instancetype)initWithDevice:(MTRDevice *)device queue:(dispatch_queue_t)queue
- (instancetype)initWithContext:(id)context queue:(dispatch_queue_t)queue
{
if (self = [super init]) {
_lock = OS_UNFAIR_LOCK_INIT;
_device = device;
_context = context;
_queue = queue;
_items = [NSMutableArray array];
}
Expand Down Expand Up @@ -152,7 +152,7 @@ - (void)_callNextReadyWorkItem
self.runningWorkItemCount = 1;

MTRAsyncCallbackQueueWorkItem * workItem = self.items.firstObject;
[workItem callReadyHandlerWithDevice:self.device];
[workItem callReadyHandlerWithContext:self.context];
}
@end

Expand All @@ -179,10 +179,10 @@ - (void)retryWork
}

// Called by the work queue
- (void)callReadyHandlerWithDevice:(MTRDevice *)device
- (void)callReadyHandlerWithContext:(id)context
{
dispatch_async(self.queue, ^{
self.readyHandler(device, self.retryCount);
self.readyHandler(context, self.retryCount);
self.retryCount++;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface MTRAsyncCallbackWorkQueue ()
// The MTRDevice object is only held and passed back as a reference and is opaque to the queue
- (instancetype)initWithDevice:(MTRDevice *)device queue:(dispatch_queue_t)queue;
- (instancetype)initWithContext:(id _Nullable)context queue:(dispatch_queue_t)queue;

// Called by DeviceController at device clean up time
- (void)invalidate;
Expand Down
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ - (instancetype)initWithDeviceID:(uint64_t)deviceID
_queue = queue;
_readCache = [NSMutableDictionary dictionary];
_expectedValueCache = [NSMutableDictionary dictionary];
_asyncCallbackWorkQueue = [[MTRAsyncCallbackWorkQueue alloc] initWithDevice:self queue:queue];
_asyncCallbackWorkQueue = [[MTRAsyncCallbackWorkQueue alloc] initWithContext:self queue:queue];
}
return self;
}
Expand Down
22 changes: 12 additions & 10 deletions src/darwin/Framework/CHIPTests/MTRAsyncCallbackQueueTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ - (void)testRunItem
{
XCTestExpectation * expectation = [self expectationWithDescription:@"Work item called"];

MTRAsyncCallbackWorkQueue * workQueue = [[MTRAsyncCallbackWorkQueue alloc] initWithDevice:nil queue:dispatch_get_main_queue()];
MTRAsyncCallbackWorkQueue * workQueue = [[MTRAsyncCallbackWorkQueue alloc] initWithContext:nil queue:dispatch_get_main_queue()];

MTRAsyncCallbackQueueWorkItem * workItem1 =
[[MTRAsyncCallbackQueueWorkItem alloc] initWithQueue:dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)];
Expand All @@ -56,33 +56,34 @@ - (void)testRunItemsSerialized
{
XCTestExpectation * expectation = [self expectationWithDescription:@"Work item called in order"];

MTRAsyncCallbackWorkQueue * workQueue = [[MTRAsyncCallbackWorkQueue alloc] initWithDevice:nil queue:dispatch_get_main_queue()];
MTRAsyncCallbackWorkQueue * workQueue = [[MTRAsyncCallbackWorkQueue alloc] initWithContext:nil queue:dispatch_get_main_queue()];

MTRAsyncCallbackQueueWorkItem * workItem1 =
[[MTRAsyncCallbackQueueWorkItem alloc] initWithQueue:dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)];
__block int counter = 0;
MTRAsyncCallbackReadyHandler readyHandler = ^(MTRDevice * _Nonnull device, NSUInteger retryCount) {
MTRAsyncCallbackReadyHandler readyHandler1 = ^(MTRDevice * _Nonnull device, NSUInteger retryCount) {
NSLog(@"Item 1 called with counter %d", counter);
sleep(1);
counter++;
NSLog(@"Item 1 woke after sleep with counter %d", counter);
[workItem1 endWork];
};
workItem1.readyHandler = readyHandler;
workItem1.readyHandler = readyHandler1;
workItem1.cancelHandler = ^{
};
[workQueue enqueueWorkItem:workItem1];

MTRAsyncCallbackQueueWorkItem * workItem2 =
[[MTRAsyncCallbackQueueWorkItem alloc] initWithQueue:dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)];
workItem2.readyHandler = ^(MTRDevice * _Nonnull device, NSUInteger retryCount) {
MTRAsyncCallbackReadyHandler readyHandler2 = ^(MTRDevice * _Nonnull device, NSUInteger retryCount) {
// expect this to have waited until workItem1's sleep(1) finished and incremented counter
NSLog(@"Item 2 called with counter %d", counter);
if (counter == 1) {
[expectation fulfill];
}
[workItem2 endWork];
};
workItem2.readyHandler = readyHandler2;
workItem2.cancelHandler = ^{
};
[workQueue enqueueWorkItem:workItem2];
Expand All @@ -99,13 +100,13 @@ - (void)testRunItemsRetry
{
XCTestExpectation * expectation = [self expectationWithDescription:@"Work item called in order"];

MTRAsyncCallbackWorkQueue * workQueue = [[MTRAsyncCallbackWorkQueue alloc] initWithDevice:nil queue:dispatch_get_main_queue()];
MTRAsyncCallbackWorkQueue * workQueue = [[MTRAsyncCallbackWorkQueue alloc] initWithContext:nil queue:dispatch_get_main_queue()];

MTRAsyncCallbackQueueWorkItem * workItem1 =
[[MTRAsyncCallbackQueueWorkItem alloc] initWithQueue:dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)];
__block int counter = 0;
MTRAsyncCallbackReadyHandler readyHandler = ^(MTRDevice * _Nonnull device, NSUInteger retryCount) {
NSLog(@"Item 1 called with counter %d retryCount %d", counter, retryCount);
MTRAsyncCallbackReadyHandler readyHandler1 = ^(MTRDevice * _Nonnull device, NSUInteger retryCount) {
NSLog(@"Item 1 called with counter %d retryCount %lu", counter, (unsigned long) retryCount);
sleep(1);
counter++;
NSLog(@"Item 1 woke after sleep with counter %d", counter);
Expand All @@ -117,21 +118,22 @@ - (void)testRunItemsRetry
[workItem1 retryWork];
}
};
workItem1.readyHandler = readyHandler;
workItem1.readyHandler = readyHandler1;
workItem1.cancelHandler = ^{
};
[workQueue enqueueWorkItem:workItem1];

MTRAsyncCallbackQueueWorkItem * workItem2 =
[[MTRAsyncCallbackQueueWorkItem alloc] initWithQueue:dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)];
workItem2.readyHandler = ^(MTRDevice * _Nonnull device, NSUInteger retryCount) {
MTRAsyncCallbackReadyHandler readyHandler2 = ^(MTRDevice * _Nonnull device, NSUInteger retryCount) {
// expect this to have waited until workItem1's sleep(1) finished and incremented counter
NSLog(@"Item 2 called with counter %d", counter);
if (counter == 2) {
[expectation fulfill];
}
[workItem2 endWork];
};
workItem2.readyHandler = readyHandler2;
workItem2.cancelHandler = ^{
};
[workQueue enqueueWorkItem:workItem2];
Expand Down

0 comments on commit 5574dfe

Please sign in to comment.