Skip to content

Commit 1f3b6a1

Browse files
authored
Merge pull request #88 from elast0ny/fix/windows_raw_mappings
Fix logging feature & Windows bug #82
2 parents 5474dd5 + 52d966a commit 1f3b6a1

File tree

6 files changed

+192
-227
lines changed

6 files changed

+192
-227
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ nix = "0.23"
3535
libc = "0.2"
3636

3737
[target.'cfg(windows)'.dependencies]
38-
win-sys = "0"
38+
win-sys = "0.2"
3939

4040
[dev-dependencies]
4141
raw_sync = "0.1"
42-
clap = "2.33"
42+
clap = {version = "3", features = ["derive"]}
4343
env_logger = "0"

examples/basic.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
use std::thread;
22

3-
use clap::{App, Arg};
3+
use clap::Parser;
44
use shared_memory::*;
55

6+
/// Spawns N threads that increment a value to 100
7+
#[derive(Parser)]
8+
#[clap(author, version, about)]
9+
struct Args {
10+
/// Number of threads to spawn
11+
num_threads: usize,
12+
13+
/// Count to this value
14+
#[clap(long, short, default_value_t = 50)]
15+
count_to: u8,
16+
}
17+
618
fn main() {
719
env_logger::init();
20+
let args = Args::parse();
821

9-
// Get number of thread argument
10-
let matches = App::new("Basic Example")
11-
.about("Spawns N threads that increment a value to 100")
12-
.arg(
13-
Arg::with_name("num_threads")
14-
.help("Number of threads to spawn")
15-
.required(true)
16-
.takes_value(true),
17-
)
18-
.get_matches();
19-
let num_threads: usize = matches
20-
.value_of("num_threads")
21-
.unwrap()
22-
.parse()
23-
.expect("Invalid number passed for num_threads");
24-
if num_threads < 1 {
22+
if args.num_threads < 1 {
2523
eprintln!("Invalid number of threads");
2624
return;
2725
}
2826

29-
let mut threads = Vec::with_capacity(num_threads);
27+
let mut threads = Vec::with_capacity(args.num_threads);
3028
let _ = std::fs::remove_file("basic_mapping");
31-
29+
let max = args.count_to;
3230
// Spawn N threads
33-
for i in 0..num_threads {
31+
for i in 0..args.num_threads {
3432
let thread_id = i + 1;
3533
threads.push(thread::spawn(move || {
36-
increment_value("basic_mapping", thread_id);
34+
increment_value("basic_mapping", thread_id, max);
3735
}));
3836
}
3937

@@ -44,7 +42,7 @@ fn main() {
4442
}
4543

4644
/// Increments a value that lives in shared memory
47-
fn increment_value(shmem_flink: &str, thread_num: usize) {
45+
fn increment_value(shmem_flink: &str, thread_num: usize, max: u8) {
4846
// Create or open the shared memory mapping
4947
let shmem = match ShmemConf::new().size(4096).flink(shmem_flink).create() {
5048
Ok(m) => m,
@@ -63,7 +61,7 @@ fn increment_value(shmem_flink: &str, thread_num: usize) {
6361

6462
// WARNING: This is prone to race conditions as no sync/locking is used
6563
unsafe {
66-
while std::ptr::read_volatile(raw_ptr) < 100 {
64+
while std::ptr::read_volatile(raw_ptr) < max {
6765
// Increment shared value by one
6866
*raw_ptr += 1;
6967

examples/mutex.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
use std::sync::atomic::{AtomicU8, Ordering};
22
use std::thread;
33

4-
use clap::{App, Arg};
4+
use clap::Parser;
55
use raw_sync::locks::*;
66
use shared_memory::*;
77

8+
/// Spawns N threads that increment a value to 10 using a mutex
9+
#[derive(Parser)]
10+
#[clap(author, version, about)]
11+
struct Args {
12+
/// Number of threads to spawn
13+
num_threads: usize,
14+
15+
/// Count to this value
16+
#[clap(long, short, default_value_t = 50)]
17+
count_to: u8,
18+
}
19+
820
fn main() {
921
env_logger::init();
10-
let matches = App::new("Mutex Example")
11-
.about("Spawns N threads that increment a value to 10 using a mutex")
12-
.arg(
13-
Arg::with_name("num_threads")
14-
.help("Number of threads to spawn")
15-
.required(true)
16-
.takes_value(true),
17-
)
18-
.get_matches();
22+
let args = Args::parse();
1923

20-
let num_threads: usize = matches
21-
.value_of("num_threads")
22-
.unwrap()
23-
.parse()
24-
.expect("Invalid number passed for num_threads");
25-
if num_threads < 1 {
24+
if args.num_threads < 1 {
2625
eprintln!("num_threads should be 2 or more");
2726
return;
2827
}
29-
let mut threads = Vec::with_capacity(num_threads);
28+
let mut threads = Vec::with_capacity(args.num_threads);
3029
let _ = std::fs::remove_file("mutex_mapping");
3130

3231
// Spawn N threads
33-
for i in 0..num_threads {
32+
for i in 0..args.num_threads {
3433
let thread_id = i + 1;
3534
threads.push(thread::spawn(move || {
3635
increment_value("mutex_mapping", thread_id);

src/lib.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ use std::path::{Path, PathBuf};
1010

1111
use cfg_if::cfg_if;
1212

13-
#[cfg(feature = "logging")]
14-
pub use log;
15-
#[cfg(not(feature = "logging"))]
16-
#[allow(unused_macros)]
17-
mod log {
18-
macro_rules! trace (($($tt:tt)*) => {{}});
19-
macro_rules! debug (($($tt:tt)*) => {{}});
20-
macro_rules! info (($($tt:tt)*) => {{}});
21-
macro_rules! warn (($($tt:tt)*) => {{}});
22-
macro_rules! error (($($tt:tt)*) => {{}});
23-
pub(crate) use {debug, trace};
13+
cfg_if! {
14+
if #[cfg(feature = "logging")] {
15+
pub use log;
16+
} else {
17+
#[allow(unused_macros)]
18+
mod log {
19+
macro_rules! trace (($($tt:tt)*) => {{}});
20+
macro_rules! debug (($($tt:tt)*) => {{}});
21+
macro_rules! info (($($tt:tt)*) => {{}});
22+
macro_rules! warn (($($tt:tt)*) => {{}});
23+
macro_rules! error (($($tt:tt)*) => {{}});
24+
pub(crate) use {debug, trace};
25+
}
26+
}
2427
}
2528

2629
use crate::log::*;
@@ -102,6 +105,12 @@ impl ShmemConf {
102105
return Err(ShmemError::MapSizeZero);
103106
}
104107

108+
if let Some(ref flink_path) = self.flink_path {
109+
if !self.overwrite_flink && flink_path.is_file() {
110+
return Err(ShmemError::LinkExists);
111+
}
112+
}
113+
105114
// Create the mapping
106115
let mapping = match self.os_id {
107116
None => {
@@ -250,7 +259,7 @@ impl Shmem {
250259
}
251260
/// Returns a raw pointer to the mapping
252261
pub fn as_ptr(&self) -> *mut u8 {
253-
self.mapping.map_ptr
262+
self.mapping.as_mut_ptr()
254263
}
255264
/// Returns mapping as a byte slice
256265
/// # Safety

src/unix.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ pub struct MapData {
2424
pub map_ptr: *mut u8,
2525
}
2626

27+
impl MapData {
28+
pub fn as_mut_ptr(&self) -> *mut u8 {
29+
self.map_ptr
30+
}
31+
}
32+
2733
/// Shared memory teardown for linux
2834
impl Drop for MapData {
2935
///Takes care of properly closing the SharedMem (munmap(), shmem_unlink(), close())

0 commit comments

Comments
 (0)