Cellular: Make AT_CellularStack socket array multi-thread safe

pull/7857/head
Ari Parkkila 2018-08-22 03:23:10 -07:00
parent deb905da1d
commit 4b223b6ff2
2 changed files with 15 additions and 2 deletions

View File

@ -95,14 +95,18 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
int max_socket_count = get_max_socket_count();
_socket_mutex.lock();
if (!_socket) {
if (socket_stack_init() != NSAPI_ERROR_OK) {
_socket_mutex.unlock();
return NSAPI_ERROR_NO_SOCKET;
}
_socket = new CellularSocket*[max_socket_count];
if (!_socket) {
tr_error("No memory to open socket!");
_socket_mutex.unlock();
return NSAPI_ERROR_NO_SOCKET;
}
_socket_count = max_socket_count;
@ -121,6 +125,7 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
if (index == -1) {
tr_error("No socket found!");
_socket_mutex.unlock();
return NSAPI_ERROR_NO_SOCKET;
}
@ -136,6 +141,8 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
psock->proto = proto;
*handle = psock;
_socket_mutex.unlock();
return NSAPI_ERROR_OK;
}
@ -166,8 +173,6 @@ nsapi_error_t AT_CellularStack::socket_close(nsapi_socket_t handle)
return err;
}
_socket[index] = NULL;
delete socket;
err = NSAPI_ERROR_OK;
// Close the socket on the modem if it was created
@ -175,6 +180,10 @@ nsapi_error_t AT_CellularStack::socket_close(nsapi_socket_t handle)
if (sock_created) {
err = socket_close_impl(sock_id);
}
_socket[index] = NULL;
delete socket;
_at.unlock();
return err;

View File

@ -165,6 +165,10 @@ protected:
// stack type from PDP context
nsapi_ip_stack_t _stack_type;
private:
// mutex for write/read to a _socket array, needed when multiple threads may open sockets simultaneously
PlatformMutex _socket_mutex;
};
} // namespace mbed