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 /**   Packet Pool Management (Packet)                                     */
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_packet.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_packet_pool_cleanup                             PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function processes packet allocate timeout and thread terminate*/
45 /*    actions that require the packet pool 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_packet_pool_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)75 VOID  _nx_packet_pool_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER)
76 {
77 
78 TX_INTERRUPT_SAVE_AREA
79 
80 NX_PACKET_POOL *pool_ptr;   /* Working packet pool pointer  */
81 
82     NX_CLEANUP_EXTENSION
83 
84     /* Setup pointer to packet pool control block.  */
85     pool_ptr =  (NX_PACKET_POOL *)thread_ptr -> tx_thread_suspend_control_block;
86 
87     /* Disable interrupts to remove the suspended thread from the packet pool.  */
88     TX_DISABLE
89 
90     /* Determine if the cleanup is still required.  */
91     if ((thread_ptr -> tx_thread_suspend_cleanup) && (pool_ptr) &&
92         (pool_ptr -> nx_packet_pool_id == NX_PACKET_POOL_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             pool_ptr -> nx_packet_pool_suspension_list =  TX_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 if necessary.  */
117             if (pool_ptr -> nx_packet_pool_suspension_list == thread_ptr)
118             {
119                 pool_ptr -> nx_packet_pool_suspension_list =  thread_ptr -> tx_thread_suspended_next;
120             }
121 
122             /* Update the links of the adjacent threads.  */
123             (thread_ptr -> tx_thread_suspended_next) -> tx_thread_suspended_previous =
124                 thread_ptr -> tx_thread_suspended_previous;
125             (thread_ptr -> tx_thread_suspended_previous) -> tx_thread_suspended_next =
126                 thread_ptr -> tx_thread_suspended_next;
127         }
128 
129         /* Decrement the suspension count.  */
130         pool_ptr -> nx_packet_pool_suspended_count--;
131 
132         /* Now we need to determine if this cleanup is from a terminate, timeout,
133            or from a wait abort.  */
134         if (thread_ptr -> tx_thread_state == TX_TCP_IP)
135         {
136 
137             /* Thread still suspended on the packet pool.  Setup return error status and
138                resume the thread.  */
139 
140             /* Setup return status.  */
141             thread_ptr -> tx_thread_suspend_status =  NX_NO_PACKET;
142 
143             /* Temporarily disable preemption.  */
144             _tx_thread_preempt_disable++;
145 
146             /* Restore interrupts.  */
147             TX_RESTORE
148 
149             /* Resume the thread!  Check for preemption even though we are executing
150                from the system timer thread right now which normally executes at the
151                highest priority.  */
152             _tx_thread_system_resume(thread_ptr);
153 
154             /* Finished, just return.  */
155             return;
156         }
157     }
158 
159     /* Restore interrupts.  */
160     TX_RESTORE
161 }
162 
163