-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
executable file
·152 lines (124 loc) · 3.22 KB
/
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
/**
* Immortal Operating System - immortalOS
*
*/
#include "pipe.h"
#include "kernel.h"
#include "config.h"
#include "types.h"
#include "memory.h"
extern u_int tasks_installed;
/*
void create_pipe(pipe_t *pipe)
{
pipe->pos_read_pipe = 0;
pipe->pos_write_pipe = 0;
pipe->pipe_itens = 0;
pipe->pos_bloqued_read = 0;
pipe->pos_bloqued_write = 0;
}
void write_pipe(pipe_t *pipe, byte data)
{
di();
// Verifica se o PIPE está cheio
if (pipe->pipe_itens == MAX_PIPE_SIZE) {
pipe->pos_bloqued_write = task_running;
dispatcher(WAITING_PIPE);
}
pipe->pipe_queue[pipe->pos_write_pipe] = data;
pipe->pos_write_pipe = (pipe->pos_write_pipe + 1) % MAX_PIPE_SIZE;
pipe->pipe_itens++;
// Desbloqueia leitor, caso tenha algum bloqueado
if (pipe->pos_bloqued_read > 0) {
F_APTOS[pipe->pos_bloqued_read].task_state = READY;
pipe->pos_bloqued_read = 0;
#if PRIOR_SCH
dispatcher(READY);
#endif
ei();
}
ei();
}
byte read_pipe(pipe_t *pipe)
{
di();
byte dado;
// Verifica se o PIPE está vazio
if (pipe->pipe_itens == 0) {
//pipe->pipe_bloqued.b_queue[pipe->pipe_bloqued.b_queue_pos_in] = task_running;
//pipe->pipe_bloqued.b_queue_pos_in = (pipe->pipe_bloqued.b_queue_pos_in+1) % MAX_TASKS;
pipe->pos_bloqued_read = task_running;
dispatcher(WAITING_PIPE);
}
dado = pipe->pipe_queue[pipe->pos_read_pipe];
pipe->pos_read_pipe = (pipe->pos_read_pipe + 1) % MAX_PIPE_SIZE;
pipe->pipe_itens--;
// Desbloquear o escritor
if (pipe->pos_bloqued_write > 0) {
F_APTOS[pipe->pos_bloqued_write].task_state = READY;
pipe->pos_bloqued_write = 0;
#if PRIOR_SCH
dispatcher(READY);
#endif
}
ei();
return dado;
}
*/
/*
* Chamadas de sistema para manipulação do PIPE
*/
void pipe_create(pipe_t *pipe, u_int id, u_int size)
{
pipe->p_id = id;
pipe->p_pos_read = 0;
pipe->p_pos_write = 0;
pipe->p_size = size;
pipe->p_msg_queue = SRAMalloc(size);
pipe->p_count = 0;
}
void pipe_read(pipe_t *pipe, char* msg)
{
di();
if (pipe->p_count == 0) { // PIPE está vazio
// Bloqueia processo que tentou ler
// Chama o despachante
dispatcher(WAITING_PIPE);
}
else {
*msg = pipe->p_msg_queue[pipe->p_pos_read];
pipe->p_pos_read = (pipe->p_pos_read + 1) % pipe->p_size;
pipe->p_count--;
// Desbloqueia processos que estão aguardando espaço no pipe
libera_processos();
}
ei();
}
void pipe_write(pipe_t *pipe, char msg)
{
di();
if (pipe->p_count == pipe->p_size) { // PIPE está cheio
// Bloqueia processo que tentou ler
// Chama o despachante
dispatcher(WAITING_PIPE);
}
else {
pipe->p_msg_queue[pipe->p_pos_write] = msg;
pipe->p_pos_write = (pipe->p_pos_write + 1) % pipe->p_size;
pipe->p_count++;
// Desbloqueia processo que querem ler do pipe
libera_processos();
}
ei();
}
void pipe_destroy(pipe_t *pipe)
{
SRAMfree(pipe->p_msg_queue);
}
void libera_processos()
{
int i;
for (i = 0; i < tasks_installed; i++)
if (F_APTOS[i].task_state == WAITING_PIPE)
F_APTOS[i].task_state = READY;
}