Review and improve lock usage in Tca955x interrupt state management #2429
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses feedback from #2427 to review lock usage versus
ConcurrentDictionary
for interrupt state management in the Tca955x driver. The goal was to improve consistency and thread safety by usingConcurrentDictionary
where appropriate while documenting cases where locks remain necessary.Changes
1. Converted
_interruptPinsSubscribedEvents
toConcurrentDictionary
Before:
After:
This provides consistency with
_eventHandlers
and_interruptLastInputValues
, which already useConcurrentDictionary
. All three collections now use the same thread-safe pattern for interrupt state management.2. Reduced Lock Scope
AddCallbackForPinValueChangedEvent
:RemoveCallbackForPinValueChangedEvent
:ConcurrentDictionary
operations3. Comprehensive Lock Documentation
Added clear comments explaining why each lock is necessary:
Each of the 8 remaining lock statements now has an inline comment explaining its purpose (I2C bus access, task coordination, etc.).
Why Some Locks Remain
I2C Bus Operations (5 locks)
The I2C protocol requires atomic, sequential access. Read-modify-write sequences cannot be interleaved without corrupting device state. This is a hardware limitation, not a software design choice.
Task Coordination (2 locks)
The
_interruptProcessingTask
and_interruptPending
fields work together as a state machine to queue interrupts. They must be checked and updated atomically to prevent race conditions and ensure only one interrupt processing task runs at a time._pinValues
DictionaryThis remains a regular
Dictionary
because it's always accessed within the same critical sections as I2C operations. Converting it toConcurrentDictionary
would provide no benefit since the lock is already held for I2C access.Benefits
ConcurrentDictionary
Related
Fixes feedback from #2427 (comment: #2427 (comment))
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
1k9vsblobprodcus379.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)37cvsblobprodcus359.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)5dkvsblobprodcus355.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)c50vsblobprodcus330.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)c78vsblobprodcus322.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)dlbvsblobprodcus316.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)fdpvsblobprodcus345.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)kh4vsblobprodcus325.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)l49vsblobprodcus358.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)m6xvsblobprodcus342.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)o3svsblobprodcus318.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)pkvvsblobprodcus321.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)uy6vsblobprodcus34.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)yttvsblobprodcus357.vsblob.vsassets.io
/home/REDACTED/work/iot/iot//.dotnet/dotnet build
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.
Microsoft Reviewers: Open in CodeFlow