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