Skip to content

Commit f97d58e

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 f97d58e

File tree

9 files changed

+103
-172
lines changed

9 files changed

+103
-172
lines changed

Cargo.lock

Lines changed: 65 additions & 65 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ wooting-analog-common = { path = "../wooting-analog-common"}
2525
wooting-analog-plugin-dev = { path = "../wooting-analog-plugin-dev"}
2626

2727
[dev-dependencies]
28-
shared_memory = "^0.8"
28+
shared_memory = "^0.12"
2929

3030
[build-dependencies]
3131
cmake = "0.1"

wooting-analog-sdk/src/ffi.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,7 @@ pub extern "C" fn wooting_analog_read_full_buffer_device(
353353
mod tests {
354354
use super::*;
355355
use crate::keycode::hid_to_code;
356-
use shared_memory::{
357-
ReadLockGuard, ReadLockable, SharedMem, SharedMemCast, WriteLockGuard, WriteLockable,
358-
};
356+
use shared_memory::ShmemConf;
359357

360358
use std::sync::{Arc, MutexGuard};
361359
use std::time::Duration;
@@ -379,8 +377,6 @@ mod tests {
379377
pub analog_values: [u8; 0xFF],
380378
}
381379

382-
unsafe impl SharedMemCast for SharedState {}
383-
384380
pub fn get_sdk() -> MutexGuard<'static, AnalogSDK> {
385381
ANALOG_SDK.lock().unwrap()
386382
}
@@ -409,20 +405,6 @@ mod tests {
409405
info!("Got {:?} after {} attempts", connected, n);
410406
}
411407

412-
fn get_wlock(shmem: &mut SharedMem) -> WriteLockGuard<SharedState> {
413-
match shmem.wlock::<SharedState>(0) {
414-
Ok(v) => v,
415-
Err(_) => panic!("Failed to acquire write lock !"),
416-
}
417-
}
418-
419-
fn get_rlock(shmem: &mut SharedMem) -> ReadLockGuard<SharedState> {
420-
match shmem.rlock::<SharedState>(0) {
421-
Ok(v) => v,
422-
Err(_) => panic!("Failed to acquire write lock !"),
423-
}
424-
}
425-
426408
fn shared_init() {
427409
env_logger::try_init_from_env(env_logger::Env::from("trace"))
428410
.map_err(|e| println!("ERROR: Could not initialise env_logger. '{:?}'", e));
@@ -455,11 +437,11 @@ mod tests {
455437
//Wait a slight bit to ensure that the test-plugin worker thread has initialised the shared mem
456438
::std::thread::sleep(Duration::from_millis(500));
457439

458-
let mut shmem = match SharedMem::open_linked(
440+
let mut shmem = match ShmemConf::new().size(4096).flink(
459441
std::env::temp_dir()
460442
.join("wooting-test-plugin.link")
461443
.as_os_str(),
462-
) {
444+
).open() {
463445
Ok(v) => v,
464446
Err(e) => {
465447
println!("Error : {}", e);
@@ -474,7 +456,7 @@ mod tests {
474456
//Check the connected cb is called
475457
{
476458
{
477-
let mut shared_state = get_wlock(&mut shmem);
459+
let shared_state = unsafe { &mut *(shmem.as_ptr() as *mut u8 as *mut SharedState) };
478460
shared_state.device_connected = true;
479461
}
480462
wait_for_connected(5, true);
@@ -506,7 +488,7 @@ mod tests {
506488
//Check the cb is called with disconnected
507489
{
508490
{
509-
let mut shared_state = get_wlock(&mut shmem);
491+
let shared_state = unsafe { &mut *(shmem.as_ptr() as *mut u8 as *mut SharedState) };
510492
shared_state.device_connected = false;
511493
}
512494
wait_for_connected(5, false);
@@ -529,7 +511,7 @@ mod tests {
529511
let analog_key = 5;
530512
//Connect the device again, set a keycode to a val
531513
let device_id = {
532-
let mut shared_state = get_wlock(&mut shmem);
514+
let shared_state = unsafe { &mut *(shmem.as_ptr() as *mut u8 as *mut SharedState) };
533515
shared_state.analog_values[analog_key] = analog_val;
534516
shared_state.device_connected = true;
535517
1
@@ -632,7 +614,7 @@ mod tests {
632614
wooting_analog_set_keycode_mode(mode.clone() as u32);
633615

634616
{
635-
let mut shared_state = get_wlock(&mut shmem);
617+
let shared_state = unsafe { &mut *(shmem.as_ptr() as *mut u8 as *mut SharedState) };
636618
shared_state.analog_values[analog_key] = 0;
637619
}
638620
::std::thread::sleep(Duration::from_secs(1));
@@ -668,7 +650,7 @@ mod tests {
668650
WootingAnalogResult::Ok
669651
);
670652
{
671-
let mut shared_state = get_wlock(&mut shmem);
653+
let shared_state = unsafe { &mut *(shmem.as_ptr() as *mut u8 as *mut SharedState) };
672654
shared_state.device_connected = false;
673655
}
674656
::std::thread::sleep(Duration::from_secs(1));

wooting-analog-sdk/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ extern crate wooting_analog_common;
1515
extern crate wooting_analog_plugin_dev;
1616

1717
#[cfg(test)]
18-
#[macro_use]
1918
extern crate shared_memory;
2019

2120
//library modules

0 commit comments

Comments
 (0)