HAL_Linux: make uart writes thread safe

This commit is contained in:
Andrew Tridgell 2018-08-06 16:23:24 +10:00
parent b2b56c3e46
commit f1e10b0a8f
2 changed files with 17 additions and 2 deletions

View File

@ -296,14 +296,20 @@ size_t UARTDriver::write(uint8_t c)
if (!_initialised) {
return 0;
}
if (!_write_mutex.take_nonblocking()) {
return 0;
}
while (_writebuf.space() == 0) {
if (_nonblocking_writes) {
_write_mutex.give();
return 0;
}
hal.scheduler->delay(1);
}
return _writebuf.write(&c, 1);
size_t ret = _writebuf.write(&c, 1);
_write_mutex.give();
return ret;
}
/*
@ -314,6 +320,9 @@ size_t UARTDriver::write(const uint8_t *buffer, size_t size)
if (!_initialised) {
return 0;
}
if (!_write_mutex.take_nonblocking()) {
return 0;
}
if (!_nonblocking_writes) {
/*
use the per-byte delay loop in write() above for blocking writes
@ -323,10 +332,13 @@ size_t UARTDriver::write(const uint8_t *buffer, size_t size)
if (write(*buffer++) != 1) break;
ret++;
}
_write_mutex.give();
return ret;
}
return _writebuf.write(buffer, size);
size_t ret = _writebuf.write(buffer, size);
_write_mutex.give();
return ret;
}
/*

View File

@ -5,6 +5,7 @@
#include "AP_HAL_Linux.h"
#include "SerialDevice.h"
#include "Semaphores.h"
namespace Linux {
@ -97,6 +98,8 @@ protected:
virtual int _write_fd(const uint8_t *buf, uint16_t n);
virtual int _read_fd(uint8_t *buf, uint16_t n);
Linux::Semaphore _write_mutex;
};
}