MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / lwip_select

Function lwip_select

components/net/lwip/lwip-2.1.2/src/api/sockets.c:1985–2188  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1983#endif /* LWIP_NETCONN_FULLDUPLEX */
1984
1985int
1986lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
1987 struct timeval *timeout)
1988{
1989 u32_t waitres = 0;
1990 int nready;
1991 fd_set lreadset, lwriteset, lexceptset;
1992 u32_t msectimeout;
1993 int i;
1994 int maxfdp2;
1995#if LWIP_NETCONN_SEM_PER_THREAD
1996 int waited = 0;
1997#endif
1998#if LWIP_NETCONN_FULLDUPLEX
1999 fd_set used_sockets;
2000#endif
2001 SYS_ARCH_DECL_PROTECT(lev);
2002
2003 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select(%d, %p, %p, %p, tvsec=%"S32_F" tvusec=%"S32_F")\n",
2004 maxfdp1, (void *)readset, (void *) writeset, (void *) exceptset,
2005 timeout ? (s32_t)timeout->tv_sec : (s32_t) - 1,
2006 timeout ? (s32_t)timeout->tv_usec : (s32_t) - 1));
2007
2008 if ((maxfdp1 < 0) || (maxfdp1 > LWIP_SELECT_MAXNFDS)) {
2009 set_errno(EINVAL);
2010 return -1;
2011 }
2012
2013 lwip_select_inc_sockets_used(maxfdp1, readset, writeset, exceptset, &used_sockets);
2014
2015 /* Go through each socket in each list to count number of sockets which
2016 currently match */
2017 nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
2018
2019 if (nready < 0) {
2020 /* one of the sockets in one of the fd_sets was invalid */
2021 set_errno(EBADF);
2022 lwip_select_dec_sockets_used(maxfdp1, &used_sockets);
2023 return -1;
2024 } else if (nready > 0) {
2025 /* one or more sockets are set, no need to wait */
2026 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready));
2027 } else {
2028 /* If we don't have any current events, then suspend if we are supposed to */
2029 if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) {
2030 LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n"));
2031 /* This is OK as the local fdsets are empty and nready is zero,
2032 or we would have returned earlier. */
2033 } else {
2034 /* None ready: add our semaphore to list:
2035 We don't actually need any dynamic memory. Our entry on the
2036 list is only valid while we are in this function, so it's ok
2037 to use local variables (unless we're running in MPU compatible
2038 mode). */
2039 API_SELECT_CB_VAR_DECLARE(select_cb);
2040 API_SELECT_CB_VAR_ALLOC(select_cb, set_errno(ENOMEM); lwip_select_dec_sockets_used(maxfdp1, &used_sockets); return -1);
2041 memset(&API_SELECT_CB_VAR_REF(select_cb), 0, sizeof(struct lwip_select_cb));
2042

Callers 6

START_TESTFunction · 0.50
tcp_client_entryFunction · 0.50
tcp_server_entryFunction · 0.50
udp_client_entryFunction · 0.50
udp_server_entryFunction · 0.50

Calls 10

lwip_link_select_cbFunction · 0.85
done_socketFunction · 0.85
lwip_unlink_select_cbFunction · 0.85
lwip_selscanFunction · 0.70
sys_sem_newFunction · 0.50
sys_arch_sem_waitFunction · 0.50
sys_sem_freeFunction · 0.50

Tested by 2

START_TESTFunction · 0.40