1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![deny(missing_docs)]
mod semary;
mod shared_mem;
pub use self::semary::*;
pub use self::shared_mem::*;
use alloc::collections::BTreeMap;
use alloc::sync::Arc;
use bitflags::*;
use lock::Mutex;
#[derive(Default)]
pub struct SemProc {
arrays: BTreeMap<SemId, Arc<SemArray>>,
undos: BTreeMap<(SemId, SemNum), SemOp>,
}
#[derive(Default, Clone)]
pub struct ShmProc {
shm_identifiers: BTreeMap<ShmId, ShmIdentifier>,
}
bitflags! {
struct IpcGetFlag: usize {
const CREAT = 1 << 9;
const EXCLUSIVE = 1 << 10;
const NO_WAIT = 1 << 11;
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct IpcPerm {
pub key: u32,
pub uid: u32,
pub gid: u32,
pub cuid: u32,
pub cgid: u32,
pub mode: u32,
pub __seq: u32,
pub __pad1: usize,
pub __pad2: usize,
}
type SemId = usize;
type ShmId = usize;
type SemNum = u16;
type SemOp = i16;
impl SemProc {
pub fn add(&mut self, array: Arc<SemArray>) -> SemId {
let id = self.get_free_id();
self.arrays.insert(id, array);
id
}
pub fn remove(&mut self, id: SemId) {
self.arrays.remove(&id);
}
fn get_free_id(&self) -> SemId {
(0..).find(|i| self.arrays.get(i).is_none()).unwrap()
}
pub fn get(&self, id: SemId) -> Option<Arc<SemArray>> {
self.arrays.get(&id).cloned()
}
pub fn add_undo(&mut self, id: SemId, num: SemNum, op: SemOp) {
let old_val = *self.undos.get(&(id, num)).unwrap_or(&0);
let new_val = old_val - op;
self.undos.insert((id, num), new_val);
}
}
impl Clone for SemProc {
fn clone(&self) -> Self {
SemProc {
arrays: self.arrays.clone(),
undos: BTreeMap::default(),
}
}
}
impl Drop for SemProc {
fn drop(&mut self) {
for (&(id, num), &op) in self.undos.iter() {
debug!("semundo: id: {}, num: {}, op: {}", id, num, op);
let sem_array = self.arrays[&id].clone();
let sem = &sem_array[num as usize];
match op {
1 => sem.release(),
0 => {}
_ => unimplemented!("Semaphore: semundo.(Not 1)"),
}
}
}
}
impl ShmProc {
pub fn add(&mut self, shared_guard: Arc<Mutex<ShmGuard>>) -> ShmId {
let id = self.get_free_id();
let shm_identifier = ShmIdentifier {
addr: 0,
guard: shared_guard,
};
self.shm_identifiers.insert(id, shm_identifier);
id
}
fn get_free_id(&self) -> ShmId {
(0..)
.find(|i| self.shm_identifiers.get(i).is_none())
.unwrap()
}
pub fn get(&self, id: ShmId) -> Option<ShmIdentifier> {
self.shm_identifiers.get(&id).cloned()
}
pub fn set(&mut self, id: ShmId, shm_id: ShmIdentifier) {
self.shm_identifiers.insert(id, shm_id);
}
pub fn get_id(&self, addr: usize) -> Option<ShmId> {
for (key, value) in &self.shm_identifiers {
if value.addr == addr {
return Some(*key);
}
}
None
}
pub fn pop(&mut self, id: ShmId) {
self.shm_identifiers.remove(&id);
}
}