Skip to content

Commit 935c793

Browse files
Darwin: Refactor stack startup logic when creating a controller (#32845)
* Darwin: Don't stop/start the stack when creating a controller * Keep refactorings in place but retain current behavior for now * Darwin: Enable CHIP_CONFIG_GLOBALS_NO_DESTRUCT=1 This was missed in #32745 when adding -fno-c++-static-destructors, since that flag does not carry through to lazily initialized chip::Global instances.
1 parent d7dde9b commit 935c793

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

src/darwin/Framework/CHIP/MTRDeviceController.mm

+3-13
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,15 @@ - (nullable instancetype)initWithParameters:(MTRDeviceControllerAbstractParamete
139139
{
140140
if (![parameters isKindOfClass:MTRDeviceControllerParameters.class]) {
141141
MTR_LOG_ERROR("Unsupported type of MTRDeviceControllerAbstractParameters: %@", parameters);
142-
143142
if (error) {
144143
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT];
145144
}
146145
return nil;
147146
}
147+
auto * controllerParameters = static_cast<MTRDeviceControllerParameters *>(parameters);
148148

149-
__auto_type * factory = [MTRDeviceControllerFactory sharedInstance];
150-
if (!factory.isRunning) {
151-
auto * params = [[MTRDeviceControllerFactoryParams alloc] initWithoutStorage];
152-
153-
if (![factory startControllerFactory:params error:error]) {
154-
return nil;
155-
}
156-
}
157-
158-
auto * parametersForFactory = static_cast<MTRDeviceControllerParameters *>(parameters);
159-
160-
return [factory initializeController:self withParameters:parametersForFactory error:error];
149+
// MTRDeviceControllerFactory will auto-start in per-controller-storage mode if necessary
150+
return [MTRDeviceControllerFactory.sharedInstance initializeController:self withParameters:controllerParameters error:error];
161151
}
162152

163153
- (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory

src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm

+28-6
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,14 @@ - (CHIP_ERROR)_initFabricTable:(FabricTable &)fabricTable
417417
return [NSArray arrayWithArray:fabricList];
418418
}
419419

420-
- (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams error:(NSError * __autoreleasing *)error;
420+
- (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams error:(NSError * __autoreleasing *)error
421+
{
422+
return [self _startControllerFactory:startupParams startingController:NO error:error];
423+
}
424+
425+
- (BOOL)_startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams
426+
startingController:(BOOL)startingController
427+
error:(NSError * __autoreleasing *)error
421428
{
422429
[self _assertCurrentQueueIsNotMatterQueue];
423430

@@ -535,6 +542,7 @@ - (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams
535542
// state is brought up live on factory init, and not when it comes time
536543
// to actually start a controller, and does not actually clean itself up
537544
// until its refcount (which starts as 0) goes to 0.
545+
// TODO: Don't cause a stack shutdown and restart if startingController
538546
_controllerFactory->RetainSystemState();
539547
_controllerFactory->ReleaseSystemState();
540548

@@ -597,11 +605,6 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(MTRDeviceController *
597605
{
598606
[self _assertCurrentQueueIsNotMatterQueue];
599607

600-
if (![self checkIsRunning:error]) {
601-
MTR_LOG_ERROR("Trying to start controller while Matter controller factory is not running");
602-
return nil;
603-
}
604-
605608
id<MTRDeviceControllerStorageDelegate> _Nullable storageDelegate;
606609
dispatch_queue_t _Nullable storageDelegateQueue;
607610
NSUUID * uniqueIdentifier;
@@ -623,9 +626,28 @@ - (MTRDeviceController * _Nullable)_startDeviceController:(MTRDeviceController *
623626
otaProviderDelegateQueue = nil;
624627
} else {
625628
MTR_LOG_ERROR("Unknown kind of startup params: %@", startupParams);
629+
if (error != nil) {
630+
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT];
631+
}
626632
return nil;
627633
}
628634

635+
if (![self isRunning]) {
636+
if (storageDelegate != nil) {
637+
MTR_LOG_DEFAULT("Auto-starting Matter controller factory in per-controller storage mode");
638+
auto * params = [[MTRDeviceControllerFactoryParams alloc] initWithoutStorage];
639+
if (![self _startControllerFactory:params startingController:YES error:error]) {
640+
return nil;
641+
}
642+
} else {
643+
MTR_LOG_ERROR("Trying to start controller while Matter controller factory is not running");
644+
if (error != nil) {
645+
*error = [MTRError errorForCHIPErrorCode:CHIP_ERROR_INCORRECT_STATE];
646+
}
647+
return nil;
648+
}
649+
}
650+
629651
if (_usingPerControllerStorage && storageDelegate == nil) {
630652
MTR_LOG_ERROR("Must have a controller storage delegate when we do not have storage for the controller factory");
631653
if (error != nil) {

src/darwin/Framework/Matter.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -2256,6 +2256,7 @@
22562256
OTHER_CPLUSPLUSFLAGS = (
22572257
"$(OTHER_CFLAGS)",
22582258
"-fno-c++-static-destructors",
2259+
"-DCHIP_CONFIG_GLOBALS_NO_DESTRUCT=1",
22592260
);
22602261
OTHER_LDFLAGS = "";
22612262
"OTHER_LDFLAGS[sdk=*]" = (
@@ -2421,6 +2422,7 @@
24212422
OTHER_CPLUSPLUSFLAGS = (
24222423
"$(OTHER_CFLAGS)",
24232424
"-fno-c++-static-destructors",
2425+
"-DCHIP_CONFIG_GLOBALS_NO_DESTRUCT=1",
24242426
);
24252427
OTHER_LDFLAGS = "";
24262428
"OTHER_LDFLAGS[sdk=*]" = (

0 commit comments

Comments
 (0)