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-2007. |
---|
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.com/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/system.h> |
---|
29 | #include <rtems/posix/pthread.h> |
---|
30 | #include <rtems/posix/priority.h> |
---|
31 | #include <rtems/posix/time.h> |
---|
32 | |
---|
33 | int pthread_setschedparam( |
---|
34 | pthread_t thread, |
---|
35 | int policy, |
---|
36 | struct sched_param *param |
---|
37 | ) |
---|
38 | { |
---|
39 | register Thread_Control *the_thread; |
---|
40 | POSIX_API_Control *api; |
---|
41 | Thread_CPU_budget_algorithms budget_algorithm; |
---|
42 | Thread_CPU_budget_algorithm_callout budget_callout; |
---|
43 | Objects_Locations location; |
---|
44 | int rc; |
---|
45 | |
---|
46 | /* |
---|
47 | * Check all the parameters |
---|
48 | */ |
---|
49 | if ( !param ) |
---|
50 | return EINVAL; |
---|
51 | |
---|
52 | rc = _POSIX_Thread_Translate_sched_param( |
---|
53 | policy, |
---|
54 | param, |
---|
55 | &budget_algorithm, |
---|
56 | &budget_callout |
---|
57 | ); |
---|
58 | if ( rc ) |
---|
59 | return rc; |
---|
60 | |
---|
61 | /* |
---|
62 | * Actually change the scheduling policy and parameters |
---|
63 | */ |
---|
64 | the_thread = _Thread_Get( thread, &location ); |
---|
65 | switch ( location ) { |
---|
66 | |
---|
67 | case OBJECTS_LOCAL: |
---|
68 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
69 | |
---|
70 | if ( api->schedpolicy == SCHED_SPORADIC ) |
---|
71 | (void) _Watchdog_Remove( &api->Sporadic_timer ); |
---|
72 | |
---|
73 | api->schedpolicy = policy; |
---|
74 | api->schedparam = *param; |
---|
75 | the_thread->budget_algorithm = budget_algorithm; |
---|
76 | the_thread->budget_callout = budget_callout; |
---|
77 | |
---|
78 | switch ( api->schedpolicy ) { |
---|
79 | case SCHED_OTHER: |
---|
80 | case SCHED_FIFO: |
---|
81 | case SCHED_RR: |
---|
82 | the_thread->cpu_time_budget = _Thread_Ticks_per_timeslice; |
---|
83 | |
---|
84 | the_thread->real_priority = |
---|
85 | _POSIX_Priority_To_core( api->schedparam.sched_priority ); |
---|
86 | |
---|
87 | _Thread_Change_priority( |
---|
88 | the_thread, |
---|
89 | the_thread->real_priority, |
---|
90 | true |
---|
91 | ); |
---|
92 | break; |
---|
93 | |
---|
94 | case SCHED_SPORADIC: |
---|
95 | api->ss_high_priority = api->schedparam.sched_priority; |
---|
96 | _Watchdog_Remove( &api->Sporadic_timer ); |
---|
97 | _POSIX_Threads_Sporadic_budget_TSR( 0, the_thread ); |
---|
98 | break; |
---|
99 | } |
---|
100 | |
---|
101 | _Objects_Put( &the_thread->Object ); |
---|
102 | return 0; |
---|
103 | |
---|
104 | #if defined(RTEMS_MULTIPROCESSING) |
---|
105 | case OBJECTS_REMOTE: |
---|
106 | #endif |
---|
107 | case OBJECTS_ERROR: |
---|
108 | break; |
---|
109 | } |
---|
110 | |
---|
111 | return ESRCH; |
---|
112 | } |
---|