1 /*
2  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32 
33 #ifndef __SYS_ARCH_H__
34 #define __SYS_ARCH_H__
35 
36 #include "freertos/FreeRTOS.h"
37 #include "freertos/task.h"
38 #include "freertos/queue.h"
39 #include "freertos/semphr.h"
40 #include "arch/vfs_lwip.h"
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 
47 typedef SemaphoreHandle_t sys_sem_t;
48 typedef SemaphoreHandle_t sys_mutex_t;
49 typedef TaskHandle_t sys_thread_t;
50 
51 typedef struct sys_mbox_s {
52   QueueHandle_t os_mbox;
53   void *owner;
54 }* sys_mbox_t;
55 
56 /** This is returned by _fromisr() sys functions to tell the outermost function
57  * that a higher priority task was woken and the scheduler needs to be invoked.
58  */
59 #define ERR_NEED_SCHED 123
60 
61 void sys_delay_ms(uint32_t ms);
62 #define sys_msleep(ms) sys_delay_ms(ms)
63 
64 #define LWIP_COMPAT_MUTEX 0
65 
66 #if !LWIP_COMPAT_MUTEX
67 #define sys_mutex_valid( x ) ( ( ( *x ) == NULL) ? pdFALSE : pdTRUE )
68 #define sys_mutex_set_invalid( x ) ( ( *x ) = NULL )
69 #endif
70 
71 #define sys_mbox_valid( x ) ( ( ( *x ) == NULL) ? pdFALSE : pdTRUE )
72 
73 /* Define the sys_mbox_set_invalid() to empty to support lock-free mbox in ESP LWIP.
74  *
75  * The basic idea about the lock-free mbox is that the mbox should always be valid unless
76  * no socket APIs are using the socket and the socket is closed. ESP LWIP achieves this by
77  * following two changes to official LWIP:
78  * 1. Postpone the deallocation of mbox to netconn_free(), in other words, free the mbox when
79  *    no one is using the socket.
80  * 2. Define the sys_mbox_set_invalid() to empty if the mbox is not actually freed.
81 
82  * The second change is necessary. Consider a common scenario: the application task calls
83  * recv() to receive packets from the socket, the sys_mbox_valid() returns true. Because there
84  * is no lock for the mbox, the LWIP CORE can call sys_mbox_set_invalid() to set the mbox at
85  * anytime and the thread-safe issue may happen.
86  *
87  * However, if the sys_mbox_set_invalid() is not called after sys_mbox_free(), e.g. in netconn_alloc(),
88  * we need to initialize the mbox to invalid explicitly since sys_mbox_set_invalid() now is empty.
89  */
90 #define sys_mbox_set_invalid( x )  *x = NULL
91 
92 #define sys_sem_valid( x ) ( ( ( *x ) == NULL) ? pdFALSE : pdTRUE )
93 #define sys_sem_set_invalid( x ) ( ( *x ) = NULL )
94 
95 void sys_delay_ms(uint32_t ms);
96 sys_sem_t* sys_thread_sem_init(void);
97 void sys_thread_sem_deinit(void);
98 sys_sem_t* sys_thread_sem_get(void);
99 
100 #ifdef __cplusplus
101 }
102 #endif
103 
104 #endif /* __SYS_ARCH_H__ */
105