1 /**************************************************************************/
2 /* */
3 /* Copyright (c) Microsoft Corporation. All rights reserved. */
4 /* */
5 /* This software is licensed under the Microsoft Software License */
6 /* Terms for Microsoft Azure RTOS. Full text of the license can be */
7 /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */
8 /* and in the root directory of this software. */
9 /* */
10 /**************************************************************************/
11
12
13 /**************************************************************************/
14 /**************************************************************************/
15 /** */
16 /** NetX Component */
17 /** */
18 /** Transmission Control Protocol (TCP) */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23 #define NX_SOURCE_CODE
24
25
26 /* Include necessary system files. */
27
28 #include "nx_api.h"
29 #include "tx_thread.h"
30 #include "tx_timer.h"
31 #include "nx_ip.h"
32 #include "nx_tcp.h"
33
34
35 /**************************************************************************/
36 /* */
37 /* FUNCTION RELEASE */
38 /* */
39 /* _nx_tcp_receive_cleanup PORTABLE C */
40 /* 6.1 */
41 /* AUTHOR */
42 /* */
43 /* Yuxin Zhou, Microsoft Corporation */
44 /* */
45 /* DESCRIPTION */
46 /* */
47 /* This function processes TCP receive timeout and thread terminate */
48 /* actions that require the TCP socket data structures to be cleaned */
49 /* up. */
50 /* */
51 /* INPUT */
52 /* */
53 /* thread_ptr Pointer to suspended thread's */
54 /* control block */
55 /* */
56 /* OUTPUT */
57 /* */
58 /* None */
59 /* */
60 /* CALLS */
61 /* */
62 /* tx_event_flags_set Set event flag */
63 /* _tx_thread_system_resume Resume thread service */
64 /* */
65 /* CALLED BY */
66 /* */
67 /* _nx_tcp_deferred_cleanup_check Deferred cleanup processing */
68 /* _nx_tcp_socket_connection_reset Socket reset connection */
69 /* _nx_tcp_socket_disconnect Socket disconnect */
70 /* _tx_thread_timeout Thread timeout processing */
71 /* _tx_thread_terminate Thread terminate processing */
72 /* */
73 /* RELEASE HISTORY */
74 /* */
75 /* DATE NAME DESCRIPTION */
76 /* */
77 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */
78 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
79 /* resulting in version 6.1 */
80 /* */
81 /**************************************************************************/
_nx_tcp_receive_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)82 VOID _nx_tcp_receive_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER)
83 {
84
85 TX_INTERRUPT_SAVE_AREA
86
87 NX_IP *ip_ptr;
88 NX_TCP_SOCKET *socket_ptr; /* Working socket pointer */
89
90 NX_CLEANUP_EXTENSION
91
92 /* Disable interrupts. */
93 TX_DISABLE
94
95 /* Setup pointer to TCP socket control block. */
96 socket_ptr = (NX_TCP_SOCKET *)thread_ptr -> tx_thread_suspend_control_block;
97
98 /* Determine if the socket pointer is valid. */
99 if ((!socket_ptr) || (socket_ptr -> nx_tcp_socket_id != NX_TCP_ID))
100 {
101
102 /* Restore interrupts. */
103 TX_RESTORE
104
105 return;
106 }
107
108 /* Determine if the cleanup is still required. */
109 if (!(thread_ptr -> tx_thread_suspend_cleanup))
110 {
111
112 /* Restore interrupts. */
113 TX_RESTORE
114
115 return;
116 }
117
118 /* Determine if the caller is an ISR or the system timer thread. */
119 #ifndef TX_TIMER_PROCESS_IN_ISR
120 if ((TX_THREAD_GET_SYSTEM_STATE()) || (_tx_thread_current_ptr == &_tx_timer_thread))
121 #else
122 if (TX_THREAD_GET_SYSTEM_STATE())
123 #endif
124 {
125
126 /* Yes, defer the processing to the NetX IP thread. */
127
128 /* Yes, change the suspend cleanup routine to indicate the cleanup is deferred. */
129 thread_ptr -> tx_thread_suspend_cleanup = _nx_tcp_cleanup_deferred;
130
131 /* Pickup the IP pointer. */
132 ip_ptr = socket_ptr -> nx_tcp_socket_ip_ptr;
133
134 /* Restore interrupts. */
135 TX_RESTORE
136
137 /* Set the deferred cleanup flag for the IP thread. */
138 tx_event_flags_set(&(ip_ptr -> nx_ip_events), NX_IP_TCP_CLEANUP_DEFERRED, TX_OR);
139
140 /* Return to caller. */
141 return;
142 }
143 else
144 {
145
146 /* Yes, we still have thread suspension! */
147
148 /* Clear the suspension cleanup flag. */
149 thread_ptr -> tx_thread_suspend_cleanup = TX_NULL;
150
151 /* Remove the suspended thread from the list. */
152
153 /* See if this is the only suspended thread on the list. */
154 if (thread_ptr == thread_ptr -> tx_thread_suspended_next)
155 {
156
157 /* Yes, the only suspended thread. */
158
159 /* Update the head pointer. */
160 socket_ptr -> nx_tcp_socket_receive_suspension_list = NX_NULL;
161 }
162 else
163 {
164
165 /* At least one more thread is on the same suspension list. */
166
167 /* Update the list head pointer. */
168 socket_ptr -> nx_tcp_socket_receive_suspension_list = thread_ptr -> tx_thread_suspended_next;
169
170 /* Update the links of the adjacent threads. */
171 (thread_ptr -> tx_thread_suspended_next) -> tx_thread_suspended_previous =
172 thread_ptr -> tx_thread_suspended_previous;
173 (thread_ptr -> tx_thread_suspended_previous) -> tx_thread_suspended_next =
174 thread_ptr -> tx_thread_suspended_next;
175 }
176
177 /* Decrement the suspension count. */
178 socket_ptr -> nx_tcp_socket_receive_suspended_count--;
179
180 /* Now we need to determine if this cleanup is from a terminate, timeout,
181 or from a wait abort. */
182 if (thread_ptr -> tx_thread_state == TX_TCP_IP)
183 {
184
185 /* Thread still suspended on the TCP socket. Setup return error status and
186 resume the thread. */
187
188 /* Determine which receive error is present. */
189 if (socket_ptr -> nx_tcp_socket_state != NX_TCP_ESTABLISHED)
190 {
191
192 /* This socket is no longer connected. */
193 thread_ptr -> tx_thread_suspend_status = NX_NOT_CONNECTED;
194 }
195 else
196 {
197
198 /* Setup return status. */
199 thread_ptr -> tx_thread_suspend_status = NX_NO_PACKET;
200 }
201
202 /* Temporarily disable preemption. */
203 _tx_thread_preempt_disable++;
204
205 /* Restore interrupts. */
206 TX_RESTORE
207
208 /* Resume the thread! Check for preemption even though we are executing
209 from the system timer thread right now which normally executes at the
210 highest priority. */
211 _tx_thread_system_resume(thread_ptr);
212
213 /* Finished, just return. */
214 return;
215 }
216 }
217
218 /* Restore interrupts. */
219 TX_RESTORE
220 }
221
222