api_lib.c, api_msg.c, tcpip.c: integrate sys_mbox_fetch(conn->mbox, NULL) calls from api_lib.c to tcpip.c's tcpip_apimsg(). Now, use a local variable and not a dynamic one from memp to send tcpip_msg to tcpip_thread in a synchrone call. Free tcpip_msg from tcpip_apimsg is not done in tcpip_thread. This give a faster and more reliable communication between api_lib and tcpip.

This commit is contained in:
fbernon 2007-03-21 16:38:58 +00:00
parent 766159e27e
commit 3eb38d7611
4 changed files with 108 additions and 121 deletions

View File

@ -70,6 +70,14 @@ HISTORY
* api_lib.c: Use memcpy in netbuf_copy_partial. * api_lib.c: Use memcpy in netbuf_copy_partial.
++ Bug fixes: ++ Bug fixes:
2007-03-21 Frédéric Bernon
* api_lib.c, api_msg.c, tcpip.c: integrate sys_mbox_fetch(conn->mbox, NULL) calls from
api_lib.c to tcpip.c's tcpip_apimsg(). Now, use a local variable and not a
dynamic one from memp to send tcpip_msg to tcpip_thread in a synchrone call.
Free tcpip_msg from tcpip_apimsg is not done in tcpip_thread. This give a
faster and more reliable communication between api_lib and tcpip.
2007-03-21 Frédéric Bernon 2007-03-21 Frédéric Bernon
* opt.h: Add LWIP_NETIF_CALLBACK (to avoid compiler warning) and set it to 0. * opt.h: Add LWIP_NETIF_CALLBACK (to avoid compiler warning) and set it to 0.

View File

