Skip to content

Commit 686b779

Browse files
author
Cruz Monrreal
authored
Merge pull request #7857 from AriParkkila/cell-stack-mt
Cellular: Make AT_CellularStack socket array multi-thread safe
2 parents e2409bf + 4b223b6 commit 686b779

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

features/cellular/framework/AT/AT_CellularStack.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,18 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
9595

9696
int max_socket_count = get_max_socket_count();
9797

98+
_socket_mutex.lock();
99+
98100
if (!_socket) {
99101
if (socket_stack_init() != NSAPI_ERROR_OK) {
102+
_socket_mutex.unlock();
100103
return NSAPI_ERROR_NO_SOCKET;
101104
}
102105

103106
_socket = new CellularSocket*[max_socket_count];
104107
if (!_socket) {
105108
tr_error("No memory to open socket!");
109+
_socket_mutex.unlock();
106110
return NSAPI_ERROR_NO_SOCKET;
107111
}
108112
_socket_count = max_socket_count;
@@ -121,6 +125,7 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
121125

122126
if (index == -1) {
123127
tr_error("No socket found!");
128+
_socket_mutex.unlock();
124129
return NSAPI_ERROR_NO_SOCKET;
125130
}
126131

@@ -136,6 +141,8 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
136141
psock->proto = proto;
137142
*handle = psock;
138143

144+
_socket_mutex.unlock();
145+
139146
return NSAPI_ERROR_OK;
140147
}
141148

@@ -166,15 +173,17 @@ nsapi_error_t AT_CellularStack::socket_close(nsapi_socket_t handle)
166173
return err;
167174
}
168175

169-
_socket[index] = NULL;
170-
delete socket;
171176
err = NSAPI_ERROR_OK;
172177

173178
// Close the socket on the modem if it was created
174179
_at.lock();
175180
if (sock_created) {
176181
err = socket_close_impl(sock_id);
177182
}
183+
184+
_socket[index] = NULL;
185+
delete socket;
186+
178187
_at.unlock();
179188

180189
return err;

features/cellular/framework/AT/AT_CellularStack.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
165165

166166
// stack type from PDP context
167167
nsapi_ip_stack_t _stack_type;
168+
169+
private:
170+
// mutex for write/read to a _socket array, needed when multiple threads may open sockets simultaneously
171+
PlatformMutex _socket_mutex;
168172
};
169173

170174
} // namespace mbed

0 commit comments

Comments
 (0)