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_trace.h"
29 #include "tx_thread.h"
30 #include "tx_timer.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _tx_thread_time_slice_change                        PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    William E. Lamie, Microsoft Corporation                             */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function processes thread time slice change requests.  The     */
46 /*    previous time slice is returned to the caller.  If the new request  */
47 /*    is made for an executing thread, it is also placed in the actual    */
48 /*    time-slice countdown variable.                                      */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    thread_ptr                            Pointer to thread             */
53 /*    new_time_slice                        New time slice                */
54 /*    old_time_slice                        Old time slice                */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    status                                Service return status         */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    None                                                                */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application Code                                                    */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
73 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_tx_thread_time_slice_change(TX_THREAD * thread_ptr,ULONG new_time_slice,ULONG * old_time_slice)77 UINT  _tx_thread_time_slice_change(TX_THREAD *thread_ptr, ULONG new_time_slice, ULONG *old_time_slice)
78 {
79 
80 TX_INTERRUPT_SAVE_AREA
81 
82 TX_THREAD       *current_thread;
83 
84 
85     /* Lockout interrupts while the thread is being resumed.  */
86     TX_DISABLE
87 
88     /* If trace is enabled, insert this event into the trace buffer.  */
89     TX_TRACE_IN_LINE_INSERT(TX_TRACE_THREAD_TIME_SLICE_CHANGE, thread_ptr, new_time_slice, thread_ptr -> tx_thread_new_time_slice, 0, TX_TRACE_THREAD_EVENTS)
90 
91     /* Log this kernel call.  */
92     TX_EL_THREAD_TIME_SLICE_CHANGE_INSERT
93 
94     /* Return the old time slice.  */
95     *old_time_slice =  thread_ptr -> tx_thread_new_time_slice;
96 
97     /* Setup the new time-slice.  */
98     thread_ptr -> tx_thread_time_slice =      new_time_slice;
99     thread_ptr -> tx_thread_new_time_slice =  new_time_slice;
100 
101     /* Pickup thread pointer.  */
102     TX_THREAD_GET_CURRENT(current_thread)
103 
104     /* Determine if this thread is the currently executing thread.  */
105     if (thread_ptr == current_thread)
106     {
107 
108         /* Yes, update the time-slice countdown variable.  */
109         _tx_timer_time_slice =  new_time_slice;
110     }
111 
112     /* Restore interrupts.  */
113     TX_RESTORE
114 
115     /* Return completion status.  */
116     return(TX_SUCCESS);
117 }
118 
119