1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Component                                                        */
16 /**                                                                       */
17 /**   User Datagram Protocol (UDP)                                        */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "nx_api.h"
28 #include "tx_thread.h"
29 #include "nx_udp.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_udp_receive_cleanup                             PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function processes UDP receive timeout and thread terminate    */
45 /*    actions that require the UDP socket data structures to be cleaned   */
46 /*    up.                                                                 */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    thread_ptr                            Pointer to suspended thread's */
51 /*                                            control block               */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _tx_thread_system_resume              Resume thread service         */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _tx_thread_timeout                    Thread timeout processing     */
64 /*    _tx_thread_terminate                  Thread terminate processing   */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
71 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_nx_udp_receive_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)75 VOID  _nx_udp_receive_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER)
76 {
77 
78 TX_INTERRUPT_SAVE_AREA
79 
80 NX_UDP_SOCKET *socket_ptr;  /* Working socket pointer  */
81 
82     NX_CLEANUP_EXTENSION
83 
84     /* Setup pointer to UDP socket control block.  */
85     socket_ptr =  (NX_UDP_SOCKET *)thread_ptr -> tx_thread_suspend_control_block;
86 
87     /* Disable interrupts to remove the suspended thread from the UDP socket.  */
88     TX_DISABLE
89 
90     /* Determine if the cleanup is still required.  */
91     if ((thread_ptr -> tx_thread_suspend_cleanup) && (socket_ptr) &&
92         (socket_ptr -> nx_udp_socket_id == NX_UDP_ID))
93     {
94 
95         /* Yes, we still have thread suspension!  */
96 
97         /* Clear the suspension cleanup flag.  */
98         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
99 
100         /* Remove the suspended thread from the list.  */
101 
102         /* See if this is the only suspended thread on the list.  */
103         if (thread_ptr == thread_ptr -> tx_thread_suspended_next)
104         {
105 
106             /* Yes, the only suspended thread.  */
107 
108             /* Update the head pointer.  */
109             socket_ptr -> nx_udp_socket_receive_suspension_list =  NX_NULL;
110         }
111         else
112         {
113 
114             /* At least one more thread is on the same suspension list.  */
115 
116             /* Update the list head pointer.  */
117             socket_ptr -> nx_udp_socket_receive_suspension_list =  thread_ptr -> tx_thread_suspended_next;
118 
119             /* Update the links of the adjacent threads.  */
120             (thread_ptr -> tx_thread_suspended_next) -> tx_thread_suspended_previous =
121                 thread_ptr -> tx_thread_suspended_previous;
122             (thread_ptr -> tx_thread_suspended_previous) -> tx_thread_suspended_next =
123                 thread_ptr -> tx_thread_suspended_next;
124         }
125 
126         /* Decrement the suspension count.  */
127         socket_ptr -> nx_udp_socket_receive_suspended_count--;
128 
129         /* Now we need to determine if this cleanup is from a terminate, timeout,
130            or from a wait abort.  */
131         if (thread_ptr -> tx_thread_state == TX_TCP_IP)
132         {
133 
134             /* Thread still suspended on the UDP socket.  Setup return error status and
135                resume the thread.  */
136 
137             /* Setup return status.  */
138             thread_ptr -> tx_thread_suspend_status =  NX_NO_PACKET;
139 
140             /* Temporarily disable preemption.  */
141             _tx_thread_preempt_disable++;
142 
143             /* Restore interrupts.  */
144             TX_RESTORE
145 
146             /* Resume the thread!  Check for preemption even though we are executing
147                from the system timer thread right now which normally executes at the
148                highest priority.  */
149             _tx_thread_system_resume(thread_ptr);
150 
151             /* Finished, just return.  */
152             return;
153         }
154     }
155 
156     /* Restore interrupts.  */
157     TX_RESTORE
158 }
159 
160