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 "nx_packet.h"
29 #include "tx_thread.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_packet_pool_delete                              PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function deletes a previously created packet pool.             */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    pool_ptr                              Packet pool control block     */
49 /*                                            pointer                     */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    status                                Return status                 */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _tx_thread_system_resume              Resume threads suspended      */
58 /*    _tx_thread_system_preempt_check       Check for preemption          */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Application Code                                                    */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
69 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*                                                                        */
72 /**************************************************************************/
_nx_packet_pool_delete(NX_PACKET_POOL * pool_ptr)73 UINT  _nx_packet_pool_delete(NX_PACKET_POOL *pool_ptr)
74 {
75 
76 TX_INTERRUPT_SAVE_AREA
77 
78 TX_THREAD *thread_ptr;      /* Working thread pointer  */
79 
80 
81     /* Disable interrupts to remove the packet pool from the created list.  */
82     TX_DISABLE
83 
84     /* Decrement the number of packet pools created.  */
85     _nx_packet_pool_created_count--;
86 
87     /* Clear the packet pool ID to make it invalid.  */
88     pool_ptr -> nx_packet_pool_id =  0;
89 
90     /* See if the packet pool only one on the list.  */
91     if (pool_ptr == pool_ptr -> nx_packet_pool_created_next)
92     {
93 
94         /* Only created packet pool, just set the created list to NULL.  */
95         _nx_packet_pool_created_ptr =  NX_NULL;
96     }
97     else
98     {
99 
100         /* Link-up the neighbors.  */
101         (pool_ptr -> nx_packet_pool_created_next) -> nx_packet_pool_created_previous =
102             pool_ptr -> nx_packet_pool_created_previous;
103         (pool_ptr -> nx_packet_pool_created_previous) -> nx_packet_pool_created_next =
104             pool_ptr -> nx_packet_pool_created_next;
105 
106         /* See if we have to update the created list head pointer.  */
107         if (_nx_packet_pool_created_ptr == pool_ptr)
108         {
109 
110             /* Yes, move the head pointer to the next link. */
111             _nx_packet_pool_created_ptr =  pool_ptr -> nx_packet_pool_created_next;
112         }
113     }
114 
115     /* Temporarily disable preemption.  */
116     _tx_thread_preempt_disable++;
117 
118     /* Restore interrupts.  */
119     TX_RESTORE
120 
121     /* Walk through the packet pool suspension list to resume any and all
122        threads suspended on this packet pool.  */
123     thread_ptr =  pool_ptr -> nx_packet_pool_suspension_list;
124     while (pool_ptr -> nx_packet_pool_suspended_count)
125     {
126         /* Lockout interrupts.  */
127         TX_DISABLE
128 
129         /* Clear the cleanup pointer, this prevents the timeout from doing
130            anything.  */
131         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
132 
133         /* Temporarily disable preemption again.  */
134         _tx_thread_preempt_disable++;
135 
136         /* Restore interrupts.  */
137         TX_RESTORE
138 
139         /* Set the return status in the thread to NX_POOL_DELETED.  */
140         thread_ptr -> tx_thread_suspend_status =  NX_POOL_DELETED;
141 
142         /* Move the thread pointer ahead.  */
143         thread_ptr =  thread_ptr -> tx_thread_suspended_next;
144 
145         /* Resume the thread.  */
146         _tx_thread_system_resume(thread_ptr -> tx_thread_suspended_previous);
147 
148         /* Decrease the suspended count.  */
149         pool_ptr -> nx_packet_pool_suspended_count--;
150     }
151 
152     /* Disable interrupts.  */
153     TX_DISABLE
154 
155     /* Release previous preempt disable.  */
156     _tx_thread_preempt_disable--;
157 
158     /* Restore interrupts.  */
159     TX_RESTORE
160 
161     /* If trace is enabled, insert this event into the trace buffer.  */
162     NX_TRACE_IN_LINE_INSERT(NX_TRACE_PACKET_POOL_DELETE, pool_ptr, 0, 0, 0, NX_TRACE_PACKET_EVENTS, 0, 0);
163 
164     /* If trace is enabled, unregister this object.  */
165     NX_TRACE_OBJECT_UNREGISTER(pool_ptr);
166 
167     /* Check for preemption.  */
168     _tx_thread_system_preempt_check();
169 
170     /* Return NX_SUCCESS.  */
171     return(NX_SUCCESS);
172 }
173 
174