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_disconnect_cleanup PORTABLE C */
40 /* 6.1 */
41 /* AUTHOR */
42 /* */
43 /* Yuxin Zhou, Microsoft Corporation */
44 /* */
45 /* DESCRIPTION */
46 /* */
47 /* This function processes TCP disconnect 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 Connection reset processing */
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_disconnect_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)82 VOID _nx_tcp_disconnect_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 /* Clear the suspension pointer. */
152 socket_ptr -> nx_tcp_socket_disconnect_suspended_thread = NX_NULL;
153
154 /* Now we need to determine if this cleanup is from a terminate, timeout,
155 or from a wait abort. */
156 if (thread_ptr -> tx_thread_state == TX_TCP_IP)
157 {
158
159 /* Thread still suspended on the TCP socket. Setup return error status and
160 resume the thread. */
161
162 /* Setup return status. */
163 thread_ptr -> tx_thread_suspend_status = NX_DISCONNECT_FAILED;
164
165 /* Temporarily disable preemption. */
166 _tx_thread_preempt_disable++;
167
168 /* Restore interrupts. */
169 TX_RESTORE
170
171 /* Resume the thread! Check for preemption even though we are executing
172 from the system timer thread right now which normally executes at the
173 highest priority. */
174 _tx_thread_system_resume(thread_ptr);
175
176 /* Finished, just return. */
177 return;
178 }
179 }
180
181 /* Restore interrupts. */
182 TX_RESTORE
183 }
184
185