Skip to content

Commit b7b3e0c

Browse files
committed
Update shared_memory crate
The crate now only provides a ptr to the shared memory and no locking primitive of its own, which I think is perfectly fine for this use-case as we're only operating on plain old data. Tested with wooting-analog-midi.
1 parent 8f258ea commit b7b3e0c

File tree

6 files changed

+86
-131
lines changed

6 files changed

+86
-131
lines changed

Cargo.lock

Lines changed: 65 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wooting-analog-sdk/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ bimap = "0.4"
2424
wooting-analog-common = { path = "../wooting-analog-common"}
2525
wooting-analog-plugin-dev = { path = "../wooting-analog-plugin-dev"}
2626

27-
[dev-dependencies]
28-
shared_memory = "^0.8"
29-
3027
[build-dependencies]
3128
cmake = "0.1"
3229
wooting-analog-common = { path = "../wooting-analog-common"}

wooting-analog-test-plugin/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2018"
88

99
[dependencies]
1010
wooting-analog-plugin-dev = { path = "../wooting-analog-plugin-dev"}
11-
shared_memory = "^0.8"
11+
shared_memory = "^0.12"
1212
log = "^0.4"
1313
env_logger = "^0.8"
1414

wooting-analog-test-plugin/src/lib.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ pub struct SharedState {
4444
pub analog_values: [u8; 0xFF],
4545
}
4646

47-
unsafe impl SharedMemCast for SharedState {}
48-
4947
impl WootingAnalogTestPlugin {
5048
fn new() -> Self {
5149
if let Err(e) = env_logger::try_init() {
@@ -68,8 +66,8 @@ impl WootingAnalogTestPlugin {
6866

6967
let worker_thread = thread::spawn(move || {
7068
let link_path = std::env::temp_dir().join("wooting-test-plugin.link");
71-
let mut my_shmem = {
72-
match SharedMem::open_linked(link_path.as_os_str()) {
69+
let my_shmem = {
70+
match ShmemConf::new().size(4096).flink(link_path.as_os_str()).open() {
7371
Ok(v) => v,
7472
Err(e) => {
7573
if link_path.exists() {
@@ -79,7 +77,7 @@ impl WootingAnalogTestPlugin {
7977
error!("Could not delete old link file: {}", e);
8078
}
8179
}
82-
match SharedMem::create_linked(link_path.as_os_str(), LockType::Mutex, 4096)
80+
match ShmemConf::new().size(4096).flink(link_path.as_os_str()).create()
8381
{
8482
Ok(m) => m,
8583
Err(e) => {
@@ -93,16 +91,10 @@ impl WootingAnalogTestPlugin {
9391
}
9492
};
9593

96-
info!("{:?}", my_shmem.get_link_path());
94+
info!("{:?}", my_shmem.get_flink_path());
9795

9896
{
99-
let mut shared_state = match my_shmem.wlock::<SharedState>(0) {
100-
Ok(v) => v,
101-
Err(_) => {
102-
error!("Test plugin Failed to acquire write lock! Stopping...");
103-
return;
104-
}
105-
};
97+
let shared_state = unsafe { &mut *(my_shmem.as_ptr() as *mut u8 as *mut SharedState) };
10698
shared_state.vendor_id = 0x03eb;
10799
shared_state.product_id = 0xFFFF;
108100
shared_state.device_type = DeviceType::Keyboard;
@@ -122,13 +114,7 @@ impl WootingAnalogTestPlugin {
122114
}
123115

124116
{
125-
let mut state = match my_shmem.wlock::<SharedState>(0) {
126-
Ok(v) => v,
127-
Err(_) => {
128-
warn!("failed to get lock");
129-
continue;
130-
}
131-
};
117+
let state = unsafe { &mut *(my_shmem.as_ptr() as *mut u8 as *mut SharedState) };
132118

133119
if state.dirty_device_info || t_device.lock().unwrap().is_none() {
134120
state.dirty_device_info = false;
@@ -166,8 +152,6 @@ impl WootingAnalogTestPlugin {
166152
}
167153

168154
if !state.device_connected {
169-
//make sure we drop the state so we're not holding the lock while the thread is sleeping
170-
drop(state);
171155
thread::sleep(Duration::from_millis(500));
172156
continue;
173157
}

wooting-analog-virtual-control/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2018"
77

88
[dependencies]
99
wooting-analog-common = { path = "../wooting-analog-common"}
10-
shared_memory = "^0.8"
10+
shared_memory = "^0.12"
1111
log = "^0.4"
1212
iced = "^0.4"
1313
env_logger = "0.8"

0 commit comments

Comments
 (0)