Skip to content

Commit

Permalink
Implement Send and Sync for Rtc.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwandor committed Jul 22, 2024
1 parent e6d8ee2 commit 0a1ea3a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
- Changed `get_unix_timestamp` and `set_unix_timestamp` to use u32 rather than u64, to match the
size of the device registers.
- Made `Rtc::new` unsafe, as it must be passed a valid pointer.
- Made `set_unix_timestamp` take `&mut self` rather than `&self` because it writes to device memory.

### Other changes

- Implemented `Send` and `Sync` for `Rtc`.

## 0.1.0

Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ impl Rtc {
}

/// Sets the current time in seconds since UNIX epoch.
pub fn set_unix_timestamp(&self, unix_time: u32) {
pub fn set_unix_timestamp(&mut self, unix_time: u32) {
// SAFETY: We know that self.registers points to the control registers
// of a PL031 device which is appropriately mapped.
unsafe { addr_of_mut!((*self.registers).lr).write_volatile(unix_time) }
}
}

// SAFETY: `Rtc` just contains a pointer to device memory, which can be accessed from any context.
unsafe impl Send for Rtc {}

// SAFETY: An `&Rtc` only allows reading device registers, which can safety be done from multiple
// places at once.
unsafe impl Sync for Rtc {}

0 comments on commit 0a1ea3a

Please sign in to comment.