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