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 /**   User Datagram Protocol (UDP)                                        */
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 "nx_udp.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _nx_udp_bind_cleanup                                PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function processes UDP bind timeout and thread terminate       */
46 /*    actions that require the UDP socket data structures to be cleaned   */
47 /*    up.                                                                 */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    thread_ptr                            Pointer to suspended thread's */
52 /*                                            control block               */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _tx_thread_system_resume              Resume thread service         */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    _nx_udp_socket_unbind                 Unbind processing             */
65 /*    _tx_thread_timeout                    Thread timeout processing     */
66 /*    _tx_thread_terminate                  Thread terminate processing   */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
73 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_nx_udp_bind_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)77 VOID  _nx_udp_bind_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER)
78 {
79 
80 TX_INTERRUPT_SAVE_AREA
81 
82 NX_UDP_SOCKET *socket_ptr;        /* Working socket pointer  */
83 NX_UDP_SOCKET *owning_socket_ptr; /* Socket owning the port  */
84 
85     NX_CLEANUP_EXTENSION
86 
87     /* Setup pointer to UDP socket control block.  */
88     socket_ptr =  (NX_UDP_SOCKET *)thread_ptr -> tx_thread_suspend_control_block;
89 
90     /* Disable interrupts to remove the suspended thread from the UDP socket.  */
91     TX_DISABLE
92 
93     /* Determine if the cleanup is still required.  */
94     if ((thread_ptr -> tx_thread_suspend_cleanup) && (socket_ptr) &&
95         (socket_ptr -> nx_udp_socket_id == NX_UDP_ID))
96     {
97 
98         /* Yes, we still have thread suspension!  */
99 
100         /* Clear the socket bind in progress flag.  */
101         socket_ptr -> nx_udp_socket_bind_in_progress =  NX_NULL;
102 
103         /* Clear the suspension cleanup flag.  */
104         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
105 
106         /* Pickup the socket owning the port. This pointer was
107            saved in the bind processing prior to suspension.  */
108         owning_socket_ptr =  socket_ptr -> nx_udp_socket_bound_previous;
109 
110         /* Remove the suspended thread from the list.  */
111 
112         /* See if this is the only suspended thread on the list.  */
113         if (thread_ptr == thread_ptr -> tx_thread_suspended_next)
114         {
115 
116             /* Yes, the only suspended thread.  */
117 
118             /* Update the head pointer.  */
119             owning_socket_ptr -> nx_udp_socket_bind_suspension_list =  NX_NULL;
120         }
121         else
122         {
123 
124             /* At least one more thread is on the same suspension list.  */
125 
126             /* Update the list head pointer.  */
127             owning_socket_ptr -> nx_udp_socket_bind_suspension_list =  thread_ptr -> tx_thread_suspended_next;
128 
129             /* Update the links of the adjacent threads.  */
130             (thread_ptr -> tx_thread_suspended_next) -> tx_thread_suspended_previous =
131                 thread_ptr -> tx_thread_suspended_previous;
132             (thread_ptr -> tx_thread_suspended_previous) -> tx_thread_suspended_next =
133                 thread_ptr -> tx_thread_suspended_next;
134         }
135 
136         /* Decrement the suspension count.  */
137         owning_socket_ptr -> nx_udp_socket_bind_suspended_count--;
138 
139         /* Now we need to determine if this cleanup is from a terminate, timeout,
140            or from a wait abort.  */
141         if (thread_ptr -> tx_thread_state == TX_TCP_IP)
142         {
143 
144             /* Thread still suspended on the UDP socket.  Setup return error status and
145                resume the thread.  */
146 
147             /* Setup return status.  */
148             thread_ptr -> tx_thread_suspend_status =  NX_PORT_UNAVAILABLE;
149 
150             /* Temporarily disable preemption.  */
151             _tx_thread_preempt_disable++;
152 
153             /* Restore interrupts.  */
154             TX_RESTORE
155 
156             /* Resume the thread!  Check for preemption even though we are executing
157                from the system timer thread right now which normally executes at the
158                highest priority.  */
159             _tx_thread_system_resume(thread_ptr);
160 
161             /* Finished, just return.  */
162             return;
163         }
164     }
165 
166     /* Restore interrupts.  */
167     TX_RESTORE
168 }
169 
170