1 | /* |
---|
2 | * Copyright (c) 2016 embedded brains GmbH |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | |
---|
9 | #if HAVE_CONFIG_H |
---|
10 | #include "config.h" |
---|
11 | #endif |
---|
12 | |
---|
13 | #include <pthread.h> |
---|
14 | #include <errno.h> |
---|
15 | |
---|
16 | #include <rtems/posix/priorityimpl.h> |
---|
17 | #include <rtems/posix/threadsup.h> |
---|
18 | #include <rtems/score/threadimpl.h> |
---|
19 | #include <rtems/score/schedulerimpl.h> |
---|
20 | |
---|
21 | typedef struct { |
---|
22 | int prio; |
---|
23 | int error; |
---|
24 | } POSIX_Set_sched_prio_context; |
---|
25 | |
---|
26 | static bool _POSIX_Set_sched_prio_filter( |
---|
27 | Thread_Control *the_thread, |
---|
28 | Priority_Control *new_priority_p, |
---|
29 | void *arg |
---|
30 | ) |
---|
31 | { |
---|
32 | POSIX_Set_sched_prio_context *context; |
---|
33 | int prio; |
---|
34 | const Scheduler_Control *scheduler; |
---|
35 | POSIX_API_Control *api; |
---|
36 | bool valid; |
---|
37 | Priority_Control current_priority; |
---|
38 | Priority_Control new_priority; |
---|
39 | |
---|
40 | context = arg; |
---|
41 | prio = context->prio; |
---|
42 | scheduler = _Scheduler_Get_own( the_thread ); |
---|
43 | |
---|
44 | new_priority = _POSIX_Priority_To_core( scheduler, prio, &valid ); |
---|
45 | if ( !valid ) { |
---|
46 | context->error = EINVAL; |
---|
47 | return false; |
---|
48 | } |
---|
49 | |
---|
50 | *new_priority_p = new_priority; |
---|
51 | |
---|
52 | current_priority = _Thread_Get_priority( the_thread ); |
---|
53 | the_thread->real_priority = new_priority; |
---|
54 | |
---|
55 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
56 | |
---|
57 | api->Sporadic.high_priority = new_priority; |
---|
58 | |
---|
59 | if ( api->Sporadic.low_priority < new_priority ) { |
---|
60 | api->Sporadic.low_priority = new_priority; |
---|
61 | } |
---|
62 | |
---|
63 | context->error = 0; |
---|
64 | return _Thread_Priority_less_than( current_priority, new_priority ) |
---|
65 | || !_Thread_Owns_resources( the_thread ); |
---|
66 | } |
---|
67 | |
---|
68 | int pthread_setschedprio( pthread_t thread, int prio ) |
---|
69 | { |
---|
70 | Thread_Control *the_thread; |
---|
71 | Per_CPU_Control *cpu_self; |
---|
72 | POSIX_Set_sched_prio_context context; |
---|
73 | ISR_lock_Context lock_context; |
---|
74 | |
---|
75 | context.prio = prio; |
---|
76 | |
---|
77 | the_thread = _Thread_Get( thread, &lock_context ); |
---|
78 | |
---|
79 | if ( the_thread == NULL ) { |
---|
80 | return ESRCH; |
---|
81 | } |
---|
82 | |
---|
83 | cpu_self = _Thread_Dispatch_disable_critical( &lock_context ); |
---|
84 | _ISR_lock_ISR_enable( &lock_context ); |
---|
85 | |
---|
86 | _Thread_Change_priority( |
---|
87 | the_thread, |
---|
88 | 0, |
---|
89 | &context, |
---|
90 | _POSIX_Set_sched_prio_filter, |
---|
91 | true |
---|
92 | ); |
---|
93 | |
---|
94 | _Thread_Dispatch_enable( cpu_self ); |
---|
95 | return context.error; |
---|
96 | } |
---|