remove spMutex, use atomic test&set instead

This commit is contained in:
Philippe Teuwen 2019-05-16 00:29:30 +02:00
commit b4e8be82f4
4 changed files with 27 additions and 22 deletions

View file

@ -48,8 +48,6 @@ static int cmd_tail = 0;
// to lock rxBuffer operations from different threads // to lock rxBuffer operations from different threads
static pthread_mutex_t rxBufferMutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t rxBufferMutex = PTHREAD_MUTEX_INITIALIZER;
// serial port access from different threads
static pthread_mutex_t spMutex = PTHREAD_MUTEX_INITIALIZER;
// Global start time for WaitForResponseTimeout & dl_it, so we can reset timeout when we get packets // Global start time for WaitForResponseTimeout & dl_it, so we can reset timeout when we get packets
// as sending lot of these packets can slow down things wuite a lot on slow links (e.g. hw status or lf read at 9600) // as sending lot of these packets can slow down things wuite a lot on slow links (e.g. hw status or lf read at 9600)
@ -344,8 +342,6 @@ __attribute__((force_align_arg_pointer))
break; break;
} }
pthread_mutex_lock(&spMutex);
res = uart_receive(sp, (uint8_t *)&rx_raw.pre, sizeof(PacketResponseNGPreamble), &rxlen); res = uart_receive(sp, (uint8_t *)&rx_raw.pre, sizeof(PacketResponseNGPreamble), &rxlen);
if ((res == PM3_SUCCESS) && (rxlen == sizeof(PacketResponseNGPreamble))) { if ((res == PM3_SUCCESS) && (rxlen == sizeof(PacketResponseNGPreamble))) {
rx.magic = rx_raw.pre.magic; rx.magic = rx_raw.pre.magic;
@ -465,8 +461,6 @@ __attribute__((force_align_arg_pointer))
} }
} }
pthread_mutex_unlock(&spMutex);
// TODO if error, shall we resync ? // TODO if error, shall we resync ?
pthread_mutex_lock(&txBufferMutex); pthread_mutex_lock(&txBufferMutex);
@ -486,7 +480,6 @@ __attribute__((force_align_arg_pointer))
if (txBuffer_pending) { if (txBuffer_pending) {
pthread_mutex_lock(&spMutex);
if (txBufferNGLen) { // NG packet if (txBufferNGLen) { // NG packet
res = uart_send(sp, (uint8_t *) &txBufferNG, txBufferNGLen); res = uart_send(sp, (uint8_t *) &txBufferNG, txBufferNGLen);
if (res == PM3_EIO) { if (res == PM3_EIO) {
@ -501,7 +494,6 @@ __attribute__((force_align_arg_pointer))
} }
conn.last_command = txBuffer.cmd; conn.last_command = txBuffer.cmd;
} }
pthread_mutex_unlock(&spMutex);
txBuffer_pending = false; txBuffer_pending = false;
@ -628,13 +620,7 @@ int TestProxmark(void) {
// reconfigure. // reconfigure.
if (conn.send_via_fpc_usart == false) { if (conn.send_via_fpc_usart == false) {
#if defined(_WIN32) int res = uart_reconfigure_timeouts(UART_USB_CLIENT_RX_TIMEOUT_MS);
pthread_mutex_lock(&spMutex);
#endif
int res = uart_reconfigure_timeouts(sp, UART_USB_CLIENT_RX_TIMEOUT_MS);
#if defined(_WIN32)
pthread_mutex_unlock(&spMutex);
#endif
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
return res; return res;
} }

View file

@ -107,6 +107,6 @@ uint32_t uart_get_speed(const serial_port sp);
/* Reconfigure timeouts /* Reconfigure timeouts
*/ */
int uart_reconfigure_timeouts(serial_port sp, uint32_t value); int uart_reconfigure_timeouts(uint32_t value);
#endif // _UART_H_ #endif // _UART_H_

View file

@ -73,8 +73,12 @@ struct timeval timeout = {
.tv_usec = UART_FPC_CLIENT_RX_TIMEOUT_MS * 1000 .tv_usec = UART_FPC_CLIENT_RX_TIMEOUT_MS * 1000
}; };
int uart_reconfigure_timeouts(serial_port sp, uint32_t value) { uint32_t newtimeout_value = 0;
timeout.tv_usec = value * 1000; bool newtimeout_nopending = true;
int uart_reconfigure_timeouts(uint32_t value) {
newtimeout_value = value;
__atomic_clear(&newtimeout_nopending, __ATOMIC_SEQ_CST);
return PM3_SUCCESS; return PM3_SUCCESS;
} }
@ -83,7 +87,7 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
if (sp == 0) return INVALID_SERIAL_PORT; if (sp == 0) return INVALID_SERIAL_PORT;
// init timeouts // init timeouts
uart_reconfigure_timeouts(sp, UART_FPC_CLIENT_RX_TIMEOUT_MS); timeout.tv_usec = UART_FPC_CLIENT_RX_TIMEOUT_MS * 1000;
if (memcmp(pcPortName, "tcp:", 4) == 0) { if (memcmp(pcPortName, "tcp:", 4) == 0) {
struct addrinfo *addr, *rp; struct addrinfo *addr, *rp;
@ -241,7 +245,6 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin
// Reset the output count // Reset the output count
*pszRxLen = 0; *pszRxLen = 0;
do { do {
// Reset file descriptor // Reset file descriptor
FD_ZERO(&rfds); FD_ZERO(&rfds);
@ -299,6 +302,8 @@ int uart_send(const serial_port sp, const uint8_t *pbtTx, const uint32_t len) {
uint32_t pos = 0; uint32_t pos = 0;
fd_set rfds; fd_set rfds;
struct timeval tv; struct timeval tv;
if (__atomic_test_and_set(&newtimeout_nopending, __ATOMIC_SEQ_CST) == 0)
timeout.tv_usec = newtimeout_value * 1000;
while (pos < len) { while (pos < len) {
// Reset file descriptor // Reset file descriptor

View file

@ -48,7 +48,19 @@ typedef struct {
COMMTIMEOUTS ct; // Serial port time-out configuration COMMTIMEOUTS ct; // Serial port time-out configuration
} serial_port_windows; } serial_port_windows;
int uart_reconfigure_timeouts(serial_port sp, uint32_t value) { uint32_t newtimeout_value = 0;
bool newtimeout_nopending = true;
int uart_reconfigure_timeouts(uint32_t value) {
newtimeout_value = value;
__atomic_clear(&newtimeout_nopending, __ATOMIC_SEQ_CST);
return PM3_SUCCESS;
}
static int uart_reconfigure_timeouts_polling(serial_port sp) {
if (__atomic_test_and_set(&newtimeout_nopending, __ATOMIC_SEQ_CST) != 0)
return PM3_SUCCESS;
serial_port_windows *spw; serial_port_windows *spw;
spw = (serial_port_windows *)sp; spw = (serial_port_windows *)sp;
spw->ct.ReadIntervalTimeout = value; spw->ct.ReadIntervalTimeout = value;
@ -103,7 +115,8 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
return INVALID_SERIAL_PORT; return INVALID_SERIAL_PORT;
} }
uart_reconfigure_timeouts(sp, UART_FPC_CLIENT_RX_TIMEOUT_MS); uart_reconfigure_timeouts(UART_FPC_CLIENT_RX_TIMEOUT_MS);
uart_reconfigure_timeouts_polling(sp);
if (!uart_set_speed(sp, speed)) { if (!uart_set_speed(sp, speed)) {
// try fallback automatically // try fallback automatically
@ -176,6 +189,7 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin
} }
int uart_send(const serial_port sp, const uint8_t *p_tx, const uint32_t len) { int uart_send(const serial_port sp, const uint8_t *p_tx, const uint32_t len) {
uart_reconfigure_timeouts_polling(sp);
DWORD txlen = 0; DWORD txlen = 0;
int res = WriteFile(((serial_port_windows *)sp)->hPort, p_tx, len, &txlen, NULL); int res = WriteFile(((serial_port_windows *)sp)->hPort, p_tx, len, &txlen, NULL);
if (res) if (res)