Skip to content

Commit 81c9d39

Browse files
author
Jorge Aparicio
committed
CsCtxt renamed to CriticalSection. Mutex.lock removed in favor of Mutex.borrow
1 parent f2d4e38 commit 81c9d39

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2121
- Execution context primitives: execution context `Local` data and `Token`s to
2222
identify execution contexts.
2323

24+
- A `borrow` method to `Mutex` that replaces `lock`. This method returns a `&-`
25+
reference.
26+
2427
### Changed
2528

2629
- [breaking-change] `StackFrame` has been renamed to `StackedRegisters` and
@@ -45,6 +48,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4548
- `vector_table` and its associated `struct`, `VectorTable`. It's not a good
4649
idea to give people a simple way to call the exception handlers.
4750

51+
- `Mutex`'s `lock` method as it's unsound. You can use it to get multiple `&mut
52+
-` references to the data.
53+
4854
## [v0.1.6] - 2017-01-22
4955

5056
### Added

src/interrupt.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@ impl<T> Mutex<T> {
1515
}
1616

1717
impl<T> Mutex<T> {
18-
/// Gets access to the inner data
19-
///
20-
/// NOTE this prevents interrupts handlers from running thus gaining
21-
/// exclusive access to the processor
22-
pub fn lock<F, R>(&self, f: F) -> R
23-
where F: FnOnce(&mut T) -> R
24-
{
25-
unsafe { ::interrupt::free(|_| f(&mut *self.inner.get())) }
18+
/// Borrows the data for the duration of the critical section
19+
pub fn borrow<'cs>(&self, _ctxt: &'cs CriticalSection) -> &'cs T {
20+
unsafe { &*self.inner.get() }
2621
}
2722
}
2823

@@ -32,7 +27,6 @@ pub unsafe trait Nr {
3227
fn nr(&self) -> u8;
3328
}
3429

35-
// FIXME `T` should have some bound: `Send` or `Sync`?
3630
unsafe impl<T> Sync for Mutex<T> {}
3731

3832
/// Disable interrupts, globally
@@ -69,25 +63,26 @@ pub fn enable() {
6963
}
7064
}
7165

72-
/// Critical section token
66+
/// Critical section context
7367
///
7468
/// Indicates that you are executing code within a critical section
75-
pub struct CsCtxt {
69+
pub struct CriticalSection {
7670
_0: (),
7771
}
7872

7973
/// Execute closure `f` in an interrupt-free context.
8074
///
8175
/// This as also known as a "critical section".
8276
pub fn free<F, R>(f: F) -> R
83-
where F: FnOnce(&CsCtxt) -> R
77+
where
78+
F: FnOnce(&CriticalSection) -> R,
8479
{
8580
let primask = ::register::primask::read();
8681

8782
// disable interrupts
8883
disable();
8984

90-
let r = f(&CsCtxt { _0: () });
85+
let r = f(&CriticalSection { _0: () });
9186

9287
// If the interrupts were active before our `disable` call, then re-enable
9388
// them. Otherwise, keep them disabled

src/peripheral/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use core::ptr;
1010

1111
use volatile_register::{RO, RW, WO};
1212

13-
use interrupt::{CsCtxt, Nr};
13+
use interrupt::{CriticalSection, Nr};
1414

1515
#[cfg(test)]
1616
mod test;
@@ -69,8 +69,8 @@ impl<T> Peripheral<T> {
6969
}
7070
}
7171

72-
/// Borrows the peripheral for the duration of the critical section
73-
pub fn borrow<'cs>(&self, _ctxt: &'cs CsCtxt) -> &'cs T {
72+
/// Borrows the peripheral for the duration of a critical section
73+
pub fn borrow<'cs>(&self, _ctxt: &'cs CriticalSection) -> &'cs T {
7474
unsafe { &*self.get() }
7575
}
7676

0 commit comments

Comments
 (0)