@ -226,17 +226,16 @@ netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u16_t proto,
} }
conn->state = NETCONN_NONE; conn->state = NETCONN_NONE;
conn->socket = 0; conn->socket = 0;
conn->callback = callback;
conn->recv_avail = 0;
#if LWIP_SO_RCVTIMEO #if LWIP_SO_RCVTIMEO
conn->recv_timeout = 0; conn->recv_timeout = 0;
#endif /* LWIP_SO_RCVTIMEO */ #endif /* LWIP_SO_RCVTIMEO */
conn->callback = callback;
conn->recv_avail = 0;
msg.type = API_MSG_NEWCONN; msg.type = API_MSG_NEWCONN;
msg.msg.msg.bc.port = proto; /* misusing the port field */ msg.msg.msg.bc.port = proto; /* misusing the port field */
msg.msg.conn = conn; msg.msg.conn = conn;
api_msg_post(&msg); api_msg_post(&msg);
sys_mbox_fetch(conn->mbox, NULL);
if ( conn->err != ERR_OK ) { if ( conn->err != ERR_OK ) {
memp_free(MEMP_NETCONN, conn); memp_free(MEMP_NETCONN, conn);
@ -274,7 +273,6 @@ netconn_delete(struct netconn *conn)
msg.type = API_MSG_DELCONN; msg.type = API_MSG_DELCONN;
msg.msg.conn = conn; msg.msg.conn = conn;
api_msg_post(&msg); api_msg_post(&msg);
sys_mbox_fetch(conn->mbox, NULL);
/* Drain the recvmbox. */ /* Drain the recvmbox. */
if (conn->recvmbox != SYS_MBOX_NULL) { if (conn->recvmbox != SYS_MBOX_NULL) {
@ -296,17 +294,18 @@ netconn_delete(struct netconn *conn)
while (sys_arch_mbox_fetch(conn->acceptmbox, &mem, 1) != SYS_ARCH_TIMEOUT) { while (sys_arch_mbox_fetch(conn->acceptmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
netconn_delete((struct netconn *)mem); netconn_delete((struct netconn *)mem);
} }
sys_mbox_free(conn->acceptmbox); sys_mbox_free(conn->acceptmbox);
conn->acceptmbox = SYS_MBOX_NULL; conn->acceptmbox = SYS_MBOX_NULL;
} }
sys_mbox_free(conn->mbox); sys_mbox_free(conn->mbox);
conn->mbox = SYS_MBOX_NULL; conn->mbox = SYS_MBOX_NULL;
if (conn->sem != SYS_SEM_NULL) { if (conn->sem != SYS_SEM_NULL) {
sys_sem_free(conn->sem); sys_sem_free(conn->sem);
/* conn->sem = SYS_SEM_NULL; */
} }
/* conn->sem = SYS_SEM_NULL;*/
memp_free(MEMP_NETCONN, conn); memp_free(MEMP_NETCONN, conn);
return ERR_OK; return ERR_OK;
} }
@ -389,7 +388,6 @@ netconn_bind(struct netconn *conn, struct ip_addr *addr,
msg.msg.msg.bc.ipaddr = addr; msg.msg.msg.bc.ipaddr = addr;
msg.msg.msg.bc.port = port; msg.msg.msg.bc.port = port;
api_msg_post(&msg); api_msg_post(&msg);
sys_mbox_fetch(conn->mbox, NULL);
return conn->err; return conn->err;
} }
@ -404,7 +402,6 @@ netconn_connect(struct netconn *conn, struct ip_addr *addr,
return ERR_VAL; return ERR_VAL;
} }
if (conn->recvmbox == SYS_MBOX_NULL) { if (conn->recvmbox == SYS_MBOX_NULL) {
if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) { if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
return ERR_MEM; return ERR_MEM;
@ -416,7 +413,6 @@ netconn_connect(struct netconn *conn, struct ip_addr *addr,
msg.msg.msg.bc.ipaddr = addr; msg.msg.msg.bc.ipaddr = addr;
msg.msg.msg.bc.port = port; msg.msg.msg.bc.port = port;
api_msg_post(&msg); api_msg_post(&msg);
sys_mbox_fetch(conn->mbox, NULL);
return conn->err; return conn->err;
} }
@ -432,7 +428,6 @@ netconn_disconnect(struct netconn *conn)
msg.type = API_MSG_DISCONNECT; msg.type = API_MSG_DISCONNECT;
msg.msg.conn = conn; msg.msg.conn = conn;
api_msg_post(&msg); api_msg_post(&msg);
sys_mbox_fetch(conn->mbox, NULL);
return conn->err; return conn->err;
} }
@ -456,7 +451,6 @@ netconn_listen(struct netconn *conn)
msg.type = API_MSG_LISTEN; msg.type = API_MSG_LISTEN;
msg.msg.conn = conn; msg.msg.conn = conn;
api_msg_post(&msg); api_msg_post(&msg);
sys_mbox_fetch(conn->mbox, NULL);
return conn->err; return conn->err;
} }
@ -504,7 +498,6 @@ netconn_recv(struct netconn *conn)
return NULL; return NULL;
} }
buf = memp_malloc(MEMP_NETBUF); buf = memp_malloc(MEMP_NETBUF);
if (buf == NULL) { if (buf == NULL) {
@ -549,8 +542,6 @@ netconn_recv(struct netconn *conn)
msg.msg.msg.len = 1; msg.msg.msg.len = 1;
} }
api_msg_post(&msg); api_msg_post(&msg);
sys_mbox_fetch(conn->mbox, NULL);
} else { } else {
#if LWIP_SO_RCVTIMEO #if LWIP_SO_RCVTIMEO
sys_mbox_fetch_timeout(conn->recvmbox, (void *)&buf, conn->recv_timeout); sys_mbox_fetch_timeout(conn->recvmbox, (void *)&buf, conn->recv_timeout);
@ -565,12 +556,8 @@ netconn_recv(struct netconn *conn)
} }
} }
LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv: received %p (err %d)\n", (void *)buf, conn->err)); LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv: received %p (err %d)\n", (void *)buf, conn->err));
return buf; return buf;
} }
@ -592,8 +579,6 @@ netconn_send(struct netconn *conn, struct netbuf *buf)
msg.msg.conn = conn; msg.msg.conn = conn;
msg.msg.msg.p = buf->p; msg.msg.msg.p = buf->p;
api_msg_post(&msg); api_msg_post(&msg);
sys_mbox_fetch(conn->mbox, NULL);
return conn->err; return conn->err;
} }
@ -614,7 +599,6 @@ netconn_write(struct netconn *conn, const void *dataptr, u16_t size, u8_t copy)
msg.type = API_MSG_WRITE; msg.type = API_MSG_WRITE;
msg.msg.conn = conn; msg.msg.conn = conn;
conn->state = NETCONN_WRITE; conn->state = NETCONN_WRITE;
while (conn->err == ERR_OK && size > 0) { while (conn->err == ERR_OK && size > 0) {
msg.msg.msg.w.dataptr = dataptr; msg.msg.msg.w.dataptr = dataptr;
@ -641,7 +625,6 @@ netconn_write(struct netconn *conn, const void *dataptr, u16_t size, u8_t copy)
LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_write: writing %d bytes (%d)\n", len, copy)); LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_write: writing %d bytes (%d)\n", len, copy));
msg.msg.msg.w.len = len; msg.msg.msg.w.len = len;
api_msg_post(&msg); api_msg_post(&msg);
sys_mbox_fetch(conn->mbox, NULL);
if (conn->err == ERR_OK) { if (conn->err == ERR_OK) {
dataptr = (void *)((u8_t *)dataptr + len); dataptr = (void *)((u8_t *)dataptr + len);
size -= len; size -= len;
@ -672,7 +655,6 @@ netconn_close(struct netconn *conn)
msg.type = API_MSG_CLOSE; msg.type = API_MSG_CLOSE;
msg.msg.conn = conn; msg.msg.conn = conn;
api_msg_post(&msg); api_msg_post(&msg);
sys_mbox_fetch(conn->mbox, NULL);
if (conn->err == ERR_MEM && if (conn->err == ERR_MEM &&
conn->sem != SYS_SEM_NULL) { conn->sem != SYS_SEM_NULL) {
sys_sem_wait(conn->sem); sys_sem_wait(conn->sem);
@ -700,15 +682,14 @@ netconn_join_leave_group (struct netconn *conn,
return conn->err; return conn->err;
} }
msg.type = API_MSG_JOIN_LEAVE;
msg.msg.conn = conn;
ipaddr[0] = multiaddr; ipaddr[0] = multiaddr;
ipaddr[1] = interface; ipaddr[1] = interface;
msg.type = API_MSG_JOIN_LEAVE;
msg.msg.conn = conn;
msg.msg.msg.bc.ipaddr = (struct ip_addr *)ipaddr; msg.msg.msg.bc.ipaddr = (struct ip_addr *)ipaddr;
msg.msg.msg.bc.port = join_or_leave; msg.msg.msg.bc.port = join_or_leave;
api_msg_post(&msg); api_msg_post(&msg);
sys_mbox_fetch(conn->mbox, NULL);
return conn->err; return conn->err;
} }
#endif /* LWIP_IGMP */ #endif /* LWIP_IGMP */

View File

@ -128,9 +128,9 @@ recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
if (p != NULL) { if (p != NULL) {
len = p->tot_len; len = p->tot_len;
conn->recv_avail += len; conn->recv_avail += len;
} } else {
else
len = 0; len = 0;
}
/* Register event with callback */ /* Register event with callback */
if (conn->callback) if (conn->callback)
(*conn->callback)(conn, NETCONN_EVT_RCVPLUS, len); (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, len);
@ -264,8 +264,7 @@ accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
newconn->acceptmbox = SYS_MBOX_NULL; newconn->acceptmbox = SYS_MBOX_NULL;
newconn->err = err; newconn->err = err;
/* Register event with callback */ /* Register event with callback */
if (conn->callback) if (conn->callback) {
{
(*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0); (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0);
} }
/* We have to set the callback here even though /* We have to set the callback here even though
@ -387,8 +386,7 @@ do_delconn(struct api_msg_msg *msg)
} }
} }
/* Trigger select() in socket layer */ /* Trigger select() in socket layer */
if (msg->conn->callback) if (msg->conn->callback) {
{
(*msg->conn->callback)(msg->conn, NETCONN_EVT_RCVPLUS, 0); (*msg->conn->callback)(msg->conn, NETCONN_EVT_RCVPLUS, 0);
(*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDPLUS, 0); (*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDPLUS, 0);
} }
@ -451,8 +449,7 @@ do_bind(struct api_msg_msg *msg)
#endif /* LWIP_UDP */ #endif /* LWIP_UDP */
#if LWIP_TCP #if LWIP_TCP
case NETCONN_TCP: case NETCONN_TCP:
msg->conn->err = tcp_bind(msg->conn->pcb.tcp, msg->conn->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
msg->msg.bc.ipaddr, msg->msg.bc.port);
#endif /* LWIP_TCP */ #endif /* LWIP_TCP */
default: default:
break; break;
@ -739,8 +736,7 @@ do_write(struct api_msg_msg *msg)
} }
msg->conn->err = err; msg->conn->err = err;
if (msg->conn->callback) if (msg->conn->callback)
if (err == ERR_OK) if (err == ERR_OK) {
{
if (tcp_sndbuf(msg->conn->pcb.tcp) <= TCP_SNDLOWAT) if (tcp_sndbuf(msg->conn->pcb.tcp) <= TCP_SNDLOWAT)
(*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDMINUS, msg->msg.w.len); (*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDMINUS, msg->msg.w.len);
} }
@ -842,10 +838,12 @@ static api_msg_decode decode[API_MSG_MAX] = {
do_join_leave_group do_join_leave_group
#endif /* LWIP_IGMP */ #endif /* LWIP_IGMP */
}; };
void void
api_msg_input(struct api_msg *msg) api_msg_input(struct api_msg *msg)
{ { struct api_msg_msg msg_copy;
decode[msg->type](&(msg->msg)); msg_copy=msg->msg;
decode[msg->type](&msg_copy);
} }
err_t err_t

