-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel_streams.c
283 lines (206 loc) · 4.5 KB
/
kernel_streams.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "util.h"
#include "tinyos.h"
#include "kernel_cc.h"
#include "kernel_streams.h"
#include "kernel_sched.h"
#include "kernel_proc.h"
#define MAX_FILES MAX_PROC
FCB FT[MAX_FILES];
rlnode FCB_freelist;
void initialize_files()
{
rlnode_init(&FCB_freelist,NULL);
for(int i=0;i<MAX_FILES;i++) {
FT[i].refcount = 0;
rlnode_init(& FT[i].freelist_node, &FT[i]);
rlist_push_back(&FCB_freelist, & FT[i].freelist_node);
}
}
FCB* acquire_FCB()
{
if(! is_rlist_empty(& FCB_freelist)) {
FCB* fcb = rlist_pop_front(& FCB_freelist)->fcb;
fcb->refcount = 0;
return fcb;
}
else
return NULL;
}
void release_FCB(FCB* fcb)
{
rlist_push_back(& FCB_freelist, & fcb->freelist_node);
}
void FCB_incref(FCB* fcb)
{
assert(fcb);
fcb->refcount++;
}
int FCB_decref(FCB* fcb)
{
assert(fcb);
fcb->refcount --;
if(fcb->refcount==0) {
int retval = fcb->streamfunc->Close(fcb->streamobj);
release_FCB(fcb);
return retval;
}
else
return 0;
}
int FCB_reserve(size_t num, Fid_t *fid, FCB** fcb)
{
PCB* cur = CURPROC;
size_t f=0;
uint i;
/* Find distinct fids */
for(i=0; i<num; i++) {
while(f<MAX_FILEID && cur->FIDT[f]!=NULL)
f++;
if(f==MAX_FILEID) break;
fid[i] = f; f++;
}
if(i<num) return 0;
/* Allocate FCBs */
for(i=0;i<num;i++)
if((fcb[i] = acquire_FCB()) == NULL)
break;
if(i<num) {
/* Roll back */
while(i>0) {
release_FCB(fcb[i-1]);
i--;
}
return 0;
}
/* Found all */
for(i=0;i<num;i++) {
cur->FIDT[fid[i]]=fcb[i];
FCB_incref(fcb[i]);
}
return 1;
}
void FCB_unreserve(size_t num, Fid_t *fid, FCB** fcb)
{
PCB* cur = CURPROC;
for(size_t i=0; i<num ; i++) {
assert(cur->FIDT[fid[i]]==fcb[i]);
cur->FIDT[fid[i]] = NULL;
release_FCB(fcb[i]);
}
}
/*
*
* I/O routines
*
*/
FCB* get_fcb(Fid_t fid)
{
if(fid < 0 || fid >= MAX_FILEID) return NULL;
return CURPROC->FIDT[fid];
}
int sys_Read(Fid_t fd, char *buf, unsigned int size)
{
int retcode = -1;
int (*devread)(void*,char*,uint);
void* sobj;
/* Get the fields from the stream */
FCB* fcb = get_fcb(fd);
if(fcb) {
sobj = fcb->streamobj;
devread = fcb->streamfunc->Read;
/* make sure that the stream will not be closed (by another thread)
while we are using it! */
FCB_incref(fcb);
if(devread)
retcode = devread(sobj, buf, size);
/* Need to decrease the reference to FCB */
FCB_decref(fcb);
}
/* We must not go into non-preemptive domain with kernel_mutex locked */
return retcode;
}
int sys_Write(Fid_t fd, const char *buf, unsigned int size)
{
int retcode = -1;
int (*devwrite)(void*, const char*, uint) = NULL;
void* sobj = NULL;
/* Get the fields from the stream */
FCB* fcb = get_fcb(fd);
if(fcb) {
sobj = fcb->streamobj;
devwrite = fcb->streamfunc->Write;
/* make sure that the stream will not be closed (by another thread)
while we are using it! */
FCB_incref(fcb);
if(devwrite)
retcode = devwrite(sobj, buf, size);
/* Need to decrease the reference to FCB */
FCB_decref(fcb);
}
return retcode;
}
int sys_Close(int fd)
{
int retcode = (fd>=0 && fd<MAX_FILEID) ? 0 : -1; /* Closing a closed fd is legal! */
FCB* fcb = get_fcb(fd);
if(fcb) {
CURPROC->FIDT[fd] = NULL;
retcode = FCB_decref(fcb);
}
return retcode;
}
/*
Copy file descriptor oldfd into file descriptor newfd.
This call returns 0 on success and -1 on failure.
Possible reasons for failure:
- Either oldfd or newfd is invalid.
*/
int sys_Dup2(int oldfd, int newfd)
{
int retcode=0;
if(oldfd<0 || newfd<0 || oldfd>=MAX_FILEID || newfd>=MAX_FILEID)
return -1;
FCB* old = get_fcb(oldfd);
FCB* new = get_fcb(newfd);
if(old==NULL) {
retcode = -1;
}
else if(old!=new) {
if(new)
FCB_decref(new);
FCB_incref(old);
CURPROC->FIDT[newfd] = old;
}
return retcode;
}
unsigned int sys_GetTerminalDevices()
{
return device_no(DEV_SERIAL);
}
/**
Open a stream for the given device.
*/
Fid_t open_stream(Device_type major, unsigned int minor)
{
Fid_t fid;
FCB* fcb;
if(! FCB_reserve(1, &fid, &fcb))
goto finerr;
if(device_open(major, minor, & fcb->streamobj, &fcb->streamfunc)) {
FCB_unreserve(1, &fid, &fcb);
goto finerr;
}
goto finok;
finerr:
fid = NOFILE;
finok:
return fid;
}
int sys_OpenNull()
{
return open_stream(DEV_NULL, 0);
}
Fid_t sys_OpenTerminal(unsigned int termno)
{
return open_stream(DEV_SERIAL, termno);
}