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 /** ThreadX Component                                                     */
17 /**                                                                       */
18 /**   Block Pool                                                          */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define TX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "tx_api.h"
29 #include "tx_thread.h"
30 #include "tx_timer.h"
31 #include "tx_block_pool.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _txe_block_allocate                                 PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    William E. Lamie, Microsoft Corporation                             */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function checks for errors in the allocate block memory        */
47 /*    function call.                                                      */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    pool_ptr                          Pointer to pool control block     */
52 /*    block_ptr                         Pointer to place allocated block  */
53 /*                                        pointer                         */
54 /*    wait_option                       Suspension option                 */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    TX_POOL_ERROR                     Invalid pool pointer              */
59 /*    TX_PTR_ERROR                      Invalid destination pointer       */
60 /*    TX_WAIT_ERROR                     Invalid wait option               */
61 /*    status                            Actual Completion status          */
62 /*                                                                        */
63 /*  CALLS                                                                 */
64 /*                                                                        */
65 /*    _tx_block_allocate                Actual block allocate function    */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application Code                                                    */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
76 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*                                                                        */
79 /**************************************************************************/
_txe_block_allocate(TX_BLOCK_POOL * pool_ptr,VOID ** block_ptr,ULONG wait_option)80 UINT  _txe_block_allocate(TX_BLOCK_POOL *pool_ptr, VOID **block_ptr, ULONG wait_option)
81 {
82 
83 UINT            status;
84 
85 #ifndef TX_TIMER_PROCESS_IN_ISR
86 
87 TX_THREAD       *current_thread;
88 #endif
89 
90     /* Default status to success.  */
91     status =  TX_SUCCESS;
92 
93     /* Check for an invalid pool pointer.  */
94     if (pool_ptr == TX_NULL)
95     {
96 
97         /* Pool pointer is invalid, return appropriate error code.  */
98         status =  TX_POOL_ERROR;
99     }
100 
101     /* Check for an invalid pool pointer.  */
102     else if (pool_ptr -> tx_block_pool_id != TX_BLOCK_POOL_ID)
103     {
104 
105         /* Pool pointer is invalid, return appropriate error code.  */
106         status =  TX_POOL_ERROR;
107     }
108 
109     /* Check for an invalid destination for return pointer.  */
110     else if (block_ptr == TX_NULL)
111     {
112 
113         /* Null destination pointer, return appropriate error.  */
114         status =  TX_PTR_ERROR;
115     }
116     else
117     {
118 
119         /* Check for a wait option error.  Only threads are allowed any form of
120            suspension.  */
121         if (wait_option != TX_NO_WAIT)
122         {
123 
124             /* Is the call from an ISR or Initialization?  */
125             if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
126             {
127 
128                 /* A non-thread is trying to suspend, return appropriate error code.  */
129                 status =  TX_WAIT_ERROR;
130             }
131 
132 #ifndef TX_TIMER_PROCESS_IN_ISR
133             else
134             {
135 
136                 /* Pickup thread pointer.  */
137                 TX_THREAD_GET_CURRENT(current_thread)
138 
139                 /* Is the current thread the timer thread?  */
140                 if (current_thread == &_tx_timer_thread)
141                 {
142 
143                     /* A non-thread is trying to suspend, return appropriate error code.  */
144                     status =  TX_WAIT_ERROR;
145                 }
146             }
147 #endif
148         }
149     }
150 
151     /* Determine if everything is okay.  */
152     if (status == TX_SUCCESS)
153     {
154 
155         /* Call actual block allocate function.  */
156         status =  _tx_block_allocate(pool_ptr, block_ptr, wait_option);
157     }
158 
159     /* Return completion status.  */
160     return(status);
161 }
162 
163