Change the triple buffer object interface

This commit is contained in:
Fred Sundvik 2016-02-21 18:30:04 +02:00
parent 679bfe7c54
commit 2710361cd8
3 changed files with 49 additions and 78 deletions

View file

@ -43,7 +43,7 @@ void triple_buffer_init(triple_buffer_object_t* object) {
SET_DATA_AVAILABLE(0);
}
static bool triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) {
void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object) {
serial_link_lock();
if (GET_DATA_AVAILABLE()) {
uint8_t shared_index = GET_SHARED_INDEX();
@ -52,41 +52,25 @@ static bool triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_
SET_SHARED_INDEX(read_index);
SET_DATA_AVAILABLE(false);
serial_link_unlock();
return true;
return object->buffer + object_size * shared_index;
}
else {
serial_link_unlock();
return false;
return NULL;
}
}
static void triple_buffer_actual_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) {
uint8_t read_index = GET_READ_INDEX();
memcpy(dst, object->buffer + object_size*read_index, object_size);
}
static void triple_buffer_end_read(uint16_t object_size, triple_buffer_object_t* object) {
}
void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src) {
void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object) {
uint8_t write_index = GET_WRITE_INDEX();
memcpy(object->buffer + object_size * write_index, src, object_size);
return object->buffer + object_size * write_index;
}
void triple_buffer_end_write_internal(triple_buffer_object_t* object) {
serial_link_lock();
uint8_t shared_index = GET_SHARED_INDEX();
uint8_t write_index = GET_WRITE_INDEX();
SET_SHARED_INDEX(write_index);
SET_WRITE_INDEX(shared_index);
SET_DATA_AVAILABLE(true);
serial_link_unlock();
}
bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) {
if (triple_buffer_begin_read(object_size, object)) {
triple_buffer_actual_read(object_size, object, dst);
triple_buffer_end_read(object_size, object);
return true;
}
else {
return false;
}
}

View file

@ -31,8 +31,19 @@ typedef struct {
}triple_buffer_object_t;
void triple_buffer_init(triple_buffer_object_t* object);
void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src);
bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst);
#define triple_buffer_begin_write(object) \
(typeof(*object.buffer[0])*)triple_buffer_begin_write_internal(sizeof(*object.buffer[0]), (triple_buffer_object_t*)object)
#define triple_buffer_end_write(object) \
triple_buffer_end_write_internal((triple_buffer_object_t*)object)
#define triple_buffer_read(object) \
(typeof(*object.buffer[0])*)triple_buffer_read_internal(sizeof(*object.buffer[0]), (triple_buffer_object_t*)object)
void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object);
void triple_buffer_end_write_internal(triple_buffer_object_t* object);
void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object);
#endif

View file

@ -40,67 +40,43 @@ AfterEach(TripleBufferedObject) {}
Ensure(TripleBufferedObject, writes_and_reads_object) {
uint32_t src = 0x3456ABCC;
uint32_t dst;
triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src);
triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst);
assert_that(dst, is_equal_to(src));
*triple_buffer_begin_write(&test_object) = 0x3456ABCC;
triple_buffer_end_write(&test_object);
assert_that(*triple_buffer_read(&test_object), is_equal_to(0x3456ABCC));
}
Ensure(TripleBufferedObject, does_not_read_empty) {
uint32_t dst;
bool res = triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst);
assert_that(res, is_equal_to(false));
}
Ensure(TripleBufferedObject, writes_and_reads_object_decomposed) {
uint32_t src = 0x3456ABCC;
uint32_t dst;
triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src);
triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object);
triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst);
triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object);
assert_that(dst, is_equal_to(src));
assert_that(triple_buffer_read(&test_object), is_equal_to(NULL));
}
Ensure(TripleBufferedObject, writes_twice_and_reads_object) {
uint32_t src = 0x3456ABCC;
uint32_t dst;
triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src);
src = 0x44778899;
triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src);
triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst);
assert_that(dst, is_equal_to(src));
*triple_buffer_begin_write(&test_object) = 0x3456ABCC;
triple_buffer_end_write(&test_object);
*triple_buffer_begin_write(&test_object) = 0x44778899;
triple_buffer_end_write(&test_object);
assert_that(*triple_buffer_read(&test_object), is_equal_to(0x44778899));
}
Ensure(TripleBufferedObject, performs_another_write_in_the_middle_of_read) {
uint32_t src = 1;
uint32_t dst;
triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src);
src = 2;
triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object);
triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src);
triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst);
triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object);
assert_that(dst, is_equal_to(1));
triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst);
assert_that(dst, is_equal_to(2));
assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false));
*triple_buffer_begin_write(&test_object) = 1;
triple_buffer_end_write(&test_object);
uint32_t* read = triple_buffer_read(&test_object);
*triple_buffer_begin_write(&test_object) = 2;
triple_buffer_end_write(&test_object);
assert_that(*read, is_equal_to(1));
assert_that(*triple_buffer_read(&test_object), is_equal_to(2));
assert_that(triple_buffer_read(&test_object), is_equal_to(NULL));
}
Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) {
uint32_t src = 1;
uint32_t dst;
triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src);
triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object);
src = 2;
triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src);
src = 3;
triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src);
triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst);
triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object);
assert_that(dst, is_equal_to(1));
triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst);
assert_that(dst, is_equal_to(3));
assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false));
*triple_buffer_begin_write(&test_object) = 1;
triple_buffer_end_write(&test_object);
uint32_t* read = triple_buffer_read(&test_object);
*triple_buffer_begin_write(&test_object) = 2;
triple_buffer_end_write(&test_object);
*triple_buffer_begin_write(&test_object) = 3;
triple_buffer_end_write(&test_object);
assert_that(*read, is_equal_to(1));
assert_that(*triple_buffer_read(&test_object), is_equal_to(3));
assert_that(triple_buffer_read(&test_object), is_equal_to(NULL));
}