diff --git a/core/Jamfile b/core/Jamfile index 2787dcbc76..6e0fae63bf 100644 --- a/core/Jamfile +++ b/core/Jamfile @@ -28,7 +28,7 @@ SubDir TOP core ; Module core : kernel_init.c sched.c mutex.c msg.c queue.c - clist.c thread.c bitarithm.c ; + clist.c thread.c bitarithm.c cib.c ; Module hwtimer : hwtimer.c : hwtimer_cpu ; diff --git a/core/cib.c b/core/cib.c new file mode 100644 index 0000000000..252abbde47 --- /dev/null +++ b/core/cib.c @@ -0,0 +1,47 @@ +typedef struct cib { + unsigned int read_count; + unsigned int write_count; + unsigned int complement; +} cib_t; + +void cib_init(cib_t *cib, unsigned int size) { + cib->read_count = 0; + cib->write_count = 0; + cib->complement = 0-size; +} + +int cib_avail (cib_t *cib) { + return cib->write_count - cib->read_count; +} + +int cib_get(cib_t *cib) { + int avail = cib_avail (cib); + + if (avail > 0) { + return cib->read_count++ & ~cib->complement; + } + + return -1; +} + +int cib_put(cib_t *cib) { + int avail = cib_avail (cib); + + if ((int)(avail + cib->complement) < 0 ) { + return cib->write_count++ & ~(cib->complement); + } + + return -1; +} + +/* +int main() { + cib_t cib; + + cib_init(&cib, 0); + + int res = cib_get(&cib); + + printf("%i\n", res); +} +*/ diff --git a/core/include/cib.h b/core/include/cib.h new file mode 100644 index 0000000000..7ef179280d --- /dev/null +++ b/core/include/cib.h @@ -0,0 +1,15 @@ +#ifndef __CIB_H +#define __CIB_H + +typedef struct cib_t { + unsigned int read_count; + unsigned int write_count; + unsigned int complement; +} cib_t; + +void cib_init(cib_t *cib, unsigned int size); +int cib_get(cib_t *cib); +int cib_put(cib_t *cib); +int cib_avail(cib_t *cib); + +#endif /* __CIB_H */