Extract BufferExchange, simplify threading.

This commit is contained in:
Jared Boone 2016-10-06 13:38:56 -07:00
parent 84334ef818
commit 5dfb53263a
6 changed files with 161 additions and 81 deletions

View File

@ -174,6 +174,7 @@ set(CPPSRC
file.cpp
log_file.cpp
${COMMON}/png_writer.cpp
${COMMON}/buffer_exchange.cpp
capture_thread.cpp
io_file.cpp
io_wave.cpp

View File

@ -22,67 +22,20 @@
#include "capture_thread.hpp"
#include "baseband_api.hpp"
#include "buffer_exchange.hpp"
// BufferExchange ///////////////////////////////////////////////////////////
class BufferExchange {
public:
BufferExchange(CaptureConfig* const config);
~BufferExchange();
#if defined(LPC43XX_M0)
StreamBuffer* get() {
StreamBuffer* p { nullptr };
fifo_buffers_for_application->out(p);
return p;
struct BasebandCapture {
BasebandCapture(CaptureConfig* const config) {
baseband::capture_start(config);
}
bool put(StreamBuffer* const p) {
return fifo_buffers_for_baseband->in(p);
~BasebandCapture() {
baseband::capture_stop();
}
#endif
#if defined(LPC43XX_M4)
StreamBuffer* get() {
StreamBuffer* p { nullptr };
fifo_buffers_for_baseband->out(p);
return p;
}
bool put(StreamBuffer* const p) {
return fifo_buffers_for_application->in(p);
}
#endif
static FIFO<StreamBuffer*>* fifo_buffers_for_baseband;
static FIFO<StreamBuffer*>* fifo_buffers_for_application;
private:
CaptureConfig* const config;
};
FIFO<StreamBuffer*>* BufferExchange::fifo_buffers_for_baseband = nullptr;
FIFO<StreamBuffer*>* BufferExchange::fifo_buffers_for_application = nullptr;
BufferExchange::BufferExchange(
CaptureConfig* const config
) : config { config }
{
baseband::capture_start(config);
fifo_buffers_for_baseband = config->fifo_buffers_empty;
fifo_buffers_for_application = config->fifo_buffers_full;
}
BufferExchange::~BufferExchange() {
fifo_buffers_for_baseband = nullptr;
fifo_buffers_for_application = nullptr;
baseband::capture_stop();
}
// CaptureThread //////////////////////////////////////////////////////////
Thread* CaptureThread::thread = nullptr;
CaptureThread::CaptureThread(
std::unique_ptr<stream::Writer> writer,
size_t write_size,
@ -101,23 +54,11 @@ CaptureThread::CaptureThread(
CaptureThread::~CaptureThread() {
if( thread ) {
chThdTerminate(thread);
chEvtSignal(thread, event_mask_loop_wake);
chThdWait(thread);
thread = nullptr;
}
}
void CaptureThread::check_fifo_isr() {
// TODO: Prevent over-signalling by transmitting a set of
// flags from the baseband core.
const auto fifo = BufferExchange::fifo_buffers_for_application;
if( fifo ) {
if( !fifo->is_empty() ) {
chEvtSignalI(thread, event_mask_loop_wake);
}
}
}
msg_t CaptureThread::static_fn(void* arg) {
auto obj = static_cast<CaptureThread*>(arg);
const auto error = obj->run();
@ -132,20 +73,17 @@ msg_t CaptureThread::static_fn(void* arg) {
}
Optional<File::Error> CaptureThread::run() {
BasebandCapture capture { &config };
BufferExchange buffers { &config };
while( !chThdShouldTerminate() ) {
auto buffer = buffers.get();
if( buffer ) {
auto write_result = writer->write(buffer->data(), buffer->size());
if( write_result.is_error() ) {
return write_result.error();
}
buffer->empty();
buffers.put(buffer);
} else {
chEvtWaitAny(event_mask_loop_wake);
auto write_result = writer->write(buffer->data(), buffer->size());
if( write_result.is_error() ) {
return write_result.error();
}
buffer->empty();
buffers.put(buffer);
}
return { };

View File

@ -48,16 +48,12 @@ public:
return config;
}
static void check_fifo_isr();
private:
static constexpr auto event_mask_loop_wake = EVENT_MASK(0);
CaptureConfig config;
std::unique_ptr<stream::Writer> writer;
std::function<void()> success_callback;
std::function<void(File::Error)> error_callback;
static Thread* thread;
Thread* thread;
static msg_t static_fn(void* arg);

View File

@ -31,7 +31,7 @@
#include "irq_controls.hpp"
#include "capture_thread.hpp"
#include "buffer_exchange.hpp"
#include "ch.h"
@ -48,7 +48,7 @@ CH_IRQ_HANDLER(M4Core_IRQHandler) {
CH_IRQ_PROLOGUE();
chSysLockFromIsr();
CaptureThread::check_fifo_isr();
BufferExchange::handle_isr();
EventDispatcher::check_fifo_isr();
chSysUnlockFromIsr();

View File

@ -0,0 +1,54 @@
/*
* Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc.
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "buffer_exchange.hpp"
BufferExchange* BufferExchange::obj { nullptr };
BufferExchange::BufferExchange(
CaptureConfig* const config
) : config { config }
{
obj = this;
fifo_buffers_for_baseband = config->fifo_buffers_empty;
fifo_buffers_for_application = config->fifo_buffers_full;
}
BufferExchange::~BufferExchange() {
obj = nullptr;
fifo_buffers_for_baseband = nullptr;
fifo_buffers_for_application = nullptr;
}
StreamBuffer* BufferExchange::get(FIFO<StreamBuffer*>* fifo) {
while(true) {
StreamBuffer* p { nullptr };
fifo->out(p);
if( p ) {
return p;
}
chSysLock();
thread = chThdSelf();
chSchGoSleepS(THD_STATE_SUSPENDED);
chSysUnlock();
}
}

View File

@ -0,0 +1,91 @@
/*
* Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc.
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#include "ch.h"
#include "message.hpp"
#include "fifo.hpp"
#include "io.hpp"
class BufferExchange {
public:
BufferExchange(CaptureConfig* const config);
~BufferExchange();
#if defined(LPC43XX_M0)
bool empty() const {
return fifo_buffers_for_application->is_empty();
}
StreamBuffer* get() {
return get(fifo_buffers_for_application);
}
bool put(StreamBuffer* const p) {
return fifo_buffers_for_baseband->in(p);
}
#endif
#if defined(LPC43XX_M4)
bool empty() const {
return fifo_buffers_for_baseband->is_empty();
}
StreamBuffer* get() {
return get(fifo_buffers_for_baseband);
}
bool put(StreamBuffer* const p) {
return fifo_buffers_for_application->in(p);
}
#endif
static void handle_isr() {
if( obj ) {
obj->check_fifo_isr();
}
}
private:
CaptureConfig* const config;
FIFO<StreamBuffer*>* fifo_buffers_for_baseband;
FIFO<StreamBuffer*>* fifo_buffers_for_application;
Thread* thread;
static BufferExchange* obj;
void check_fifo_isr() {
if( !empty() ) {
wakeup_isr();
}
}
void wakeup_isr() {
auto thread_tmp = thread;
if( thread_tmp ) {
thread = nullptr;
chSchReadyI(thread_tmp);
}
}
StreamBuffer* get(FIFO<StreamBuffer*>* fifo);
};