Skip to content

Conversation

@dNechita
Copy link
Contributor

PR Description

Local Backend

First patch introduces thread-safe register read/write operations for the local backend using direct_reg_access via debugfs. It adds two new backend ops:

  • reg_read()
  • reg_write()

These functions use file locking (flock) to ensure atomic access and prevent race conditions when multiple threads interact with the same register interface.

The fallback logic in iio_device_reg_read() and iio_device_reg_write() is preserved for backends that do not implement these atomic operations.

This improves reliability for multi-threaded applications using libiio with the local backend.

Network, USB and Serial Backends

Second patch implement atomic register operations which ensures that a register read or a register write cannot be interrupted by other clients of IIOD.
Advantages:

  • half round trips: single command vs write-address + read-value
  • binary addresses/values vs hex string conversion

On client-server compatibility:
v1.0 clients can connect to v1.0+ servers (atomic ops used)
v1.0 clients connecting to v0.x servers fall back to attribute method

Closes #1351.

PR Type

  • Bug fix (a change that fixes an issue)
  • New feature (a change that adds new functionality)
  • Breaking change (a change that affects other repos or cause CIs to fail)

PR Checklist

  • I have conducted a self-review of my own code changes
  • I have commented new code, particularly complex or unclear areas
  • I have checked that I did not introduce new warnings or errors (CI output)
  • I have checked that components that use libiio did not get broken
  • I have updated the documentation accordingly (GitHub Pages, READMEs, etc)

This patch introduces thread-safe register read/write operations for the
local backend using `direct_reg_access` via debugfs. It adds two new
backend ops:
- `reg_read()`
- `reg_write()`

These functions use file locking (`flock`) to ensure atomic access and
prevent race conditions when multiple threads interact with the same
register interface.

The fallback logic in `iio_device_reg_read()` and `iio_device_reg_write()`
is preserved for backends that do not implement these atomic operations.

This improves reliability for multi-threaded applications using libiio
with the local backend.

Signed-off-by: Michael Hennerich <[email protected]>
@rgetz
Copy link
Contributor

rgetz commented Nov 21, 2025

Don't you want to fix this at the same time?

     * NOTE: This has a race condition when multiple clients access
     * the same IIOD server, as the two-step process (write address,
     * then read value) is not atomic.

@dNechita
Copy link
Contributor Author

Don't you want to fix this at the same time?

     * NOTE: This has a race condition when multiple clients access
     * the same IIOD server, as the two-step process (write address,
     * then read value) is not atomic.

Yes, I think this can be removed. The race condition won't happen on IIOD server anymore.
Thanks!

Implement atomic register operations which ensures that a register read
or a register write cannot be interrupted by other clients of IIOD.
Advantages:
 - half round trips: single command vs write-address + read-value
 - binary addresses/values vs hex string converion

On client-server compatibility:
v1.0 clients can connect to v1.0+ servers (atomic ops used)
v1.0 clients connecting to v0.x servers fall back to attribute method

Signed-off-by: Dan Nechita <[email protected]>
@dNechita dNechita force-pushed the atomic-reg-access-all-backends branch from 2ba0f46 to 22331ae Compare November 24, 2025 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable atomic register access support across all backends

4 participants