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 /** POSIX wrapper for THREADX                                             */
16 /**                                                                       */
17 /**                                                                       */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 /* Include necessary system files.  */
23 
24 #include "tx_api.h"    /* Threadx API */
25 #include "pthread.h"  /* Posix API */
26 #include "px_int.h"    /* Posix helper functions */
27 
28 
29 /**************************************************************************/
30 /*                                                                        */
31 /*  FUNCTION                                               RELEASE        */
32 /*                                                                        */
33 /*    pthread_setschedparam                               PORTABLE C      */
34 /*                                                           6.1.7        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    William E. Lamie, Microsoft Corporation                             */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This function changes the scheduling parameters of a pthread.       */
42 /*                                                                        */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    thread                         POSIX thread ID                      */
47 /*    policy                         Address of the thread attributes     */
48 /*    sched_param                    Address of structure to contain the  */
49 /*                                   returned scheduling parameters       */
50 /*                                                                        */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*     0                             if successful                        */
55 /*     Value                         in case of any error                 */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    posix_tid2tcb                                                       */
60 /*    posix_tcb2thread                                                    */
61 /*    tx_thread_priority_change                                           */
62 /*                                                                        */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application Code                                                    */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
73 /*                                                                        */
74 /**************************************************************************/
pthread_setschedparam(pthread_t thread,INT policy,const struct sched_param * param)75 INT pthread_setschedparam(pthread_t thread, INT policy, const struct sched_param *param)
76 {
77     TX_INTERRUPT_SAVE_AREA
78     POSIX_TCB  *thread_tcb;
79     TX_THREAD  *TheThread;
80     UINT        Tmp;
81     ULONG       UTmp;
82 
83 
84 
85     thread_tcb=posix_tid2tcb(thread);
86 
87     if(thread_tcb==NULL)
88     {
89         return(ESRCH);
90     }
91     if (!(( policy == SCHED_FIFO )||(policy== SCHED_RR)))
92     {
93         return(ENOTSUP);
94     }
95 
96 
97     TX_DISABLE
98     thread_tcb->sched_policy=policy;
99     thread_tcb->sched_attr.sched_priority=param->sched_priority;
100     thread_tcb->current_priority= param->sched_priority;
101 
102     TheThread=posix_tcb2thread(thread_tcb);
103 
104     tx_thread_priority_change(TheThread, (TX_LOWEST_PRIORITY - thread_tcb->current_priority + 1),&Tmp);
105     thread_tcb->orig_priority=(ULONG) Tmp;
106 
107     if(policy==SCHED_RR) thread_tcb->time_slice=SCHED_RR_TIME_SLICE;
108     else thread_tcb->time_slice=0;
109     tx_thread_time_slice_change(TheThread, thread_tcb->time_slice, &UTmp);
110 
111     TX_RESTORE
112 
113     return(NO_ERROR);
114 }
115 
116 
117 
118 
119 
120