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 /** ThreadX Component                                                     */
16 /**                                                                       */
17 /**   Thread                                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define TX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "tx_api.h"
28 #include "tx_thread.h"
29 #include "tx_timer.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _txe_thread_reset                                   PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    William E. Lamie, Microsoft Corporation                             */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function checks for errors in the thread reset function call.  */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    thread_ptr                            Pointer to thread to reset    */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    TX_THREAD_ERROR                       Invalid thread pointer        */
53 /*    TX_CALLER_ERROR                       Invalid caller of function    */
54 /*    status                                Service return status         */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _tx_thread_reset                      Actual thread reset function  */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Application Code                                                    */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
69 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*                                                                        */
72 /**************************************************************************/
_txe_thread_reset(TX_THREAD * thread_ptr)73 UINT  _txe_thread_reset(TX_THREAD *thread_ptr)
74 {
75 
76 UINT        status;
77 #ifndef TX_TIMER_PROCESS_IN_ISR
78 TX_THREAD   *current_thread;
79 #endif
80 
81 
82     /* Default status to success.  */
83     status =  TX_SUCCESS;
84 
85     /* Check for an invalid thread pointer.  */
86     if (thread_ptr == TX_NULL)
87     {
88 
89         /* Thread pointer is invalid, return appropriate error code.  */
90         status =  TX_THREAD_ERROR;
91     }
92 
93     /* Now check for an invalid thread ID.  */
94     else if (thread_ptr -> tx_thread_id != TX_THREAD_ID)
95     {
96 
97         /* Thread pointer is invalid, return appropriate error code.  */
98         status =  TX_THREAD_ERROR;
99     }
100     else
101     {
102 
103 #ifndef TX_TIMER_PROCESS_IN_ISR
104 
105         /* Pickup thread pointer.  */
106         TX_THREAD_GET_CURRENT(current_thread)
107 
108         /* Check for invalid caller of this function.  First check for a calling thread.  */
109         if (current_thread == &_tx_timer_thread)
110         {
111 
112             /* Invalid caller of this function, return appropriate error code.  */
113             status =  TX_CALLER_ERROR;
114         }
115 #endif
116 
117         /* Check for interrupt or initialization call.  */
118         if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
119         {
120 
121             /* Invalid caller of this function, return appropriate error code.  */
122             status =  TX_CALLER_ERROR;
123         }
124     }
125 
126     /* Determine if everything is okay.  */
127     if (status == TX_SUCCESS)
128     {
129 
130         /* Call actual thread reset function.  */
131         status =  _tx_thread_reset(thread_ptr);
132     }
133 
134     /* Return completion status.  */
135     return(status);
136 }
137 
138