View File

@ -211,8 +211,11 @@ tcpip_thread(void *arg)
default: default:
break; break;
} }
if (msg->type!=TCPIP_MSG_API) {
memp_free(MEMP_TCPIP_MSG, msg); memp_free(MEMP_TCPIP_MSG, msg);
} }
}
} }
#if ETHARP_TCPIP_INPUT #if ETHARP_TCPIP_INPUT
@ -284,16 +287,13 @@ tcpip_callback(void (*f)(void *ctx), void *ctx)
err_t err_t
tcpip_apimsg(struct api_msg *apimsg) tcpip_apimsg(struct api_msg *apimsg)
{ {
struct tcpip_msg *msg; struct tcpip_msg msg;
if (mbox != SYS_MBOX_NULL) { if (mbox != SYS_MBOX_NULL) {
msg = memp_malloc(MEMP_TCPIP_MSG); msg.type = TCPIP_MSG_API;
if (msg == NULL) { msg.msg.apimsg = apimsg;
return ERR_MEM; sys_mbox_post(mbox, &msg);
} sys_mbox_fetch(apimsg->msg.conn->mbox, NULL);
msg->type = TCPIP_MSG_API;
msg->msg.apimsg = apimsg;
sys_mbox_post(mbox, msg);
return ERR_OK; return ERR_OK;
} }
return ERR_VAL; return ERR_VAL;