#define FOOLOS_MODULE_NAME "ringbuffer" #include "lib/logger/log.h" #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; f.front=f.size-1; f.back=f.size-1; return f; } volatile bool ringbuffer_put(ringbuffer* f,uint8_t c) { x86_int_disable(); lock_spin(sl); if((f->back-1+f->size)%f->size==f->front) { lock_release(sl); x86_int_enable(); return false; } f->data[f->back]=c; f->back--; f->back+=f->size; f->back%=f->size; lock_release(sl); x86_int_enable(); return true; } volatile bool ringbuffer_has(ringbuffer* f) { x86_int_disable(); bool res=true; lock_spin(sl); if(f->front==f->back) res=false; lock_release(sl); x86_int_enable(); return res; } volatile uint8_t ringbuffer_get(ringbuffer* f) // non blocking . please check first { x86_int_disable(); char c; lock_spin(sl); if(f->front==f->back) { lock_release(sl); x86_int_enable(); return ' '; } c=f->data[f->front]; f->front--; f->front+=f->size; f->front%=f->size; lock_release(sl); x86_int_enable(); return c; }