summaryrefslogtreecommitdiff
path: root/kernel/ringbuffer.c
blob: 69f11b205f138b3426b337a2524988ae5993710a (plain)
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
#define FOOLOS_MODULE_NAME "ringbuffer"
#include "ringbuffer.h"

// TODO: this is disabled because a kb interrupt can occur anytime
// and the kernel will need to access the ringbuffer while we are accessing!
// DO WE need a spinlock in general? do not use a global one anyway!!!!
//static int sl=9;

ringbuffer ringbuffer_init(uint32_t size)
{
    ringbuffer f;
    f.data=kballoc(size);
    f.size=size*4096;
    f.front=f.size-1;
    f.back=f.size-1;
    return f;
}

bool ringbuffer_put(ringbuffer* f,uint8_t c)
{
//    x86_cli();

    if((f->back-1+f->size)%f->size==f->front)
    {
//	x86_sti();
	return false;
    }

    f->data[f->back]=c;
    f->back--;
    f->back+=f->size;
    f->back%=f->size;

//    x86_sti();
    return true;
}

bool ringbuffer_has(ringbuffer* f)
{
//    x86_cli();
    bool res=true;

    if(f->front==f->back)
	res=false;

//    x86_sti();
    return res;
}

uint8_t ringbuffer_get(ringbuffer* f) // non blocking . please check first
{
//    x86_cli();
    char c;

    if(f->front==f->back)
    {
//	x86_sti();
	return ' ';
    }

    c=f->data[f->front];

    f->front--;
    f->front+=f->size;
    f->front%=f->size;

  //  x86_sti();
    return c;
}