@ -67,50 +67,47 @@ private:
int _head { 0 } ;
int _head { 0 } ;
int _tail { 0 } ;
int _tail { 0 } ;
px4_sem_t _lock = SEM_INITIALIZER ( 1 ) ;
px4_sem_t _lock = SEM_INITIALIZER ( 1 ) ;
bool _is_printing { false } ;
pthread_t _printing_task ;
} ;
} ;
void ConsoleBuffer : : print ( bool follow )
void ConsoleBuffer : : print ( bool follow )
{
{
lock ( ) ;
// print to stdout, but with a buffer in between to avoid nested locking and potential dead-locks
_printing_task = pthread_self ( ) ;
// (which could happen in combination with the MAVLink shell: dmesg writing to the pipe waiting
int i = _head ;
// mavlink to read, while mavlink calls printf, waiting for the console lock)
const int buffer_length = 512 ;
char buffer [ buffer_length ] ;
int offset = - 1 ;
do {
while ( true ) {
// print the full buffer or what's newly added
_is_printing = true ;
int total_size_read = 0 ;
if ( i < _tail ) {
do {
: : write ( 1 , _buffer + i , _tail - i ) ;
int read_size = read ( buffer , buffer_length , & offset ) ;
} else if ( _tail < i ) {
if ( read_size < = 0 ) {
: : write ( 1 , _buffer + i , BOARD_CONSOLE_BUFFER_SIZE - i ) ;
break ;
: : write ( 1 , _buffer , _tail ) ;
}
}
i = _tail ;
: : write ( 1 , buffer , read_size ) ;
if ( read_size < buffer_length ) {
break ;
}
total_size_read + = read_size ;
} while ( total_size_read < BOARD_CONSOLE_BUFFER_SIZE ) ;
_is_printing = false ;
if ( follow ) {
if ( follow ) {
unlock ( ) ;
usleep ( 10000 ) ;
usleep ( 10000 ) ;
lock ( ) ;
} else {
break ;
}
}
}
} while ( follow ) ;
unlock ( ) ;
}
}
void ConsoleBuffer : : write ( const char * buffer , size_t len )
void ConsoleBuffer : : write ( const char * buffer , size_t len )
{
{
if ( _is_printing & & pthread_self ( ) = = _printing_task ) { // avoid adding to the buffer while we are printing it
return ;
}
lock ( ) ; // same rule as for printf: this cannot be used from IRQ handlers
lock ( ) ; // same rule as for printf: this cannot be used from IRQ handlers
for ( size_t i = 0 ; i < len ; + + i ) {
for ( size_t i = 0 ; i < len ; + + i ) {