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 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _txe_thread_preemption_change                       PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    William E. Lamie, Microsoft Corporation                             */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function checks for errors in the preemption threshold change  */
44 /*    function call.                                                      */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    thread_ptr                            Pointer to thread             */
49 /*    new_threshold                         New preemption threshold      */
50 /*    old_threshold                         Old preemption threshold      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    TX_THREAD_ERROR                       Invalid thread pointer        */
55 /*    TX_PTR_ERROR                          Invalid old threshold pointer */
56 /*    TX_CALLER_ERROR                       Invalid caller of function    */
57 /*    status                                Actual completion status      */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _tx_thread_preemption_change          Actual preempt change function*/
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application Code                                                    */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
72 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_txe_thread_preemption_change(TX_THREAD * thread_ptr,UINT new_threshold,UINT * old_threshold)76 UINT  _txe_thread_preemption_change(TX_THREAD *thread_ptr, UINT new_threshold, UINT *old_threshold)
77 {
78 
79 UINT        status;
80 
81 
82     /* Check for an invalid thread pointer.  */
83     if (thread_ptr == TX_NULL)
84     {
85 
86         /* Thread pointer is invalid, return appropriate error code.  */
87         status =  TX_THREAD_ERROR;
88     }
89 
90     /* Now check for invalid thread ID.  */
91     else if (thread_ptr -> tx_thread_id != TX_THREAD_ID)
92     {
93 
94         /* Thread pointer is invalid, return appropriate error code.  */
95         status =  TX_THREAD_ERROR;
96     }
97 
98     /* Check for a valid old threshold pointer.  */
99     else if (old_threshold == TX_NULL)
100     {
101 
102         /* Invalid destination pointer, return appropriate error code.  */
103         status =  TX_PTR_ERROR;
104     }
105 
106     /* Check for invalid caller of this function.  */
107     else if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
108     {
109 
110         /* Invalid caller of this function, return appropriate error code.  */
111         status =  TX_CALLER_ERROR;
112     }
113 
114     /* Determine if the preemption-threshold is valid.  */
115     else if (new_threshold > thread_ptr -> tx_thread_user_priority)
116     {
117 
118         /* Return an error status.  */
119         status =  TX_THRESH_ERROR;
120     }
121     else
122     {
123 
124         /* Call actual change thread preemption function.  */
125         status =  _tx_thread_preemption_change(thread_ptr, new_threshold, old_threshold);
126     }
127 
128     /* Return completion status.  */
129     return(status);
130 }
131 
132