update mpsc_sem
This commit is contained in:
parent
29ef0a52d5
commit
ffe22a57fb
1 changed files with 8 additions and 8 deletions
|
@ -14,7 +14,7 @@ use user_lib::{thread_create, waittid};
|
||||||
|
|
||||||
const SEM_MUTEX: usize = 0;
|
const SEM_MUTEX: usize = 0;
|
||||||
const SEM_EMPTY: usize = 1;
|
const SEM_EMPTY: usize = 1;
|
||||||
const SEM_EXISTED: usize = 2;
|
const SEM_AVAIL: usize = 2;
|
||||||
const BUFFER_SIZE: usize = 8;
|
const BUFFER_SIZE: usize = 8;
|
||||||
static mut BUFFER: [usize; BUFFER_SIZE] = [0; BUFFER_SIZE];
|
static mut BUFFER: [usize; BUFFER_SIZE] = [0; BUFFER_SIZE];
|
||||||
static mut FRONT: usize = 0;
|
static mut FRONT: usize = 0;
|
||||||
|
@ -27,20 +27,20 @@ unsafe fn producer(id: *const usize) -> ! {
|
||||||
for _ in 0..NUMBER_PER_PRODUCER {
|
for _ in 0..NUMBER_PER_PRODUCER {
|
||||||
semaphore_down(SEM_EMPTY);
|
semaphore_down(SEM_EMPTY);
|
||||||
semaphore_down(SEM_MUTEX);
|
semaphore_down(SEM_MUTEX);
|
||||||
BUFFER[FRONT] = id;
|
BUFFER[TAIL] = id;
|
||||||
FRONT = (FRONT + 1) % BUFFER_SIZE;
|
TAIL = (TAIL + 1) % BUFFER_SIZE;
|
||||||
semaphore_up(SEM_MUTEX);
|
semaphore_up(SEM_MUTEX);
|
||||||
semaphore_up(SEM_EXISTED);
|
semaphore_up(SEM_AVAIL);
|
||||||
}
|
}
|
||||||
exit(0)
|
exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn consumer() -> ! {
|
unsafe fn consumer() -> ! {
|
||||||
for _ in 0..PRODUCER_COUNT * NUMBER_PER_PRODUCER {
|
for _ in 0..PRODUCER_COUNT * NUMBER_PER_PRODUCER {
|
||||||
semaphore_down(SEM_EXISTED);
|
semaphore_down(SEM_AVAIL);
|
||||||
semaphore_down(SEM_MUTEX);
|
semaphore_down(SEM_MUTEX);
|
||||||
print!("{} ", BUFFER[TAIL]);
|
print!("{} ", BUFFER[FRONT]);
|
||||||
TAIL = (TAIL + 1) % BUFFER_SIZE;
|
FRONT = (FRONT + 1) % BUFFER_SIZE;
|
||||||
semaphore_up(SEM_MUTEX);
|
semaphore_up(SEM_MUTEX);
|
||||||
semaphore_up(SEM_EMPTY);
|
semaphore_up(SEM_EMPTY);
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ pub fn main() -> i32 {
|
||||||
// create semaphores
|
// create semaphores
|
||||||
assert_eq!(semaphore_create(1) as usize, SEM_MUTEX);
|
assert_eq!(semaphore_create(1) as usize, SEM_MUTEX);
|
||||||
assert_eq!(semaphore_create(BUFFER_SIZE) as usize, SEM_EMPTY);
|
assert_eq!(semaphore_create(BUFFER_SIZE) as usize, SEM_EMPTY);
|
||||||
assert_eq!(semaphore_create(0) as usize, SEM_EXISTED);
|
assert_eq!(semaphore_create(0) as usize, SEM_AVAIL);
|
||||||
// create threads
|
// create threads
|
||||||
let ids: Vec<_> = (0..PRODUCER_COUNT).collect();
|
let ids: Vec<_> = (0..PRODUCER_COUNT).collect();
|
||||||
let mut threads = Vec::new();
|
let mut threads = Vec::new();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue