summaryrefslogtreecommitdiff
path: root/extmod/modlwip.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-07-03 16:22:48 +1000
committerDamien George <damien.p.george@gmail.com>2019-07-03 16:22:48 +1000
commitc60caf19951c8326be9c3b6f3b016a4d21f69276 (patch)
tree0598142327985d11306fd4eb4f793862f9f70bda /extmod/modlwip.c
parent1d6cb6357a2cde40822712211fb51c4a4bbe41cf (diff)
extmod/modlwip: Use mp_sched_schedule to schedule socket callbacks.
The helper function exec_user_callback executes within the context of an lwIP C callback, and the user (Python) callback to be scheduled may want to perform further TCP/IP actions, so the latter should be scheduled to run outside the lwIP context (otherwise it's effectively a "hard IRQ" and such callbacks have lots of restrictions).
Diffstat (limited to 'extmod/modlwip.c')
-rw-r--r--extmod/modlwip.c24
1 files changed, 5 insertions, 19 deletions
diff --git a/extmod/modlwip.c b/extmod/modlwip.c
index aed69f920..be932b6a9 100644
--- a/extmod/modlwip.c
+++ b/extmod/modlwip.c
@@ -356,7 +356,8 @@ STATIC void lwip_socket_free_incoming(lwip_socket_obj_t *socket) {
static inline void exec_user_callback(lwip_socket_obj_t *socket) {
if (socket->callback != MP_OBJ_NULL) {
- mp_call_function_1_protected(socket->callback, MP_OBJ_FROM_PTR(socket));
+ // Schedule the user callback to execute outside the lwIP context
+ mp_sched_schedule(socket->callback, MP_OBJ_FROM_PTR(socket));
}
}
@@ -446,18 +447,6 @@ STATIC err_t _lwip_tcp_recv_unaccepted(void *arg, struct tcp_pcb *pcb, struct pb
return ERR_BUF;
}
-// "Poll" (idle) callback to be called ASAP after accept callback
-// to execute Python callback function, as it can't be executed
-// from accept callback itself.
-STATIC err_t _lwip_tcp_accept_finished(void *arg, struct tcp_pcb *pcb)
-{
- // The ->connected entry of the pcb holds the listening socket of the accept
- lwip_socket_obj_t *socket = (lwip_socket_obj_t*)pcb->connected;
- tcp_poll(pcb, NULL, 0);
- exec_user_callback(socket);
- return ERR_OK;
-}
-
// Callback for incoming tcp connections.
STATIC err_t _lwip_tcp_accept(void *arg, struct tcp_pcb *newpcb, err_t err) {
// err can be ERR_MEM to notify us that there was no memory for an incoming connection
@@ -476,12 +465,9 @@ STATIC err_t _lwip_tcp_accept(void *arg, struct tcp_pcb *newpcb, err_t err) {
if (++socket->incoming.connection.iput >= socket->incoming.connection.alloc) {
socket->incoming.connection.iput = 0;
}
- if (socket->callback != MP_OBJ_NULL) {
- // Schedule accept callback to be called when lwIP is done
- // with processing this incoming connection on its side and
- // is idle.
- tcp_poll(newpcb, _lwip_tcp_accept_finished, 1);
- }
+
+ // Schedule user accept callback
+ exec_user_callback(socket);
// Set the error callback to handle the case of a dropped connection before we
// have a chance to take it off the accept queue.