-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: philosophers: Add dynamic semaphore example
Add a sample that implements the forks using dynamically allocate semaphores. Signed-off-by: David Brown <[email protected]>
- Loading branch information
Showing
4 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2023 Linaro LTD | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Semaphore based sync. | ||
//! | ||
//! This is the simplest type of sync, which uses a single semaphore per fork. | ||
extern crate alloc; | ||
|
||
use alloc::vec::Vec; | ||
use alloc::boxed::Box; | ||
|
||
use zephyr::{ | ||
sync::Arc, sys::sync::Semaphore, time::Forever | ||
}; | ||
|
||
use crate::{ForkSync, NUM_PHIL}; | ||
|
||
#[derive(Debug)] | ||
pub struct SemSync { | ||
/// The forks for this philosopher. This is a big excessive, as we really don't need all of | ||
/// them, but the ForSync code uses the index here. | ||
forks: [Arc<Semaphore>; NUM_PHIL], | ||
} | ||
|
||
impl ForkSync for SemSync { | ||
fn take(&self, index: usize) { | ||
self.forks[index].take(Forever).unwrap(); | ||
} | ||
|
||
fn release(&self, index: usize) { | ||
self.forks[index].give(); | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn dyn_semaphore_sync() -> Vec<Arc<dyn ForkSync>> { | ||
let forks = [(); NUM_PHIL].each_ref().map(|()| { | ||
Arc::new(Semaphore::new(1, 1).unwrap()) | ||
}); | ||
|
||
let syncers = (0..NUM_PHIL).map(|_| { | ||
let syncer = SemSync { | ||
forks: forks.clone(), | ||
}; | ||
let item = Box::new(syncer) as Box<dyn ForkSync>; | ||
Arc::from(item) | ||
}).collect(); | ||
|
||
syncers | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters