-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel_pipe.c
252 lines (189 loc) · 5.24 KB
/
kernel_pipe.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
#include "tinyos.h"
#include "kernel_dev.h"
#include "kernel_cc.h"
#include "kernel_streams.h"
#include "util.h"
#include "kernel_pipe.h"
static file_ops reader_pipe_ops = {
//.Open = rpipe_Open,
.Read = reader_pipe_Read,
.Write = reader_pipe_Write,
.Close = reader_pipe_Close
};
static file_ops writer_pipe_ops = {
//.Open = rpipe_Open,
.Read = writer_pipe_Read,
.Write = writer_pipe_Write,
.Close = writer_pipe_Close
};
Pipe_cb* create_pipe_cb(FCB* reader, FCB* writer){
Pipe_cb* pipe_cb = (Pipe_cb*)xmalloc(sizeof(Pipe_cb));
pipe_cb->BUFFER = (char *)malloc(PIPE_BUFFER_SIZE*sizeof(char));
pipe_cb->buf_size = PIPE_BUFFER_SIZE;
pipe_cb->reader= reader;
pipe_cb->writer= writer;
pipe_cb->w_position = 0; //pipe_cb->BUFFER[0]
pipe_cb->r_position = 0; //pipe_cb->BUFFER[0]
pipe_cb->has_space = COND_INIT;
pipe_cb->has_data = COND_INIT;
pipe_cb->reader->streamobj = pipe_cb; //set pipe reader object
pipe_cb->reader->streamfunc = &reader_pipe_ops;
pipe_cb->writer->streamobj = pipe_cb; //set pipe writer object
pipe_cb->writer->streamfunc = &writer_pipe_ops;
return pipe_cb;
}
int sys_Pipe(pipe_t* pipe)
{
//FCB_reserve
Fid_t fidt_buffer[2];
FCB* fcb_buffer[2];
int reserve_flag;
reserve_flag = FCB_reserve(2, fidt_buffer, fcb_buffer);
if(reserve_flag == 0){
return -1;
}
pipe->read =fidt_buffer[1];
pipe->write =fidt_buffer[0];
// Creating Pipe_cb with the FCBs
create_pipe_cb(fcb_buffer[1], fcb_buffer[0]);
return 0;
}
int Pipe_close(Fid_t close_f){
int writer_flag = 0;
int reader_flag = 0;
FCB* close_fcb = get_fcb(close_f);
Pipe_cb* pipe_cb = close_fcb->streamobj;
// Wake up pipe_writer and pipe_reader
kernel_broadcast(&pipe_cb->has_space);
kernel_broadcast(&pipe_cb->has_data);
if(close_fcb->refcount == 0) {
free(close_fcb);
}
if(pipe_cb->reader == NULL && pipe_cb->writer == NULL) {
free(pipe_cb);
}
// If pipe reader has no more dependencies close it
if(pipe_cb->reader->refcount == 0) {
reader_pipe_Close(pipe_cb);
reader_flag = 1;
}
// If pipe writer has no more dependencies close it
if(pipe_cb->writer->refcount == 0) {
writer_pipe_Close(pipe_cb);
writer_flag = 1;
}
//Eventually,if pipe's reader & writer are no more, free pipe
if(writer_flag == 1 && reader_flag == 1) {
free(pipe_cb);
return 0;
}
return -1;
}
//______________ PIPEEE_FUNCTIONSSSS _____________//
//=====================================================
//______________ READER_PIPE_FUNCTIONS _____________//
int reader_pipe_Read(void* pipecb_t, char *buf, unsigned int n){
if(pipecb_t == NULL ){
return -1;
}
if(buf == NULL ){
return -1;
}
if(n < 0 ){
return -1;
}
Pipe_cb* pipe_cb = (Pipe_cb*)pipecb_t;
int writer_exists; //data able to read.
int has_read = 0; //index pos in Reader's Buffer.
if (pipe_cb == NULL){
return -1;
}
if(pipe_cb->writer == NULL){
writer_exists = 0;
}else{
writer_exists = 1;
}
//While Buffer is Empty
while(pipe_cb->w_position == pipe_cb->r_position){
if(!writer_exists){
return 0;
}else
{
kernel_wait(&pipe_cb->has_data,SCHED_PIPE);
}
}
//Read Data From Buffer.
do {
buf[has_read++] = pipe_cb->BUFFER[pipe_cb->r_position];
pipe_cb->r_position = (pipe_cb->r_position + 1) % PIPE_BUFFER_SIZE;
} while((pipe_cb->r_position != pipe_cb->w_position) && (has_read < n));
////wake up all the writers waiting
kernel_broadcast(&pipe_cb->has_space);
return has_read;
}
int reader_pipe_Write(void* pipecb_t, const char* buf, unsigned int n){
return -1; //reader can not write
}
int reader_pipe_Close(void* pipecb_t){
if(pipecb_t == NULL){
return -1;
}
Pipe_cb* pipe_cb = (Pipe_cb*)pipecb_t;
if(pipe_cb!=NULL){
//wake upp all the writer/readers before closing this pipe
kernel_broadcast(&pipe_cb->has_data);
pipe_cb->reader = NULL;
return 0;
}
return -1;
}
//=====================================================
//______________ WRITER_PIPE_FUNCTIONS _____________//
//=====================================================
int writer_pipe_Read(void* pipecb_t, char *buf, unsigned int n){
return -1;//writer can not read
}
int writer_pipe_Write(void* pipecb_t, const char* buf, unsigned int n){
if(pipecb_t == NULL) {
return -1;
}
Pipe_cb* pipe_cb = (Pipe_cb*)pipecb_t;
int has_write = 0; //Position of index in Reader's Buffer.
if(pipe_cb == NULL ){
return -1;
}
if(pipe_cb->reader == NULL || pipe_cb->writer == NULL){
return -1;
}
if(buf == NULL){
return -1;
}
if(n<0){
return -1;
}
while(pipe_cb->r_position == (pipe_cb->w_position+1)%PIPE_BUFFER_SIZE){
//wait until has data flowing on Stream.
kernel_wait(&pipe_cb->has_space,SCHED_PIPE);
}
//Write data
do {
pipe_cb->BUFFER[pipe_cb->w_position] = buf[has_write++];
pipe_cb->w_position = (pipe_cb->w_position + 1) % PIPE_BUFFER_SIZE;
} while(pipe_cb->r_position != (pipe_cb->w_position + 1) % PIPE_BUFFER_SIZE && (has_write < n));
//wake up all the readers waiting
kernel_broadcast(&pipe_cb->has_data);
return has_write;
}
int writer_pipe_Close(void* pipecb_t){
if(pipecb_t == NULL){
return -1;
}
Pipe_cb* pipe_cb = (Pipe_cb*) pipecb_t;
if(pipe_cb!=NULL){
//wake upp all the writer/readers before closing this pipe
kernel_broadcast(&pipe_cb->has_space);
pipe_cb->writer = NULL;
return 0;
}
return -1;
}