Skip to content

drivers/... : Acquire exclusive access to I2C bus#2326

Merged
OlegHahm merged 10 commits intoRIOT-OS:masterfrom
PeterKietzmann:make_drivers_thread_safe
Mar 3, 2015
Merged

drivers/... : Acquire exclusive access to I2C bus#2326
OlegHahm merged 10 commits intoRIOT-OS:masterfrom
PeterKietzmann:make_drivers_thread_safe

Conversation

@PeterKietzmann
Copy link
Copy Markdown
Member

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.

@PeterKietzmann PeterKietzmann added Area: drivers Area: Device drivers Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation labels Jan 20, 2015
@PeterKietzmann PeterKietzmann force-pushed the make_drivers_thread_safe branch from 4e3a8a8 to 12c7815 Compare January 20, 2015 07:55
@PeterKietzmann
Copy link
Copy Markdown
Member Author

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:

Context 1:
i2c_acquire
value = i2c_read_reg(A)
i2c_release
process(value)

[Context switch to Context 2 here]

Context 2:
i2c_acquire
i2c_write_reg(A)
i2c_release

[Context switch back to Context 1 here]

Context 1:
i2c_acquire
i2c_write_reg(A)
i2c_release

This would result in working with an outdated value.

Is this a realistic example? What is your opinion on that?

@PeterKietzmann PeterKietzmann added the Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR label Jan 20, 2015
@jnohlgard
Copy link
Copy Markdown
Member

@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.

@PeterKietzmann
Copy link
Copy Markdown
Member Author

Thanks for your advice @gebart. Any other opinions on that so I can complete this PR soon?

@thomaseichinger
Copy link
Copy Markdown
Member

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.

@PeterKietzmann
Copy link
Copy Markdown
Member Author

After your comments I tend to "just" use spi_acquire and spi_release in this PR, to safe the unique bus access as intended. The rest we should keep in mind. Are you fine with this for the moment?

@jnohlgard
Copy link
Copy Markdown
Member

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.

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.

@PeterKietzmann PeterKietzmann added the Community: Hack'n'ACK candidate This PR is a candidate for review and discussion during one of RIOT's monthly Hack'n'ACK parties label Jan 26, 2015
@PeterKietzmann
Copy link
Copy Markdown
Member Author

@jfischer-phytec-iot I will work on this PR this evening again. Thx for the look up.

@PeterKietzmann PeterKietzmann force-pushed the make_drivers_thread_safe branch 4 times, most recently from ba972c3 to 8f8fa7b Compare January 29, 2015 09:03
@PeterKietzmann
Copy link
Copy Markdown
Member Author

@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.

@PeterKietzmann
Copy link
Copy Markdown
Member Author

@thomaseichinger did you already have a look at this PR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this header?

@thomaseichinger
Copy link
Copy Markdown
Member

Do you really need to include the mutex header? The rest looks good to me.

@PeterKietzmann PeterKietzmann force-pushed the make_drivers_thread_safe branch from d79680c to 3d44ad3 Compare February 17, 2015 12:47
@PeterKietzmann
Copy link
Copy Markdown
Member Author

@thomaseichinger no it is not needed. I removed these includes.

@OlegHahm OlegHahm added the CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable label Feb 17, 2015
@PeterKietzmann PeterKietzmann force-pushed the make_drivers_thread_safe branch from 3d44ad3 to dcd70c8 Compare March 3, 2015 17:08
@PeterKietzmann PeterKietzmann removed the CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable label Mar 3, 2015
@PeterKietzmann
Copy link
Copy Markdown
Member Author

@thomaseichinger I squashed the last "SQUASH ME-labeled"commit and removed the "NEEDS SQUASHING" label. ACK?

@thomaseichinger
Copy link
Copy Markdown
Member

Code looks good to me. As I don't have most of the sensors, did you test exhaustively? ;)

@PeterKietzmann
Copy link
Copy Markdown
Member Author

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 :-)

@thomaseichinger
Copy link
Copy Markdown
Member

ACK when Travis passed.

@OlegHahm
Copy link
Copy Markdown
Member

OlegHahm commented Mar 3, 2015

Kicked Travis.

@PeterKietzmann
Copy link
Copy Markdown
Member Author

Travis is happy :-)

OlegHahm added a commit that referenced this pull request Mar 3, 2015
drivers/... : Acquire exclusive access to I2C bus
@OlegHahm OlegHahm merged commit f978282 into RIOT-OS:master Mar 3, 2015
@PeterKietzmann PeterKietzmann deleted the make_drivers_thread_safe branch March 26, 2015 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: drivers Area: Device drivers Community: Hack'n'ACK candidate This PR is a candidate for review and discussion during one of RIOT's monthly Hack'n'ACK parties Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants