| 1983 | #endif /* LWIP_NETCONN_FULLDUPLEX */ |
| 1984 | |
| 1985 | int |
| 1986 | lwip_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 | |