ESP8266: makes guarding variable for deferring events atomic

pull/9520/head
Veijo Pesonen 2019-01-29 15:19:41 +02:00
parent 7d3621dae4
commit 53c2879a5c
2 changed files with 9 additions and 6 deletions

View File

@ -26,6 +26,7 @@
#include "features/netsocket/nsapi_types.h"
#include "mbed_trace.h"
#include "platform/Callback.h"
#include "platform/mbed_critical.h"
#include "platform/mbed_debug.h"
#include "platform/mbed_wait_api.h"
@ -496,7 +497,7 @@ int ESP8266Interface::socket_close(void *handle)
_cbs[socket->id].callback = NULL;
_cbs[socket->id].data = NULL;
_cbs[socket->id].deferred = false;
core_util_atomic_store_u8(&_cbs[socket->id].deferred, false);
socket->connected = false;
_sock_i[socket->id].open = false;
@ -566,6 +567,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
{
nsapi_error_t status;
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
uint8_t expect_false = false;
if (!socket) {
return NSAPI_ERROR_NO_SOCKET;
@ -578,9 +580,10 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
status = _esp.send(socket->id, data, size);
if (status == NSAPI_ERROR_WOULD_BLOCK && !_cbs[socket->id].deferred && socket->proto == NSAPI_TCP) {
if (status == NSAPI_ERROR_WOULD_BLOCK
&& socket->proto == NSAPI_TCP
&& core_util_atomic_cas_u8(&_cbs[socket->id].deferred, &expect_false, true)) {
tr_debug("Postponing SIGIO from the device");
_cbs[socket->id].deferred = true;
_global_event_queue->call_in(50, callback(this, &ESP8266Interface::event_deferred));
} else if (status == NSAPI_ERROR_WOULD_BLOCK && socket->proto == NSAPI_UDP) {
status = NSAPI_ERROR_DEVICE_ERROR;
@ -734,8 +737,8 @@ void ESP8266Interface::event()
void ESP8266Interface::event_deferred()
{
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
if (_cbs[i].deferred && _cbs[i].callback) {
_cbs[i].deferred = false;
uint8_t expect_true = true;
if (core_util_atomic_cas_u8(&_cbs[i].deferred, &expect_true, false) && _cbs[i].callback) {
_cbs[i].callback(_cbs[i].data);
}
}

View File

@ -373,7 +373,7 @@ private:
struct {
void (*callback)(void *);
void *data;
bool deferred;
uint8_t deferred;
} _cbs[ESP8266_SOCKET_COUNT];
void event();
void event_deferred();