1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Function sets scheduling policy and parameters of the thread |
---|
5 | * @ingroup POSIXAPI |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * 13.5.2 Dynamic Thread Scheduling Parameters Access, |
---|
10 | * P1003.1c/Draft 10, p. 124 |
---|
11 | */ |
---|
12 | |
---|
13 | /* COPYRIGHT (c) 1989-2014. |
---|
14 | * On-Line Applications Research Corporation (OAR). |
---|
15 | * |
---|
16 | * The license and distribution terms for this file may be |
---|
17 | * found in the file LICENSE in this distribution or at |
---|
18 | * http://www.rtems.org/license/LICENSE. |
---|
19 | */ |
---|
20 | |
---|
21 | #if HAVE_CONFIG_H |
---|
22 | #include "config.h" |
---|
23 | #endif |
---|
24 | |
---|
25 | #include <pthread.h> |
---|
26 | #include <errno.h> |
---|
27 | |
---|
28 | #include <rtems/posix/pthreadimpl.h> |
---|
29 | #include <rtems/posix/priorityimpl.h> |
---|
30 | #include <rtems/score/threadimpl.h> |
---|
31 | #include <rtems/score/schedulerimpl.h> |
---|
32 | |
---|
33 | static int _POSIX_Set_sched_param( |
---|
34 | Thread_Control *the_thread, |
---|
35 | int policy, |
---|
36 | struct sched_param *param, |
---|
37 | Thread_CPU_budget_algorithms budget_algorithm, |
---|
38 | Thread_CPU_budget_algorithm_callout budget_callout, |
---|
39 | Thread_queue_Context *queue_context |
---|
40 | ) |
---|
41 | { |
---|
42 | const Scheduler_Control *scheduler; |
---|
43 | POSIX_API_Control *api; |
---|
44 | int low_prio; |
---|
45 | int high_prio; |
---|
46 | bool valid; |
---|
47 | Priority_Control core_normal_prio; |
---|
48 | Priority_Control core_low_prio; |
---|
49 | |
---|
50 | if ( policy == SCHED_SPORADIC ) { |
---|
51 | low_prio = param->sched_ss_low_priority; |
---|
52 | high_prio = param->sched_priority; |
---|
53 | } else { |
---|
54 | low_prio = param->sched_priority; |
---|
55 | high_prio = low_prio; |
---|
56 | } |
---|
57 | |
---|
58 | scheduler = _Thread_Scheduler_get_home( the_thread ); |
---|
59 | |
---|
60 | core_normal_prio = _POSIX_Priority_To_core( scheduler, low_prio, &valid ); |
---|
61 | if ( !valid ) { |
---|
62 | return EINVAL; |
---|
63 | } |
---|
64 | |
---|
65 | core_low_prio = _POSIX_Priority_To_core( scheduler, high_prio, &valid ); |
---|
66 | if ( !valid ) { |
---|
67 | return EINVAL; |
---|
68 | } |
---|
69 | |
---|
70 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
71 | |
---|
72 | _Watchdog_Per_CPU_remove_relative( &api->Sporadic.Timer ); |
---|
73 | |
---|
74 | _Priority_Node_set_priority( &the_thread->Real_priority, core_normal_prio ); |
---|
75 | |
---|
76 | if ( _Priority_Node_is_active( &api->Sporadic.Low_priority ) ) { |
---|
77 | _Thread_Priority_add( |
---|
78 | the_thread, |
---|
79 | &the_thread->Real_priority, |
---|
80 | queue_context |
---|
81 | ); |
---|
82 | _Thread_Priority_remove( |
---|
83 | the_thread, |
---|
84 | &api->Sporadic.Low_priority, |
---|
85 | queue_context |
---|
86 | ); |
---|
87 | _Priority_Node_set_inactive( &api->Sporadic.Low_priority ); |
---|
88 | } else { |
---|
89 | _Thread_Priority_changed( |
---|
90 | the_thread, |
---|
91 | &the_thread->Real_priority, |
---|
92 | false, |
---|
93 | queue_context |
---|
94 | ); |
---|
95 | } |
---|
96 | |
---|
97 | api->Attributes.schedpolicy = policy; |
---|
98 | api->Attributes.schedparam = *param; |
---|
99 | |
---|
100 | the_thread->budget_algorithm = budget_algorithm; |
---|
101 | the_thread->budget_callout = budget_callout; |
---|
102 | |
---|
103 | if ( policy == SCHED_SPORADIC ) { |
---|
104 | _Priority_Node_set_priority( &api->Sporadic.Low_priority, core_low_prio ); |
---|
105 | _POSIX_Threads_Sporadic_timer_insert( the_thread, api ); |
---|
106 | } else { |
---|
107 | the_thread->cpu_time_budget = |
---|
108 | rtems_configuration_get_ticks_per_timeslice(); |
---|
109 | } |
---|
110 | |
---|
111 | return 0; |
---|
112 | } |
---|
113 | |
---|
114 | int pthread_setschedparam( |
---|
115 | pthread_t thread, |
---|
116 | int policy, |
---|
117 | struct sched_param *param |
---|
118 | ) |
---|
119 | { |
---|
120 | Thread_CPU_budget_algorithms budget_algorithm; |
---|
121 | Thread_CPU_budget_algorithm_callout budget_callout; |
---|
122 | Thread_Control *the_thread; |
---|
123 | Per_CPU_Control *cpu_self; |
---|
124 | Thread_queue_Context queue_context; |
---|
125 | int error; |
---|
126 | |
---|
127 | if ( param == NULL ) { |
---|
128 | return EINVAL; |
---|
129 | } |
---|
130 | |
---|
131 | error = _POSIX_Thread_Translate_sched_param( |
---|
132 | policy, |
---|
133 | param, |
---|
134 | &budget_algorithm, |
---|
135 | &budget_callout |
---|
136 | ); |
---|
137 | if ( error != 0 ) { |
---|
138 | return error; |
---|
139 | } |
---|
140 | |
---|
141 | the_thread = _Thread_Get( thread, &queue_context.Lock_context.Lock_context ); |
---|
142 | |
---|
143 | if ( the_thread == NULL ) { |
---|
144 | return ESRCH; |
---|
145 | } |
---|
146 | |
---|
147 | _Thread_queue_Context_clear_priority_updates( &queue_context ); |
---|
148 | _Thread_Wait_acquire_critical( the_thread, &queue_context ); |
---|
149 | error = _POSIX_Set_sched_param( |
---|
150 | the_thread, |
---|
151 | policy, |
---|
152 | param, |
---|
153 | budget_algorithm, |
---|
154 | budget_callout, |
---|
155 | &queue_context |
---|
156 | ); |
---|
157 | cpu_self = _Thread_Dispatch_disable_critical( |
---|
158 | &queue_context.Lock_context.Lock_context |
---|
159 | ); |
---|
160 | _Thread_Wait_release( the_thread, &queue_context ); |
---|
161 | _Thread_Priority_update( &queue_context ); |
---|
162 | _Thread_Dispatch_enable( cpu_self ); |
---|
163 | return error; |
---|
164 | } |
---|