-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipe.c
33 lines (33 loc) · 836 Bytes
/
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
#include <stddef.h>
#include "semaphore.h"
typedef struct _osPipe *osPipeId;
struct _osPipe {
volatile int count;
uint16_t write_pos;
uint16_t read_pos;
uint16_t length;
void* base[0];
};
int pipe_write(osPipeId pipe, void* data)
{
if (pipe->count == pipe->length) return 0;
if (pipe->write_pos==pipe->length) pipe->write_pos=0;
pipe->base[pipe->write_pos++] = data;
return semaphore_leave(&pipe->count);
}
void* pipe_read(osPipeId pipe)
{
uint32_t count = semaphore_enter(&pipe->count);
if (count==0) return NULL;
if (pipe->read_pos==pipe->length) pipe->read_pos=0;
return pipe->base[pipe->read_pos++];
}
int pipe_create (osPipeId pipe, size_t size, uint32_t length)
{
pipe->count = 0;
// pipe->array = malloc(length*(size+sizeof(void*)));
pipe->write_pos = 0;
pipe->read_pos = 0;
pipe->length = length;
return 0;
}