[kernel][lib][cbuf] do a little more c++ conversion
Mostly just replacing manual spinlock with using a guard.
Test: build and validate that the cbuf continues to work via observing
that serial IO still works from the kernel.
Change-Id: Ic25a4968ea39855873caed564b19be6f62acbd36
diff --git a/kernel/lib/cbuf/cbuf.cpp b/kernel/lib/cbuf/cbuf.cpp
index 48d0f38..44c8cd7 100644
--- a/kernel/lib/cbuf/cbuf.cpp
+++ b/kernel/lib/cbuf/cbuf.cpp
@@ -10,6 +10,7 @@
#include <assert.h>
#include <debug.h>
#include <fbl/algorithm.h>
+#include <kernel/auto_lock.h>
#include <kernel/event.h>
#include <kernel/spinlock.h>
#include <pow2.h>
@@ -19,8 +20,9 @@
#define LOCAL_TRACE 0
-#define INC_POINTER(cbuf, ptr, inc) \
- modpow2(((ptr) + (inc)), (cbuf)->len_pow2)
+static inline uint inc_pointer(const cbuf_t* cbuf, uint ptr, uint inc) {
+ return modpow2(ptr + inc, cbuf->len_pow2);
+}
void cbuf_initialize(cbuf_t* cbuf, size_t len) {
cbuf_initialize_etc(cbuf, len, malloc(len));
@@ -41,22 +43,21 @@
LTRACEF("len %zu, len_pow2 %u\n", len, cbuf->len_pow2);
}
-size_t cbuf_space_avail(cbuf_t* cbuf) {
- uint consumed = modpow2((uint)(cbuf->head - cbuf->tail), cbuf->len_pow2);
+size_t cbuf_space_avail(const cbuf_t* cbuf) {
+ uint consumed = modpow2(cbuf->head - cbuf->tail, cbuf->len_pow2);
return valpow2(cbuf->len_pow2) - consumed - 1;
}
size_t cbuf_write_char(cbuf_t* cbuf, char c) {
DEBUG_ASSERT(cbuf);
- spin_lock_saved_state_t state;
- spin_lock_irqsave(&cbuf->lock, state);
+ AutoSpinLock guard(&cbuf->lock);
size_t ret = 0;
if (cbuf_space_avail(cbuf) > 0) {
cbuf->buf[cbuf->head] = c;
- cbuf->head = INC_POINTER(cbuf, cbuf->head, 1);
+ cbuf->head = inc_pointer(cbuf, cbuf->head, 1);
ret = 1;
if (cbuf->head != cbuf->tail) {
@@ -64,8 +65,6 @@
}
}
- spin_unlock_irqrestore(&cbuf->lock, state);
-
return ret;
}
@@ -74,28 +73,28 @@
DEBUG_ASSERT(c);
retry:
- if (block)
+ if (block) {
event_wait(&cbuf->event);
-
- spin_lock_saved_state_t state;
- spin_lock_irqsave(&cbuf->lock, state);
-
- // see if there's data available
- size_t ret = 0;
- if (cbuf->tail != cbuf->head) {
-
- *c = cbuf->buf[cbuf->tail];
- cbuf->tail = INC_POINTER(cbuf, cbuf->tail, 1);
-
- if (cbuf->tail == cbuf->head) {
- // we've emptied the buffer, unsignal the event
- event_unsignal(&cbuf->event);
- }
-
- ret = 1;
}
- spin_unlock_irqrestore(&cbuf->lock, state);
+ size_t ret = 0;
+ {
+ AutoSpinLock guard(&cbuf->lock);
+
+ // see if there's data available
+ if (cbuf->tail != cbuf->head) {
+
+ *c = cbuf->buf[cbuf->tail];
+ cbuf->tail = inc_pointer(cbuf, cbuf->tail, 1);
+
+ if (cbuf->tail == cbuf->head) {
+ // we've emptied the buffer, unsignal the event
+ event_unsignal(&cbuf->event);
+ }
+
+ ret = 1;
+ }
+ }
if (block && ret == 0) {
goto retry;
diff --git a/kernel/lib/cbuf/include/lib/cbuf.h b/kernel/lib/cbuf/include/lib/cbuf.h
index 16425b9..dd6233c 100644
--- a/kernel/lib/cbuf/include/lib/cbuf.h
+++ b/kernel/lib/cbuf/include/lib/cbuf.h
@@ -53,7 +53,7 @@
* @return The number of free space available in the cbuf (IOW - the maximum
* number of bytes which can currently be written)
*/
-size_t cbuf_space_avail(cbuf_t* cbuf);
+size_t cbuf_space_avail(const cbuf_t* cbuf);
/* special cases for dealing with a single char of data */
size_t cbuf_read_char(cbuf_t* cbuf, char* c, bool block);