Skip to content

Commit e228f63

Browse files
committed
change internal structure from Mutex<RefCell<>> to Arc<Mutex<<>>
1 parent 8ea3d00 commit e228f63

File tree

5 files changed

+172
-197
lines changed

5 files changed

+172
-197
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
- Added re-exports for `libftd2xx` and `ftdi` when the respective feature is used.
1010

11+
### Changed
12+
- Changed the internal shared FTDI type from `Mutex<RefCell<FtInner<Device>>>` to `Arc<Mutex<FtInner<Device>>>`.
13+
1114
## [0.11.0] - 2022-01-18
1215
### Added
1316
- Added support for input pins.

src/gpio.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::error::Error;
22
use crate::{FtInner, PinUse};
33
use ftdi_mpsse::{MpsseCmdBuilder, MpsseCmdExecutor};
4-
use std::result::Result;
5-
use std::{cell::RefCell, sync::Mutex};
4+
use std::sync::{Arc, Mutex};
65

76
/// FTDI output pin.
87
///
@@ -13,7 +12,7 @@ use std::{cell::RefCell, sync::Mutex};
1312
#[derive(Debug)]
1413
pub struct OutputPin<'a, Device: MpsseCmdExecutor> {
1514
/// Parent FTDI device.
16-
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
15+
mtx: &'a Arc<Mutex<FtInner<Device>>>,
1716
/// GPIO pin index. 0-7 for the FT232H.
1817
idx: u8,
1918
}
@@ -25,34 +24,33 @@ where
2524
Error<E>: From<E>,
2625
{
2726
pub(crate) fn new(
28-
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
27+
mtx: &'a Arc<Mutex<FtInner<Device>>>,
2928
idx: u8,
3029
) -> Result<OutputPin<'a, Device>, Error<E>> {
31-
let lock = mtx.lock().expect("Failed to aquire FTDI mutex");
32-
let mut inner = lock.borrow_mut();
33-
inner.direction |= 1 << idx;
34-
inner.allocate_pin(idx, PinUse::Output);
30+
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");
31+
32+
lock.direction |= 1 << idx;
33+
lock.allocate_pin(idx, PinUse::Output);
3534
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
36-
.set_gpio_lower(inner.value, inner.direction)
35+
.set_gpio_lower(lock.value, lock.direction)
3736
.send_immediate();
38-
inner.ft.send(cmd.as_slice())?;
37+
lock.ft.send(cmd.as_slice())?;
3938
Ok(OutputPin { mtx, idx })
4039
}
4140

4241
pub(crate) fn set(&self, state: bool) -> Result<(), Error<E>> {
43-
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
44-
let mut inner = lock.borrow_mut();
42+
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
4543

4644
if state {
47-
inner.value |= self.mask();
45+
lock.value |= self.mask();
4846
} else {
49-
inner.value &= !self.mask();
47+
lock.value &= !self.mask();
5048
};
5149

5250
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
53-
.set_gpio_lower(inner.value, inner.direction)
51+
.set_gpio_lower(lock.value, lock.direction)
5452
.send_immediate();
55-
inner.ft.send(cmd.as_slice())?;
53+
lock.ft.send(cmd.as_slice())?;
5654

5755
Ok(())
5856
}
@@ -91,7 +89,7 @@ where
9189
#[derive(Debug)]
9290
pub struct InputPin<'a, Device: MpsseCmdExecutor> {
9391
/// Parent FTDI device.
94-
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
92+
mtx: &'a Arc<Mutex<FtInner<Device>>>,
9593
/// GPIO pin index. 0-7 for the FT232H.
9694
idx: u8,
9795
}
@@ -103,28 +101,27 @@ where
103101
Error<E>: From<E>,
104102
{
105103
pub(crate) fn new(
106-
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
104+
mtx: &'a Arc<Mutex<FtInner<Device>>>,
107105
idx: u8,
108106
) -> Result<InputPin<'a, Device>, Error<E>> {
109-
let lock = mtx.lock().expect("Failed to aquire FTDI mutex");
110-
let mut inner = lock.borrow_mut();
111-
inner.direction &= !(1 << idx);
112-
inner.allocate_pin(idx, PinUse::Input);
107+
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");
108+
109+
lock.direction &= !(1 << idx);
110+
lock.allocate_pin(idx, PinUse::Input);
113111
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
114-
.set_gpio_lower(inner.value, inner.direction)
112+
.set_gpio_lower(lock.value, lock.direction)
115113
.send_immediate();
116-
inner.ft.send(cmd.as_slice())?;
114+
lock.ft.send(cmd.as_slice())?;
117115
Ok(InputPin { mtx, idx })
118116
}
119117

120118
pub(crate) fn get(&self) -> Result<bool, Error<E>> {
121-
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
122-
let mut inner = lock.borrow_mut();
119+
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
123120

124121
let mut buffer = [0u8; 1];
125122
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new().gpio_lower().send_immediate();
126-
inner.ft.send(cmd.as_slice())?;
127-
inner.ft.recv(&mut buffer)?;
123+
lock.ft.send(cmd.as_slice())?;
124+
lock.ft.recv(&mut buffer)?;
128125

129126
Ok((buffer[0] & self.mask()) != 0)
130127
}

0 commit comments

Comments
 (0)