Merge branch 'feature/lwip_use_thread_saft_and_thread_sync' into 'master'

Add multi-thread support

See merge request sdk/ESP8266_RTOS_SDK!239
This commit is contained in:
Wu Jian Gang
2018-06-29 11:46:40 +08:00
7 changed files with 349 additions and 288 deletions

View File

@ -4,18 +4,24 @@
#ifdef SOCKETS_MT
#include "../../lwip/src/api/sockets.c"
#include "lwip/priv/api_msg.h"
#ifndef LWIP_SYNC_MT_SLEEP_MS
#define LWIP_SYNC_MT_SLEEP_MS 10
#define LWIP_SYNC_MT_SLEEP_MS 10
#endif
#ifndef SOCK_MT_DEBUG_LEVEL
#define SOCK_MT_DEBUG_LEVEL 255
#endif
typedef int (*lwip_io_mt_fn)(int , int );
typedef struct socket_conn_sync {
sys_sem_t *sem;
struct netconn *conn;
} socket_conn_sync_t;
enum sock_mt_stat{
typedef int (*lwip_io_mt_fn)(int, int );
enum sock_mt_stat {
SOCK_MT_STATE_NONE = 0,
SOCK_MT_STATE_BIND,
SOCK_MT_STATE_LISTEN,
@ -63,11 +69,11 @@ struct _sock_mt {
typedef struct _sock_mt sock_mt_t;
#if (SOCK_MT_DEBUG_LEVEL < 16)
#define SOCK_MT_DEBUG(level, ...) \
#define SOCK_MT_DEBUG(level, ...) \
if (level >= SOCK_MT_DEBUG_LEVEL) \
printf(__VA_ARGS__);
#else
#define SOCK_MT_DEBUG(level, ...)
#define SOCK_MT_DEBUG(level, ...)
#endif
#if 0
@ -252,7 +258,7 @@ static sock_mt_t sockets_mt[NUM_SOCKETS];
static int lwip_enter_mt_state(int s, int arg)
{
if(tryget_socket(s) == NULL ||
if (tryget_socket(s) == NULL ||
SOCK_MT_GET_STATE(s) != SOCK_MT_STATE_NONE ||
SOCK_MT_GET_WRITE_SEL(s))
return -1;
@ -265,7 +271,7 @@ static int lwip_enter_mt_state(int s, int arg)
static int lwip_enter_mt_recv(int s, int arg)
{
if(tryget_socket(s) == NULL ||
if (tryget_socket(s) == NULL ||
SOCK_MT_GET_READ_SEL(s))
return -1;
@ -276,7 +282,7 @@ static int lwip_enter_mt_recv(int s, int arg)
static int lwip_enter_mt_shutdown(int s, int arg)
{
if(tryget_socket(s) == NULL
if (tryget_socket(s) == NULL
|| SOCK_MT_GET_SHUTDOWN(s) != SOCK_MT_SHUTDOWN_NONE)
return -1;
@ -287,7 +293,7 @@ static int lwip_enter_mt_shutdown(int s, int arg)
static int lwip_enter_mt_close(int s, int arg)
{
if(tryget_socket(s) == NULL)
if (tryget_socket(s) == NULL)
return -1;
SOCK_MT_SET_SHUTDOWN(s, SOCK_MT_SHUTDOWN_OK);
@ -344,7 +350,7 @@ failed2:
}
failed1:
for (i--; i >=0; i--) {
if (FD_ISSET(i, read_set) ){
if (FD_ISSET(i, read_set) ) {
SOCK_MT_UNLOCK(i, SOCK_MT_RECV_LOCK);
SOCK_MT_RESET_READ_SEL(i);
}
@ -360,7 +366,7 @@ failed1:
static int lwip_enter_mt_ioctrl(int s, int arg)
{
if(tryget_socket(s) == NULL)
if (tryget_socket(s) == NULL)
return -1;
SOCK_MT_LOCK(s, SOCK_MT_IOCTRL_LOCK);
@ -462,7 +468,8 @@ static const lwip_io_mt_fn lwip_exit_mt_table[] = {
static void lwip_do_sync_send(void *arg)
{
struct netconn *conn = arg;
socket_conn_sync_t *sync = (socket_conn_sync_t *)arg;
struct netconn *conn = sync->conn;
SYS_ARCH_DECL_PROTECT(lev);
SYS_ARCH_PROTECT(lev);
@ -472,32 +479,24 @@ static void lwip_do_sync_send(void *arg)
}
conn->state = NETCONN_NONE;
SYS_ARCH_UNPROTECT(lev);
if (sys_sem_valid(&(conn->snd_op_completed)))
sys_sem_signal(&(conn->snd_op_completed));
/*
* if we use "op_completed" for its sync semaphore, then socket functions
* which are blocked by op_completed will be waked up.
*
* So we had better use a specific semaphore for the function, ioctrl_completed
* may be a good choise.
*/
sys_sem_signal(&(conn->ioctrl_completed));
sys_sem_signal(sync->sem);
}
static void lwip_do_sync_rst_state(void *arg)
{
struct netconn *conn = arg;
socket_conn_sync_t *sync = (socket_conn_sync_t *)arg;
struct netconn *conn = sync->conn;
SYS_ARCH_DECL_PROTECT(lev);
SYS_ARCH_PROTECT(lev);
conn->state = NETCONN_NONE;
SYS_ARCH_UNPROTECT(lev);
sys_sem_signal(&(conn->ioctrl_completed));
sys_sem_signal(sync->sem);
}
static void lwip_sync_state_mt(struct lwip_sock *sock , int state)
static void lwip_sync_state_mt(struct lwip_sock *sock, int state)
{
SOCK_MT_DEBUG(1, "sync state %d\n", state);
@ -508,13 +507,18 @@ static void lwip_sync_state_mt(struct lwip_sock *sock , int state)
break;
case SOCK_MT_STATE_SEND :
{
tcpip_callback(lwip_do_sync_send, sock->conn);
sys_arch_sem_wait(&sock->conn->ioctrl_completed, 0);
socket_conn_sync_t sync;
sync.conn = sock->conn;
sync.sem = sys_thread_sem_get();
tcpip_callback(lwip_do_sync_send, &sync);
sys_arch_sem_wait(sync.sem, 0);
break;
}
case SOCK_MT_STATE_CONNECT :
if (sys_sem_valid(&(sock->conn->op_completed)))
sys_sem_signal(&(sock->conn->op_completed));
if (sock->conn->current_msg && sys_sem_valid(sock->conn->current_msg->op_completed_sem))
sys_sem_signal(sock->conn->current_msg->op_completed_sem);
break;
default :
break;
@ -543,7 +547,6 @@ static void lwip_sync_mt(int s)
while (module < SOCK_MT_SELECT) {
extern void sys_arch_msleep(int ms);
int ret;
SOCK_MT_TRYLOCK(s, module, ret);
if (ret == ERR_OK) {
@ -582,8 +585,13 @@ static void lwip_sync_mt(int s)
sock = tryget_socket(s);
if (sock) {
tcpip_callback(lwip_do_sync_rst_state, sock->conn);
sys_arch_sem_wait(&sock->conn->ioctrl_completed, 0);
socket_conn_sync_t sync;
sync.conn = sock->conn;
sync.sem = sys_thread_sem_get();
tcpip_callback(lwip_do_sync_rst_state, &sync);
sys_arch_sem_wait(sync.sem, 0);
}
}
@ -733,7 +741,7 @@ int lwip_getsockopt_mt(int s, int level, int optname, void *optval, socklen_t *o
{
int ret;
if (optname == SO_ERROR ) {
if (optname == SO_ERROR) {
int retval = 0;
if (SOCK_MT_GET_SHUTDOWN(s) != SOCK_MT_SHUTDOWN_NONE)
@ -910,25 +918,4 @@ int lwip_select_mt(int maxfdp1, fd_set *readset, fd_set *writeset,
return ret;
}
#ifdef SOCKETS_TCP_TRACE
int lwip_trace_tcp(int s, int cmd, void *arg)
{
struct lwip_sock *sock;
u32_t *pbuf = (u32_t *)arg;
if (!(sock = tryget_socket(s)))
return -1;
ADD_TCP_SEND_BYTES_GET(sock->conn, &pbuf[0]);
ADD_TCP_SEND_BYTES_OK_GET(sock->conn, &pbuf[2]);
ADD_TCP_SEND_BYTES_NOMEM_GET(sock->conn, &pbuf[4]);
ADD_TCP_RECV_BYTES_GET(sock->conn, &pbuf[6]);
ADD_TCP_RECV_BYTES_ERR_GET(sock->conn, &pbuf[8]);
return 0;
}
#endif
#endif

View File

@ -3,8 +3,8 @@
#
COMPONENT_ADD_INCLUDEDIRS += include/lwip/apps \
lwip/src/include \
lwip/src/include/posix \
port/esp8266/include
port/esp8266/include \
include/lwip/apps/multi-threads
COMPONENT_SRCDIRS += apps/dhcpserver \
apps/multi-threads \

View File

@ -0,0 +1,33 @@
/**
* @file
* This file is a posix wrapper for lwip/errno.h.
*/
/*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
*/
#include "posix/errno.h"

View File

@ -0,0 +1,33 @@
/**
* @file
* This file is a posix wrapper for lwip/netdb.h.
*/
/*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
*/
#include "posix/netdb.h"

View File

@ -1,8 +1,41 @@
#ifndef _SOCKETS_MT_H_
#define _SOCKETS_MT_H_
/**
* @file
* This file is a posix wrapper for lwip/sockets.h.
*/
#ifdef SOCKETS_MT
/*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
*/
#ifndef _SOCKET_MT_H_
#define _SOCKET_MT_H_
#include "posix/sys/socket.h"
#if LWIP_COMPAT_SOCKETS == 3
int lwip_mt_init(void);
int lwip_socket_mt(int domain, int type, int protocol);
int lwip_bind_mt(int s, const struct sockaddr *name, socklen_t namelen);
@ -27,46 +60,26 @@ int lwip_close_mt(int s);
int lwip_select_mt(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout);
int lwip_fcntl_mt(int s, int cmd, int val);
#ifdef SOCKETS_TCP_TRACE
int lwip_trace_tcp(int s, int cmd, void *arg);
#endif
#if LWIP_COMPAT_SOCKETS
#ifdef SOCKETS_MT_DBUG
#define accept(a,b,c) lwip_accept_mt(a,b,c); printf("%s %d\n", __FUNCTION__, __LINE__);
#define bind(a,b,c) lwip_bind_mt(a,b,c); printf("%s %d\n", __FUNCTION__, __LINE__);
#define shutdown(a,b) lwip_shutdown_mt(a,b); printf("%s %d\n", __FUNCTION__, __LINE__);
#define closesocket(s) lwip_close_mt(s); printf("%s %d\n", __FUNCTION__, __LINE__);
#define connect(a,b,c) lwip_connect_mt(a,b,c); printf("%s %d\n", __FUNCTION__, __LINE__);
#define getsockname(a,b,c) lwip_getsockname_mt(a,b,c); printf("%s %d\n", __FUNCTION__, __LINE__);
#define getpeername(a,b,c) lwip_getpeername_mt(a,b,c); printf("%s %d\n", __FUNCTION__, __LINE__);
#define setsockopt(a,b,c,d,e) lwip_setsockopt_mt(a,b,c,d,e); printf("%s %d\n", __FUNCTION__, __LINE__);
#define getsockopt(a,b,c,d,e) lwip_getsockopt_mt(a,b,c,d,e); printf("%s %d\n", __FUNCTION__, __LINE__);
#define listen(a,b) lwip_listen_mt(a,b); printf("%s %d\n", __FUNCTION__, __LINE__);
#define recv(a,b,c,d) lwip_recv_mt(a,b,c,d); printf("%s %d\n", __FUNCTION__, __LINE__);
#define recvfrom(a,b,c,d,e,f) lwip_recvfrom_mt(a,b,c,d,e,f); printf("%s %d\n", __FUNCTION__, __LINE__);
#define send(a,b,c,d) lwip_send_mt(a,b,c,d); printf("%s %d\n", __FUNCTION__, __LINE__);
#define sendto(a,b,c,d,e,f) lwip_sendto_mt(a,b,c,d,e,f); printf("%s %d\n", __FUNCTION__, __LINE__);
#define socket(a,b,c) lwip_socket_mt(a,b,c); printf("%s %d\n", __FUNCTION__, __LINE__);
#define accept(a,b,c) lwip_accept_mt(a,b,c)
#define bind(a,b,c) lwip_bind_mt(a,b,c)
#define shutdown(a,b) lwip_shutdown_mt(a,b)
#define connect(a,b,c) lwip_connect_mt(a,b,c)
#define getsockname(a,b,c) lwip_getsockname_mt(a,b,c)
#define getpeername(a,b,c) lwip_getpeername_mt(a,b,c)
#define setsockopt(a,b,c,d,e) lwip_setsockopt_mt(a,b,c,d,e)
#define getsockopt(a,b,c,d,e) lwip_getsockopt_mt(a,b,c,d,e)
#define listen(a,b) lwip_listen_mt(a,b)
#define recv(a,b,c,d) lwip_recv_mt(a,b,c,d)
#define recvfrom(a,b,c,d,e,f) lwip_recvfrom_mt(a,b,c,d,e,f)
#define send(a,b,c,d) lwip_send_mt(a,b,c,d)
#define sendto(a,b,c,d,e,f) lwip_sendto_mt(a,b,c,d,e,f)
#define socket(a,b,c) lwip_socket_mt(a,b,c)
#define select(a,b,c,d,e) lwip_select_mt(a,b,c,d,e)
#define ioctlsocket(a,b,c) lwip_ioctl_mt(a,b,c) ; printf("%s %d\n", __FUNCTION__, __LINE__);
#if LWIP_POSIX_SOCKETS_IO_NAMES
#define read(a,b,c) lwip_read_mt(a,b,c)
#define write(a,b,c) lwip_write_mt(a,b,c)
#define close(s) lwip_close_mt(s)
#define fcntl(a,b,c) lwip_fcntl_mt(a,b,c)
#endif /* LWIP_POSIX_SOCKETS_IO_NAMES */
#else /* SOCKETS_MT_DBUG */
#define ioctlsocket(a,b,c) lwip_ioctl_mt(a,b,c)
#define accept(a,b,c) lwip_accept_mt(a,b,c)
#define bind(a,b,c) lwip_bind_mt(a,b,c)
#define shutdown(a,b) lwip_shutdown_mt(a,b)
#define closesocket(s) lwip_close_mt(s)
#define connect(a,b,c) lwip_connect_mt(a,b,c)
#define getsockname(a,b,c) lwip_getsockname_mt(a,b,c)
#define getpeername(a,b,c) lwip_getpeername_mt(a,b,c)
@ -87,12 +100,6 @@ int lwip_trace_tcp(int s, int cmd, void *arg);
#define close(s) lwip_close_mt(s)
#define fcntl(a,b,c) lwip_fcntl_mt(a,b,c)
#endif /* LWIP_POSIX_SOCKETS_IO_NAMES */
#endif /* SOCKETS_MT_DBUG */
#endif /* LWIP_COMPAT_SOCKETS */
#endif /* SOCKETS_MT */
#endif
#endif /* _SOCKET_H_ */

View File

@ -55,6 +55,7 @@ typedef xTaskHandle sys_thread_t;
sys_sem_t* sys_thread_sem_init(void);
void sys_thread_sem_deinit(void);
sys_sem_t* sys_thread_sem_get(void);
err_t sys_mutex_trylock(sys_mutex_t *pxMutex);
#define LWIP_NETCONN_THREAD_SEM_ALLOC() sys_thread_sem_init()
#define LWIP_NETCONN_THREAD_SEM_FREE() sys_thread_sem_deinit()

View File

@ -50,7 +50,7 @@
#include "esp_libc.h"
#include "esp_system.h"
//#define SOCKETS_MT
#define SOCKETS_MT
//#define SOCKETS_TCP_TRACE
@ -1351,7 +1351,7 @@
* While this helps code completion, it might conflict with existing libraries.
* (only used if you use sockets.c)
*/
#define LWIP_COMPAT_SOCKETS 1
#define LWIP_COMPAT_SOCKETS 3
/**
* LWIP_POSIX_SOCKETS_IO_NAMES==1: Enable POSIX-style sockets functions names.