Repository navigation
Expand file tree
/
Copy pathpipe.c
More file actions
157 lines (117 loc) · 3.11 KB
/
Copy pathpipe.c
File metadata and controls
157 lines (117 loc) · 3.11 KB
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
#include "pipe.h"
#include "logging.h"
/* ──────────────────────────────────────────────────────────────────
Implémentation des pipes
────────────────────────────────────────────────────────────────── */
error_t pipe_create(pipe_t* p)
{
if (!p) {
return E_INVAL;
}
p->read_idx = 0;
p->write_idx = 0;
p->count = 0;
p->readers = 1;
p->writers = 1;
p->active = 1;
mutex_init(&p->lock);
sem_init(&p->not_empty, 0, 1);
sem_init(&p->not_full, 1, 1);
log_msg(LOG_DEBUG, "Pipe créé");
return E_OK;
}
error_t pipe_close(pipe_t* p)
{
if (!p) {
return E_INVAL;
}
mutex_lock(&p->lock);
p->active = 0;
mutex_unlock(&p->lock);
log_msg(LOG_DEBUG, "Pipe fermé");
return E_OK;
}
error_t pipe_write(pipe_t* p, uint32_t data)
{
if (!p || !p->active) {
return E_BADF;
}
mutex_lock(&p->lock);
if (p->count >= PIPE_BUFFER_SIZE) {
mutex_unlock(&p->lock);
return E_BUSY; /* Buffer plein (non-bloquant) */
}
p->buffer[p->write_idx] = data;
p->write_idx = (p->write_idx + 1) % PIPE_BUFFER_SIZE;
p->count++;
mutex_unlock(&p->lock);
/* Signaler que le buffer n'est pas vide */
sem_post(&p->not_empty);
return E_OK;
}
error_t pipe_write_blocking(pipe_t* p, uint32_t data)
{
if (!p || !p->active) {
return E_BADF;
}
/* Attendre que le buffer ne soit pas plein */
sem_wait(&p->not_full);
return pipe_write(p, data);
}
error_t pipe_read(pipe_t* p, uint32_t* data)
{
if (!p || !p->active || !data) {
return E_INVAL;
}
mutex_lock(&p->lock);
if (p->count == 0) {
mutex_unlock(&p->lock);
return E_BUSY; /* Buffer vide (non-bloquant) */
}
*data = p->buffer[p->read_idx];
p->read_idx = (p->read_idx + 1) % PIPE_BUFFER_SIZE;
p->count--;
mutex_unlock(&p->lock);
/* Signaler que le buffer n'est pas plein */
sem_post(&p->not_full);
return E_OK;
}
error_t pipe_read_blocking(pipe_t* p, uint32_t* data)
{
if (!p || !p->active || !data) {
return E_INVAL;
}
/* Attendre qu'il y ait des données */
sem_wait(&p->not_empty);
return pipe_read(p, data);
}
error_t pipe_flush(pipe_t* p)
{
if (!p) {
return E_INVAL;
}
mutex_lock(&p->lock);
p->read_idx = 0;
p->write_idx = 0;
p->count = 0;
mutex_unlock(&p->lock);
return E_OK;
}
int pipe_get_count(pipe_t* p)
{
if (!p) return 0;
mutex_lock(&p->lock);
int cnt = p->count;
mutex_unlock(&p->lock);
return cnt;
}
int pipe_is_empty(pipe_t* p)
{
if (!p) return 1;
return pipe_get_count(p) == 0;
}
int pipe_is_full(pipe_t* p)
{
if (!p) return 0;
return pipe_get_count(p) >= PIPE_BUFFER_SIZE;
}