34
34
35
35
MTR_DIRECT_MEMBERS
36
36
@implementation MTRServerAttribute {
37
- // _lock always protects access to _deviceController, _value, and
38
- // _parentCluster. _serializedValue is protected when we are modifying it
39
- // directly while we have no _deviceController. Once we have one,
40
- // _serializedValue is only modified on the Matter thread.
37
+ // _lock always protects access to _deviceController, _value,
38
+ // _serializedValue, and _parentCluster.
41
39
os_unfair_lock _lock;
42
40
MTRDeviceController * __weak _deviceController;
43
41
NSDictionary <NSString *, id > * _value;
@@ -137,7 +135,7 @@ - (BOOL)setValueInternal:(NSDictionary<NSString *, id> *)value logIfNotAssociate
137
135
138
136
_value = [value copy ];
139
137
140
- MTR_LOG_DEFAULT (" Attribute value updated: %@" , self); // Logs new as part of our description.
138
+ MTR_LOG_DEFAULT (" Attribute value updated: %@" , [ self _descriptionWhileLocked ] ); // Logs new value as part of our description.
141
139
142
140
MTRDeviceController * deviceController = _deviceController;
143
141
if (deviceController == nil ) {
@@ -150,6 +148,7 @@ - (BOOL)setValueInternal:(NSDictionary<NSString *, id> *)value logIfNotAssociate
150
148
_serializedValue = serializedValue;
151
149
} else {
152
150
[deviceController asyncDispatchToMatterQueue: ^{
151
+ std::lock_guard lock (self->_lock );
153
152
auto changed = ![self ->_serializedValue isEqual: serializedValue];
154
153
self->_serializedValue = serializedValue;
155
154
if (changed) {
@@ -185,7 +184,7 @@ - (BOOL)associateWithController:(nullable MTRDeviceController *)controller
185
184
186
185
_deviceController = controller;
187
186
188
- MTR_LOG_DEFAULT (" Associated %@ with controller" , self);
187
+ MTR_LOG_DEFAULT (" Associated %@ with controller" , [ self _descriptionWhileLocked ] );
189
188
190
189
return YES ;
191
190
}
@@ -216,14 +215,21 @@ - (void)updateParentCluster:(const app::ConcreteClusterPath &)cluster
216
215
_parentCluster = cluster;
217
216
}
218
217
219
- - (const chip:: app::ConcreteClusterPath &)parentCluster
218
+ - (const app::ConcreteClusterPath &)parentCluster
220
219
{
221
220
std::lock_guard lock (_lock);
222
221
return _parentCluster;
223
222
}
224
223
225
224
- (NSString *)description
226
225
{
226
+ std::lock_guard lock (_lock);
227
+ return [self _descriptionWhileLocked ];
228
+ }
229
+
230
+ - (NSString *)_descriptionWhileLocked
231
+ {
232
+ os_unfair_lock_assert_owner (&_lock);
227
233
return [NSString stringWithFormat: @" <MTRServerAttribute endpoint %u , cluster " ChipLogFormatMEI " , id " ChipLogFormatMEI " , value '%@'>" , static_cast <EndpointId>(_parentCluster.mEndpointId ), ChipLogValueMEI (_parentCluster.mClusterId ), ChipLogValueMEI (_attributeID.unsignedLongLongValue), _value];
228
234
}
229
235
0 commit comments