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_receive_cleanup                             PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function processes UDP receive 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 /*    _tx_thread_timeout                    Thread timeout processing     */
65 /*    _tx_thread_terminate                  Thread terminate processing   */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
72 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_nx_udp_receive_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)76 VOID  _nx_udp_receive_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER)
77 {
78 
79 TX_INTERRUPT_SAVE_AREA
80 
81 NX_UDP_SOCKET *socket_ptr;  /* Working socket pointer  */
82 
83     NX_CLEANUP_EXTENSION
84 
85     /* Setup pointer to UDP socket control block.  */
86     socket_ptr =  (NX_UDP_SOCKET *)thread_ptr -> tx_thread_suspend_control_block;
87 
88     /* Disable interrupts to remove the suspended thread from the UDP socket.  */
89     TX_DISABLE
90 
91     /* Determine if the cleanup is still required.  */
92     if ((thread_ptr -> tx_thread_suspend_cleanup) && (socket_ptr) &&
93         (socket_ptr -> nx_udp_socket_id == NX_UDP_ID))
94     {
95 
96         /* Yes, we still have thread suspension!  */
97 
98         /* Clear the suspension cleanup flag.  */
99         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
100 
101         /* Remove the suspended thread from the list.  */
102 
103         /* See if this is the only suspended thread on the list.  */
104         if (thread_ptr == thread_ptr -> tx_thread_suspended_next)
105         {
106 
107             /* Yes, the only suspended thread.  */
108 
109             /* Update the head pointer.  */
110             socket_ptr -> nx_udp_socket_receive_suspension_list =  NX_NULL;
111         }
112         else
113         {
114 
115             /* At least one more thread is on the same suspension list.  */
116 
117             /* Update the list head pointer.  */
118             socket_ptr -> nx_udp_socket_receive_suspension_list =  thread_ptr -> tx_thread_suspended_next;
119 
120             /* Update the links of the adjacent threads.  */
121             (thread_ptr -> tx_thread_suspended_next) -> tx_thread_suspended_previous =
122                 thread_ptr -> tx_thread_suspended_previous;
123             (thread_ptr -> tx_thread_suspended_previous) -> tx_thread_suspended_next =
124                 thread_ptr -> tx_thread_suspended_next;
125         }
126 
127         /* Decrement the suspension count.  */
128         socket_ptr -> nx_udp_socket_receive_suspended_count--;
129 
130         /* Now we need to determine if this cleanup is from a terminate, timeout,
131            or from a wait abort.  */
132         if (thread_ptr -> tx_thread_state == TX_TCP_IP)
133         {
134 
135             /* Thread still suspended on the UDP socket.  Setup return error status and
136                resume the thread.  */
137 
138             /* Setup return status.  */
139             thread_ptr -> tx_thread_suspend_status =  NX_NO_PACKET;
140 
141             /* Temporarily disable preemption.  */
142             _tx_thread_preempt_disable++;
143 
144             /* Restore interrupts.  */
145             TX_RESTORE
146 
147             /* Resume the thread!  Check for preemption even though we are executing
148                from the system timer thread right now which normally executes at the
149                highest priority.  */
150             _tx_thread_system_resume(thread_ptr);
151 
152             /* Finished, just return.  */
153             return;
154         }
155     }
156 
157     /* Restore interrupts.  */
158     TX_RESTORE
159 }
160 
161