mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			Merge pull request #7857 from AriParkkila/cell-stack-mt
Cellular: Make AT_CellularStack socket array multi-thread safepull/7903/head
						commit
						686b77949f
					
				| 
						 | 
					@ -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();
 | 
					    int max_socket_count = get_max_socket_count();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _socket_mutex.lock();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!_socket) {
 | 
					    if (!_socket) {
 | 
				
			||||||
        if (socket_stack_init() != NSAPI_ERROR_OK) {
 | 
					        if (socket_stack_init() != NSAPI_ERROR_OK) {
 | 
				
			||||||
 | 
					            _socket_mutex.unlock();
 | 
				
			||||||
            return NSAPI_ERROR_NO_SOCKET;
 | 
					            return NSAPI_ERROR_NO_SOCKET;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        _socket = new CellularSocket*[max_socket_count];
 | 
					        _socket = new CellularSocket*[max_socket_count];
 | 
				
			||||||
        if (!_socket) {
 | 
					        if (!_socket) {
 | 
				
			||||||
            tr_error("No memory to open socket!");
 | 
					            tr_error("No memory to open socket!");
 | 
				
			||||||
 | 
					            _socket_mutex.unlock();
 | 
				
			||||||
            return NSAPI_ERROR_NO_SOCKET;
 | 
					            return NSAPI_ERROR_NO_SOCKET;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        _socket_count = max_socket_count;
 | 
					        _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) {
 | 
					    if (index == -1) {
 | 
				
			||||||
        tr_error("No socket found!");
 | 
					        tr_error("No socket found!");
 | 
				
			||||||
 | 
					        _socket_mutex.unlock();
 | 
				
			||||||
        return NSAPI_ERROR_NO_SOCKET;
 | 
					        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;
 | 
					    psock->proto = proto;
 | 
				
			||||||
    *handle = psock;
 | 
					    *handle = psock;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _socket_mutex.unlock();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return NSAPI_ERROR_OK;
 | 
					    return NSAPI_ERROR_OK;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -166,8 +173,6 @@ nsapi_error_t AT_CellularStack::socket_close(nsapi_socket_t handle)
 | 
				
			||||||
        return err;
 | 
					        return err;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _socket[index] = NULL;
 | 
					 | 
				
			||||||
    delete socket;
 | 
					 | 
				
			||||||
    err = NSAPI_ERROR_OK;
 | 
					    err = NSAPI_ERROR_OK;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Close the socket on the modem if it was created
 | 
					    // 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) {
 | 
					    if (sock_created) {
 | 
				
			||||||
        err = socket_close_impl(sock_id);
 | 
					        err = socket_close_impl(sock_id);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _socket[index] = NULL;
 | 
				
			||||||
 | 
					    delete socket;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _at.unlock();
 | 
					    _at.unlock();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return err;
 | 
					    return err;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -165,6 +165,10 @@ protected:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // stack type from PDP context
 | 
					    // stack type from PDP context
 | 
				
			||||||
    nsapi_ip_stack_t _stack_type;
 | 
					    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
 | 
					} // namespace mbed
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue