drivers/... : Acquire exclusive access to I2C bus#2326
drivers/... : Acquire exclusive access to I2C bus#2326OlegHahm merged 10 commits intoRIOT-OS:masterfrom
Conversation
4e3a8a8 to
12c7815
Compare
|
Hi guys. Protecting the register-accesses with mutexes protects against concurrent writing, just as intended (like I implemented in commit 55c1724). However, when adapting the I2C-dependent sensor drivers I was wondering if we should better lock the whole function context to prevent to process outdated values (like I implemented in commit e3983bf). An example: Suppose a function that reads a register, adapts this value and writes this back to the register. Procedure: This would result in working with an outdated value. Is this a realistic example? What is your opinion on that? |
|
@PeterKietzmann My first hunch would be to add a separate mutex for the driver's own data, to protect against those kinds of races. If the processing is small, e.g. increment the value read and write back the result, I guess it would be possible to keep the SPI bus acquired during the whole read-modify-write cycle, but it would still be misuse of the SPI mutex. |
|
Thanks for your advice @gebart. Any other opinions on that so I can complete this PR soon? |
|
I couldn't think of a case yet but could this cascade of mutexes lead to a deadlock? If not I'd prefer a separated mutex too. |
|
After your comments I tend to "just" use |
If you write the threads so that they lock the mutexes in inverse order compared to each other it can lead to a deadlock. Lock the mutexes in the same order in every situation. The rule of thumb here should be something along the lines of: lock the driver data mutex first, then acquire the bus. |
|
@jfischer-phytec-iot I will work on this PR this evening again. Thx for the look up. |
ba972c3 to
8f8fa7b
Compare
|
@thomaseichinger, I adapted all I2C-based drivers to new "thread safe" model. I decided to leave the mutex locked during a "read-modify-write" cycle when the modifying part is short. |
|
@thomaseichinger did you already have a look at this PR? |
drivers/isl29020/isl29020.c
Outdated
There was a problem hiding this comment.
Do you need this header?
|
Do you really need to include the mutex header? The rest looks good to me. |
d79680c to
3d44ad3
Compare
|
@thomaseichinger no it is not needed. I removed these includes. |
3d44ad3 to
dcd70c8
Compare
|
@thomaseichinger I squashed the last "SQUASH ME-labeled"commit and removed the "NEEDS SQUASHING" label. ACK? |
|
Code looks good to me. As I don't have most of the sensors, did you test exhaustively? ;) |
|
I compiled all and spot-tested some. Neither I have all of these sensors but many of them you can find on the phytec board :-) |
|
ACK when Travis passed. |
|
Kicked Travis. |
|
Travis is happy :-) |
drivers/... : Acquire exclusive access to I2C bus
As discussed in #2289 this PR adapts all i2c-based sensor drivers to use the i2c interface safely within multiple threads. I will push separate commits for each sensor driver.