/** * Wait (forever) for a message to arrive in an mbox. * While waiting, timeouts are processed. * * @param mbox the mbox to fetch the message from * @param msg the place to store the message */ void sys_timeouts_mbox_fetch_p(u8 timeo_assigned, sys_mbox_t mbox, void **msg) { u32_t time_needed; struct sys_timeo *tmptimeout; sys_timeout_handler handler; void *arg; struct sys_timeo **timeo = &next_timeout[timeo_assigned]; again: if (!(*timeo)) { time_needed = sys_arch_mbox_fetch(mbox, msg, 0); } else { if ((*timeo)->time > 0) { time_needed = sys_arch_mbox_fetch(mbox, msg, (*timeo)->time); } else { time_needed = SYS_ARCH_TIMEOUT; } if (time_needed == SYS_ARCH_TIMEOUT) { /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message could be fetched. We should now call the timeout handler and deallocate the memory allocated for the timeout. */ tmptimeout = *timeo; *timeo = tmptimeout->next; handler = tmptimeout->h; arg = tmptimeout->arg; #if LWIP_DEBUG_TIMERNAMES if (handler != NULL) { LWIP_DEBUGF(TIMERS_DEBUG, ("stmf calling h=%s arg=%p\n", tmptimeout->handler_name, arg)); } #endif /* LWIP_DEBUG_TIMERNAMES */ memp_free(MEMP_SYS_TIMEOUT, tmptimeout); if (handler != NULL) { /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the timeout handler function. */ LOCK_TCPIP_CORE(); handler(arg); UNLOCK_TCPIP_CORE(); } LWIP_TCPIP_THREAD_ALIVE(); /* We try again to fetch a message from the mbox. */ goto again; } else { /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout occured. The time variable is set to the number of milliseconds we waited for the message. */ if (time_needed < (*timeo)->time) { (*timeo)->time -= time_needed; } else { (*timeo)->time = 0; } } } }
err_t netconn_delete(struct netconn *conn) { struct api_msg *msg; void *mem; if (conn == NULL) { return ERR_OK; } if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) { return ERR_MEM; } msg->type = API_MSG_DELCONN; msg->msg.conn = conn; api_msg_post(msg); sys_mbox_fetch(conn->mbox, NULL); memp_free(MEMP_API_MSG, msg); /* Drain the recvmbox. */ if (conn->recvmbox != SYS_MBOX_NULL) { while (sys_arch_mbox_fetch(conn->recvmbox, &mem, 1) != SYS_ARCH_TIMEOUT) { if (conn->type == NETCONN_TCP) { if(mem != NULL) pbuf_free((struct pbuf *)mem); } else { netbuf_delete((struct netbuf *)mem); } } sys_mbox_free(conn->recvmbox); conn->recvmbox = SYS_MBOX_NULL; } /* Drain the acceptmbox. */ if (conn->acceptmbox != SYS_MBOX_NULL) { while (sys_arch_mbox_fetch(conn->acceptmbox, &mem, 1) != SYS_ARCH_TIMEOUT) { netconn_delete((struct netconn *)mem); } sys_mbox_free(conn->acceptmbox); conn->acceptmbox = SYS_MBOX_NULL; } sys_mbox_free(conn->mbox); conn->mbox = SYS_MBOX_NULL; if (conn->sem != SYS_SEM_NULL) { sys_sem_free(conn->sem); } /* conn->sem = SYS_SEM_NULL;*/ memp_free(MEMP_NETCONN, conn); return ERR_OK; }
/** * Wait (forever) for a message to arrive in an mbox. * While waiting, timeouts (for this thread) are processed. * * @param mbox the mbox to fetch the message from * @param msg the place to store the message */ void sys_mbox_fetch(sys_mbox_t mbox, void **msg) { u32_t time_needed; struct sys_timeouts *timeouts; struct sys_timeo *tmptimeout; sys_timeout_handler h; void *arg; again: timeouts = sys_arch_timeouts(); if (!timeouts || !timeouts->next) { UNLOCK_TCPIP_CORE(); time_needed = sys_arch_mbox_fetch(mbox, msg, 0); LOCK_TCPIP_CORE(); } else { if (timeouts->next->time > 0) { UNLOCK_TCPIP_CORE(); time_needed = sys_arch_mbox_fetch(mbox, msg, timeouts->next->time); LOCK_TCPIP_CORE(); } else { time_needed = SYS_ARCH_TIMEOUT; } if (time_needed == SYS_ARCH_TIMEOUT) { /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message could be fetched. We should now call the timeout handler and deallocate the memory allocated for the timeout. */ tmptimeout = timeouts->next; timeouts->next = tmptimeout->next; h = tmptimeout->h; arg = tmptimeout->arg; memp_free(MEMP_SYS_TIMEOUT, tmptimeout); if (h != NULL) { LWIP_DEBUGF(SYS_DEBUG, ("smf calling h=%p(%p)\r\n", *(void**)&h, arg)); h(arg); } /* We try again to fetch a message from the mbox. */ goto again; } else { /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout occured. The time variable is set to the number of milliseconds we waited for the message. */ if (time_needed < timeouts->next->time) { timeouts->next->time -= time_needed; } else { timeouts->next->time = 0; } } } }
err_t netconn_delete(struct netconn *conn) { struct api_msg msg; void *mem; //fprintf(stderr, "netconn_delete %p\n",conn); if (conn == NULL) { return ERR_OK; } msg.type = API_MSG_DELCONN; msg.msg.conn = conn; api_msg_post(conn->stack, &msg); sys_mbox_fetch(conn->mbox, NULL); /* Drain the recvmbox. */ if (conn->recvmbox != SYS_MBOX_NULL) { while (sys_arch_mbox_fetch(conn->recvmbox, &mem, 1) != SYS_ARCH_TIMEOUT) { if (conn->type == NETCONN_TCP) { if (mem != NULL) pbuf_free((struct pbuf *)mem); } else { netbuf_delete((struct netbuf *)mem); } } sys_mbox_free(conn->recvmbox); conn->recvmbox = SYS_MBOX_NULL; } /* Drain the acceptmbox. */ if (conn->acceptmbox != SYS_MBOX_NULL) { while (sys_arch_mbox_fetch(conn->acceptmbox, &mem, 1) != SYS_ARCH_TIMEOUT) { netconn_delete((struct netconn *)mem); } sys_mbox_free(conn->acceptmbox); conn->acceptmbox = SYS_MBOX_NULL; } sys_mbox_free(conn->mbox); conn->mbox = SYS_MBOX_NULL; if (conn->sem != SYS_SEM_NULL) { sys_sem_free(conn->sem); } conn->sem = SYS_SEM_NULL; /* this should not be commented out!*/ memp_free(MEMP_NETCONN, conn); return ERR_OK; }
/*-----------------------------------------------------------------------------------*/ void sys_mbox_fetch(sys_mbox_t mbox, void **msg) { uint16_t time; struct sys_timeouts *timeouts; struct sys_timeout *tmptimeout; sys_timeout_handler h; void *arg; again: timeouts = sys_arch_timeouts(); if(timeouts->next == NULL) { sys_arch_mbox_fetch(mbox, msg, 0); } else { if(timeouts->next->time > 0) { time = sys_arch_mbox_fetch(mbox, msg, timeouts->next->time); } else { time = 0; } if(time == 0) { /* If time == 0, a timeout occured before a message could be fetched. We should now call the timeout handler and deallocate the memory allocated for the timeout. */ tmptimeout = timeouts->next; timeouts->next = tmptimeout->next; h = tmptimeout->h; arg = tmptimeout->arg; memp_free(MEMP_SYS_TIMEOUT, tmptimeout); h(arg); /* We try again to fetch a message from the mbox. */ goto again; } else { /* If time > 0, a message was received before the timeout occured. The time variable is set to the number of microseconds we waited for the message. */ if(time <= timeouts->next->time) { timeouts->next->time -= time; } else { timeouts->next->time = 0; } } } }
void tcpip_thread (void *arg) { struct tcpip_msg *msg; //while(1) { sys_arch_mbox_fetch(mbox, (void *)&msg,1); if(msg != NULL) { switch (msg->type) { case TCPIP_MSG_API: LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg)); api_msg_input(msg->msg.apimsg); break; case TCPIP_MSG_INPUT: break; case TCPIP_MSG_CALLBACK: LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg)); msg->msg.cb.f(msg->msg.cb.ctx); break; default: break; } memp_free(MEMP_TCPIP_MSG, msg); } } }
static void task1(void *pdata) { int timeout; int i; INT8U err; char *p; while(1) { dprintf("task1,%s\r\n",pdata); //dprintf("sys_jiffies:%d\r\n",sys_jiffies()); //dprintf("sys_now:%dms\r\n",sys_now()); //p = OSQPend(mbox, 1, &err); timeout = sys_arch_mbox_fetch(&sys_mbox, (void **)&p, 0); sys_mutex_lock(&sys_mutex); if(timeout != -1) dprintf("task1 received mbox: %s\r\n",p); sys_mutex_unlock(&sys_mutex); //else //OSTaskResume(9); #if 0 if(err != OS_NO_ERR) { dprintf("task1 OSQPend err:%d\r\n",err); OSTaskResume(9); } else dprintf("task1 received mbox: %s\r\n",p); #endif } }
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg) { if (sys_arch_mbox_fetch(mbox, msg, 1) != SYS_ARCH_TIMEOUT) return 0; else return SYS_MBOX_EMPTY; }
/** * Wait (forever) for a message to arrive in an mbox. * While waiting, timeouts are processed. * * @param mbox the mbox to fetch the message from * @param msg the place to store the message */ void sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg) { u32_t sleeptime; again: if (!next_timeout) { sys_arch_mbox_fetch(mbox, msg, 0); return; } sleeptime = sys_timeouts_sleeptime(); if (sleeptime == 0 || sys_arch_mbox_fetch(mbox, msg, sleeptime) == SYS_ARCH_TIMEOUT) { /* If a SYS_ARCH_TIMEOUT value is returned, a timeout occurred before a message could be fetched. */ sys_check_timeouts(); /* We try again to fetch a message from the mbox. */ goto again; } }
BOOL xMBPortEventGet( eMBEventType * eEvent ) { void *peMailBoxEvent; BOOL xEventHappend = FALSE; u32_t uiTimeSpent; uiTimeSpent = sys_arch_mbox_fetch( xMailBox, &peMailBoxEvent, MB_POLL_CYCLETIME ); if( uiTimeSpent != SYS_ARCH_TIMEOUT ) { *eEvent = *( eMBEventType * ) peMailBoxEvent; eMailBoxEvent = EV_READY; xEventHappend = TRUE; } return xEventHappend; }
/* Deallocates a mailbox. If there are messages still present in the mailbox when the mailbox is deallocated, it is an indication of a programming error in lwIP and the developer should be notified. */ void sys_mbox_free( sys_mbox_t mbox ) { void *msg; LWIP_ASSERT( "sys_mbox_free: mbox != SYS_MBOX_NULL", mbox != SYS_MBOX_NULL ); if( mbox != SYS_MBOX_NULL ) { while( uxQueueMessagesWaiting( mbox ) != 0 ) { if( sys_arch_mbox_fetch( mbox, &msg, 1 ) != SYS_ARCH_TIMEOUT ) { LWIP_ASSERT( "sys_mbox_free: memory leak (msg != NULL)", msg == NULL ); } } vQueueDelete( mbox ); #ifdef SYS_STATS vPortEnterCritical( ); lwip_stats.sys.mbox.used--; vPortExitCritical( ); #endif } }
u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg) { return sys_arch_mbox_fetch(mbox, msg, 0); }
u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg) { /* required in lwIP-1.3.0. Initial naive implementation: */ return sys_arch_mbox_fetch(mbox, msg, 1); }
/** * Wait (forever) for a message to arrive in an mbox. * While waiting, timeouts are processed. * * @param mbox the mbox to fetch the message from * @param msg the place to store the message */ void sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg) { printf("sys_timeouts_mbox_fetch()\n"); u32_t time_needed; struct sys_timeo *tmptimeout; sys_timeout_handler handler; void *arg; again: if (!next_timeout) { printf("No Timeout, sys_arch_mbox_fetch(mbox, msg, 0)\n"); time_needed = sys_arch_mbox_fetch(mbox, msg, 0); } else { if (next_timeout->time > 0) { //printf("Have timeout, fetching with timeout of %i\n", next_timeout->time); time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time); } else { //printf("No time! timing out!\n"); time_needed = SYS_ARCH_TIMEOUT; } //printf("time_needed: %i (timeout=%i)\n", time_needed, SYS_ARCH_TIMEOUT); if (time_needed == SYS_ARCH_TIMEOUT) { /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message could be fetched. We should now call the timeout handler and deallocate the memory allocated for the timeout. */ tmptimeout = next_timeout; next_timeout = tmptimeout->next; handler = tmptimeout->h; arg = tmptimeout->arg; #if LWIP_DEBUG_TIMERNAMES if (handler != NULL) { LWIP_DEBUGF(TIMERS_DEBUG, ("stmf calling h=%s arg=%p\n", tmptimeout->handler_name, arg)); } #endif /* LWIP_DEBUG_TIMERNAMES */ memp_free(MEMP_SYS_TIMEOUT, tmptimeout); if (handler != NULL) { /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the timeout handler function. */ LOCK_TCPIP_CORE(); handler(arg); UNLOCK_TCPIP_CORE(); } LWIP_TCPIP_THREAD_ALIVE(); /* We try again to fetch a message from the mbox. */ //printf("Starting over, we didnt get a message\n"); goto again; } else { /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout occured. The time variable is set to the number of milliseconds we waited for the message. */ if (time_needed < next_timeout->time) { next_timeout->time -= time_needed; } else { next_timeout->time = 0; } } } }