summaryrefslogtreecommitdiff
path: root/fs/ringbuffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ringbuffer.c')
-rw-r--r--fs/ringbuffer.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/fs/ringbuffer.c b/fs/ringbuffer.c
new file mode 100644
index 0000000..ca5870f
--- /dev/null
+++ b/fs/ringbuffer.c
@@ -0,0 +1,71 @@
+#define FOOLOS_MODULE_NAME "fifo"
+#include "lib/logger/log.h"
+
+#include "fifo.h"
+
+static int sl=9;
+
+fifo fifo_init(uint32_t size)
+{
+ fifo f;
+ f.data=kballoc(size);
+ f.size=size*4096;
+ f.front=f.size-1;
+ f.back=f.size-1;
+ return f;
+}
+
+bool fifo_put(fifo* f,uint8_t c)
+{
+ x86_int_disable();
+ lock_spin(sl);
+
+ if((f->back-1+f->size)%f->size==f->front)
+ {
+ lock_release(sl);
+ log(FOOLOS_MODULE_NAME,FOOLOS_LOG_FINE,"unlocked by put");
+ x86_int_enable();
+ return false;
+ }
+
+ f->data[f->back]=c;
+ log(FOOLOS_MODULE_NAME,FOOLOS_LOG_FINE,"put %d %d (%c)", f->back, f->front,c);
+ f->back--;
+ f->back+=f->size;
+ f->back%=f->size;
+
+ lock_release(sl);
+ x86_int_enable();
+
+ return true;
+}
+
+uint8_t fifo_get(fifo* f) // blocking
+{
+
+ char c;
+
+ x86_int_disable();
+ lock_spin(sl);
+
+ if(f->front==f->back)
+ {
+ lock_release(sl);
+ log(FOOLOS_MODULE_NAME,FOOLOS_LOG_FINE,"unlocked by get");
+ x86_int_enable();
+ c='_';
+ return false;
+ }
+
+ c=f->data[f->front];
+ log(FOOLOS_MODULE_NAME,FOOLOS_LOG_FINE,"get %d %d (%c)", f->back, f->front,c);
+
+ f->front--;
+ f->front+=f->size;
+ f->front%=f->size;
+
+ lock_release(sl);
+ x86_int_enable();
+
+ return true;
+}