Skip to content

Cellular: Make AT_CellularStack socket array multi-thread safe #7857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions features/cellular/framework/AT/AT_CellularStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -166,15 +173,17 @@ 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
_at.lock();
if (sock_created) {
err = socket_close_impl(sock_id);
}

_socket[index] = NULL;
delete socket;

_at.unlock();

return err;
Expand Down
4 changes: 4 additions & 0 deletions features/cellular/framework/AT/AT_CellularStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {

// 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
Expand Down