Skip to content

Commit

Permalink
Moving to NSMapTable
Browse files Browse the repository at this point in the history
  • Loading branch information
woody-apple committed Jun 14, 2024
1 parent a12a0c0 commit 23d202a
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ @implementation MTRDeviceController {
MTRP256KeypairBridge _operationalKeypairBridge;
MTRDeviceAttestationDelegateBridge * _deviceAttestationDelegateBridge;
MTRDeviceControllerFactory * _factory;
NSMutableDictionary * _nodeIDToDeviceMap;
NSMapTable * _nodeIDToDeviceMap;
os_unfair_lock _deviceMapLock; // protects nodeIDToDeviceMap
MTRCommissionableBrowser * _commissionableBrowser;
MTRAttestationTrustStoreBridge * _attestationTrustStoreBridge;
Expand Down Expand Up @@ -236,7 +236,7 @@ - (instancetype)initWithFactory:(MTRDeviceControllerFactory *)factory
_chipWorkQueue = queue;
_factory = factory;
_deviceMapLock = OS_UNFAIR_LOCK_INIT;
_nodeIDToDeviceMap = [NSMutableDictionary dictionary];
_nodeIDToDeviceMap = [NSMapTable strongToWeakObjectsMapTable];
_serverEndpoints = [[NSMutableArray alloc] init];
_commissionableBrowser = nil;

Expand Down Expand Up @@ -307,7 +307,7 @@ - (void)cleanupAfterStartup
// while calling out into arbitrary invalidation code, snapshot the list of
// devices before we start invalidating.
os_unfair_lock_lock(&_deviceMapLock);
NSArray<MTRDevice *> * devices = [_nodeIDToDeviceMap allValues];
NSEnumerator * devices = [_nodeIDToDeviceMap objectEnumerator];
[_nodeIDToDeviceMap removeAllObjects];
os_unfair_lock_unlock(&_deviceMapLock);

Expand Down Expand Up @@ -936,8 +936,7 @@ - (void)preWarmCommissioningSession

- (MTRBaseDevice *)deviceBeingCommissionedWithNodeID:(NSNumber *)nodeID error:(NSError * __autoreleasing *)error
{
auto block = ^MTRBaseDevice *
{
auto block = ^MTRBaseDevice * {
chip::CommissioneeDeviceProxy * deviceProxy;

auto errorCode = self->_cppCommissioner->GetDeviceBeingCommissioned(nodeID.unsignedLongLongValue, &deviceProxy);
Expand Down Expand Up @@ -970,7 +969,7 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N
// which will be in exactly the state it would be in if it were created
// while we were running and then we got shut down.
if ([self isRunning]) {
_nodeIDToDeviceMap[nodeID] = deviceToReturn;
[_nodeIDToDeviceMap setObject: deviceToReturn forKey: nodeID];
}

if (prefetchedClusterData) {
Expand Down Expand Up @@ -1002,7 +1001,7 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N
- (MTRDevice *)deviceForNodeID:(NSNumber *)nodeID
{
std::lock_guard lock(_deviceMapLock);
MTRDevice * deviceToReturn = _nodeIDToDeviceMap[nodeID];
MTRDevice * deviceToReturn = [_nodeIDToDeviceMap objectForKey: nodeID];
if (!deviceToReturn) {
deviceToReturn = [self _setupDeviceForNodeID:nodeID prefetchedClusterData:nil];
}
Expand All @@ -1014,10 +1013,10 @@ - (void)removeDevice:(MTRDevice *)device
{
std::lock_guard lock(_deviceMapLock);
auto * nodeID = device.nodeID;
MTRDevice * deviceToRemove = _nodeIDToDeviceMap[nodeID];
MTRDevice * deviceToRemove = [_nodeIDToDeviceMap objectForKey: nodeID];
if (deviceToRemove == device) {
[deviceToRemove invalidate];
_nodeIDToDeviceMap[nodeID] = nil;
[_nodeIDToDeviceMap removeObjectForKey: nodeID];
} else {
MTR_LOG_ERROR("Error: Cannot remove device %p with nodeID %llu", device, nodeID.unsignedLongLongValue);
}
Expand All @@ -1029,7 +1028,7 @@ - (void)removeDevice:(MTRDevice *)device
std::lock_guard lock(_deviceMapLock);
NSMutableDictionary<NSNumber *, NSNumber *> * deviceAttributeCounts = [NSMutableDictionary dictionary];
for (NSNumber * nodeID in _nodeIDToDeviceMap) {
deviceAttributeCounts[nodeID] = @([_nodeIDToDeviceMap[nodeID] unitTestAttributeCount]);
deviceAttributeCounts[nodeID] = @([[_nodeIDToDeviceMap objectForKey: nodeID] unitTestAttributeCount]);
}
return deviceAttributeCounts;
}
Expand Down Expand Up @@ -1096,8 +1095,7 @@ + (nullable NSData *)computePASEVerifierForSetupPasscode:(NSNumber *)setupPassco

- (NSData * _Nullable)attestationChallengeForDeviceID:(NSNumber *)deviceID
{
auto block = ^NSData *
{
auto block = ^NSData * {
chip::CommissioneeDeviceProxy * deviceProxy;

auto errorCode = CHIP_NO_ERROR;
Expand Down Expand Up @@ -1260,7 +1258,7 @@ - (void)getSessionForNode:(chip::NodeId)nodeID completion:(MTRInternalDeviceConn
// First check if MTRDevice exists from having loaded from storage, or created by a client.
// Do not use deviceForNodeID here, because we don't want to create the device if it does not already exist.
os_unfair_lock_lock(&_deviceMapLock);
MTRDevice * device = _nodeIDToDeviceMap[@(nodeID)];
MTRDevice * device = [_nodeIDToDeviceMap objectForKey: @(nodeID)];
os_unfair_lock_unlock(&_deviceMapLock);

// In the case that this device is known to use thread, queue this with subscription attempts as well, to
Expand Down Expand Up @@ -1482,7 +1480,7 @@ - (void)operationalInstanceAdded:(chip::NodeId)nodeID
// Don't use deviceForNodeID here, because we don't want to create the
// device if it does not already exist.
os_unfair_lock_lock(&_deviceMapLock);
MTRDevice * device = _nodeIDToDeviceMap[@(nodeID)];
MTRDevice * device = [_nodeIDToDeviceMap objectForKey: @(nodeID)];
os_unfair_lock_unlock(&_deviceMapLock);

if (device == nil) {
Expand Down Expand Up @@ -1922,8 +1920,7 @@ - (NSString *)openPairingWindowWithPIN:(uint64_t)deviceID
return nil;
}

auto block = ^NSString *
{
auto block = ^NSString * {
chip::SetupPayload setupPayload;
errorCode = chip::Controller::AutoCommissioningWindowOpener::OpenCommissioningWindow(self->_cppCommissioner, deviceID,
chip::System::Clock::Seconds16(static_cast<uint16_t>(duration)), chip::Crypto::kSpake2p_Min_PBKDF_Iterations,
Expand Down

0 comments on commit 23d202a

Please sign in to comment.