-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_generic.c
executable file
·264 lines (220 loc) · 5.11 KB
/
sys_generic.c
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <sys/queue.h>
struct myprocess {
LIST_ENTRY(myprocess) link;
pid_t id;
int waiting;
};
struct mymutex {
LIST_ENTRY(mymutex) link;
int d;
char *name;
LIST_HEAD(, myprocess) plist;
int is_locked;
};
int t_pause;
pid_t wake_pid;
LIST_HEAD(, mymutex) mlist;
int mlist_is_init;
int sys_mtxopen(struct proc *p, void *v, register_t *retval)
{
if(mlist_is_init == 0) {
LIST_INIT(&mlist);
mlist_is_init = 1;
}
struct sys_mtxopen_args *uap = v;
const int KMSGMAX = 10;
char * kname = malloc(KMSGMAX, M_TEMP, M_WAITOK);
size_t done;
copyinstr(SCARG(uap, name), kname, KMSGMAX, &done);
struct myprocess *pitem = malloc(sizeof(struct myprocess), M_TEMP, M_WAITOK);
pitem->id = p->p_tid;
pitem->waiting = 0;
struct mymutex *found = NULL;
int maxdescriptor = 0;
struct mymutex *it;
LIST_FOREACH(it, &mlist, link) {
if(it->d > maxdescriptor) {
maxdescriptor = it->d;
}
if(strcmp(it->name, kname) == 0) {
found = it;
}
}
if(found != NULL) {
printf("CREAT: found mutex name %s, already openend as %d\n", kname, found->d);
LIST_INSERT_HEAD(&(found->plist), pitem, link);
*retval = found->d;
return 0;
}
struct mymutex *item = malloc(sizeof(struct mymutex), M_TEMP, M_WAITOK);
item->d = maxdescriptor + 1;
item->name = malloc(KMSGMAX, M_TEMP, M_WAITOK);
kcopy(kname, item->name, KMSGMAX);
item->is_locked = 0;
printf("CREAT: not found, created new mutex name %s, descriptor %d\n", kname, item->d);
LIST_INSERT_HEAD(&mlist, item, link);
LIST_INIT(&(item->plist));
LIST_INSERT_HEAD(&(item->plist), pitem, link);
*retval = item->d;
return 0;
}
int sys_mtxclose(struct proc *p, void *v, register_t *retval)
{
if(mlist_is_init == 0) {
LIST_INIT(&mlist);
mlist_is_init = 1;
}
pid_t thread_id = p->p_tid;
struct sys_mtxclose_args *uap = v;
int d = SCARG(uap, d);
struct mymutex *varmutex;
LIST_FOREACH(varmutex, &mlist, link) {
if (varmutex->d == d) {
struct myprocess *varprocess;
LIST_FOREACH(varprocess, &(varmutex->plist), link) {
if (varprocess->id == thread_id) {
LIST_REMOVE(varprocess, link);
break;
}
}
if (LIST_EMPTY((&varmutex->plist))) {
LIST_REMOVE(varmutex, link);
}
break;
}
}
*retval = 0;
return 0;
}
int sys_mtxlock(struct proc *p, void *v, register_t *retval)
{
int error = 0;
if(mlist_is_init == 0) {
LIST_INIT(&mlist);
mlist_is_init = 1;
}
struct sys_mtxlock_args *uap = v;
int d = SCARG(uap, d);
struct mymutex *ifound = NULL;
struct mymutex *it;
LIST_FOREACH(it, &mlist, link) {
if (it->d == d) {
ifound = it;
break;
}
}
if(ifound == NULL) {
printf("LOCK: no such descriptor %d\n", d);
*retval = -1;
return 0;
}
printf("LOCK: mutex to be locked name %s, descriptor %d\n", ifound->name, ifound->d);
struct myprocess *jfound = NULL;
struct myprocess *jt;
LIST_FOREACH(jt, &(ifound->plist), link) {
if (jt->id == p->p_tid) {
jfound = jt;
break;
}
}
if(jfound == NULL) {
printf("LOCK: proc %d has not opened mutex name %s\n", d, ifound->name);
*retval = -1;
return 0;
}
printf("LOCK: proc %d start waiting mutex %s\n", jfound->id, ifound->name);
jfound->waiting = 1;
// should check mutex not in use (is_locked == 0) to skip sleep
sleep:
error = tsleep(&t_pause, PSOCK | PCATCH, "awaiting lock", 0);
if(error) {
jfound->waiting = 0;
*retval = -1;
return error;
}
if(wake_pid != p->p_tid) {
goto sleep;
}
wake_pid = 0;
jfound->waiting = 0;
*retval = 0;
return 0;
}
int sys_mtxunlock(struct proc *p, void *v, register_t *retval)
{
if(mlist_is_init == 0) {
LIST_INIT(&mlist);
mlist_is_init = 1;
}
struct sys_mtxclose_args *uap = v;
int d = SCARG(uap, d);
struct mymutex *varmutex;
LIST_FOREACH(varmutex, &mlist, link) {
if (varmutex->d == d) {
// should check mutex d actually opened by proc p?
varmutex->is_locked = 0;
break;
}
}
*retval = 0;
return 0;
}
int sys_mtxlist(struct proc *p, void *v, register_t *retval)
{
if(mlist_is_init == 0) {
LIST_INIT(&mlist);
mlist_is_init = 1;
}
struct sys_mtxlist_args *uap = v;
int *mtx_info = SCARG(uap, d);
pid_t *ps_info = SCARG(uap, pidlist);
int no_mutex_to_send = SCARG(uap, nlist);
// should check allocated memory is of actual size (nlist)
int i = 0;
struct mymutex *varmutex;
LIST_FOREACH(varmutex, &mlist, link)
{
if(varmutex -> is_locked == 0)
{
struct myprocess *iterproc;
LIST_FOREACH(iterproc, &(varmutex->plist), link)
{
if(iterproc -> waiting == 1)
{
if(i >= no_mutex_to_send)
{
*retval = -1;
return 0;
}
mtx_info[i] = varmutex -> d;
ps_info[i] = iterproc -> id;
i++;
}
}
}
}
*retval = i;
return 0;
}
int sys_mtxgrant(struct proc *p, void *v, register_t *retval)
{
if(mlist_is_init == 0) {
LIST_INIT(&mlist);
mlist_is_init = 1;
}
struct sys_mtxgrant_args *arg = v;
pid_t my_ps = SCARG(arg, selected);
int associated_mutex = SCARG(arg, d);
struct mymutex *it;
LIST_FOREACH(it, &mlist, link) {
if (it->d == associated_mutex) {
it->is_locked = 1;
break;
}
}
printf("GRANT: mutex id %d to proc %d\n", associated_mutex, my_ps);
wake_pid = my_ps;
wakeup(&t_pause);
*retval = 0;
return 0;
}