1 | /* |
---|
2 | * 13.5.2 Dynamic Thread Scheduling Parameters Access, |
---|
3 | * P1003.1c/Draft 10, p. 124 |
---|
4 | */ |
---|
5 | |
---|
6 | /* COPYRIGHT (c) 1989-2007. |
---|
7 | * On-Line Applications Research Corporation (OAR). |
---|
8 | * |
---|
9 | * The license and distribution terms for this file may be |
---|
10 | * found in the file LICENSE in this distribution or at |
---|
11 | * http://www.rtems.com/license/LICENSE. |
---|
12 | * |
---|
13 | * $Id$ |
---|
14 | */ |
---|
15 | |
---|
16 | #if HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <pthread.h> |
---|
21 | #include <errno.h> |
---|
22 | |
---|
23 | #include <rtems/system.h> |
---|
24 | #include <rtems/posix/pthread.h> |
---|
25 | #include <rtems/posix/priority.h> |
---|
26 | #include <rtems/posix/time.h> |
---|
27 | |
---|
28 | int pthread_setschedparam( |
---|
29 | pthread_t thread, |
---|
30 | int policy, |
---|
31 | struct sched_param *param |
---|
32 | ) |
---|
33 | { |
---|
34 | register Thread_Control *the_thread; |
---|
35 | POSIX_API_Control *api; |
---|
36 | Thread_CPU_budget_algorithms budget_algorithm; |
---|
37 | Thread_CPU_budget_algorithm_callout budget_callout; |
---|
38 | Objects_Locations location; |
---|
39 | int rc; |
---|
40 | |
---|
41 | /* |
---|
42 | * Check all the parameters |
---|
43 | */ |
---|
44 | if ( !param ) |
---|
45 | return EINVAL; |
---|
46 | |
---|
47 | rc = _POSIX_Thread_Translate_sched_param( |
---|
48 | policy, |
---|
49 | param, |
---|
50 | &budget_algorithm, |
---|
51 | &budget_callout |
---|
52 | ); |
---|
53 | if ( rc ) |
---|
54 | return rc; |
---|
55 | |
---|
56 | /* |
---|
57 | * Actually change the scheduling policy and parameters |
---|
58 | */ |
---|
59 | the_thread = _POSIX_Threads_Get( thread, &location ); |
---|
60 | switch ( location ) { |
---|
61 | |
---|
62 | case OBJECTS_LOCAL: |
---|
63 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
64 | |
---|
65 | if ( api->schedpolicy == SCHED_SPORADIC ) |
---|
66 | (void) _Watchdog_Remove( &api->Sporadic_timer ); |
---|
67 | |
---|
68 | api->schedpolicy = policy; |
---|
69 | api->schedparam = *param; |
---|
70 | the_thread->budget_algorithm = budget_algorithm; |
---|
71 | the_thread->budget_callout = budget_callout; |
---|
72 | |
---|
73 | switch ( api->schedpolicy ) { |
---|
74 | case SCHED_OTHER: |
---|
75 | case SCHED_FIFO: |
---|
76 | case SCHED_RR: |
---|
77 | the_thread->cpu_time_budget = _Thread_Ticks_per_timeslice; |
---|
78 | |
---|
79 | the_thread->real_priority = |
---|
80 | _POSIX_Priority_To_core( api->schedparam.sched_priority ); |
---|
81 | |
---|
82 | _Thread_Change_priority( |
---|
83 | the_thread, |
---|
84 | the_thread->real_priority, |
---|
85 | true |
---|
86 | ); |
---|
87 | break; |
---|
88 | |
---|
89 | case SCHED_SPORADIC: |
---|
90 | api->ss_high_priority = api->schedparam.sched_priority; |
---|
91 | _Watchdog_Remove( &api->Sporadic_timer ); |
---|
92 | _POSIX_Threads_Sporadic_budget_TSR( 0, the_thread ); |
---|
93 | break; |
---|
94 | } |
---|
95 | |
---|
96 | _Thread_Enable_dispatch(); |
---|
97 | return 0; |
---|
98 | |
---|
99 | #if defined(RTEMS_MULTIPROCESSING) |
---|
100 | case OBJECTS_REMOTE: |
---|
101 | #endif |
---|
102 | case OBJECTS_ERROR: |
---|
103 | break; |
---|
104 | } |
---|
105 | |
---|
106 | return ESRCH; |
---|
107 | } |
---|