1 | /* |
---|
2 | * Copyright (c) 2014 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <rtems/rtems/semimpl.h> |
---|
20 | #include <rtems/rtems/tasksimpl.h> |
---|
21 | #include <rtems/score/schedulerimpl.h> |
---|
22 | |
---|
23 | static rtems_status_code _Semaphore_Set_priority( |
---|
24 | Semaphore_Control *the_semaphore, |
---|
25 | const Scheduler_Control *scheduler, |
---|
26 | rtems_task_priority new_priority, |
---|
27 | rtems_task_priority *old_priority_p, |
---|
28 | Thread_queue_Context *queue_context |
---|
29 | ) |
---|
30 | { |
---|
31 | rtems_status_code sc; |
---|
32 | bool valid; |
---|
33 | Priority_Control core_priority; |
---|
34 | Priority_Control old_priority; |
---|
35 | #if defined(RTEMS_SMP) |
---|
36 | MRSP_Control *mrsp; |
---|
37 | uint32_t scheduler_index; |
---|
38 | #endif |
---|
39 | |
---|
40 | core_priority = _RTEMS_Priority_To_core( scheduler, new_priority, &valid ); |
---|
41 | if ( new_priority != RTEMS_CURRENT_PRIORITY && !valid ) { |
---|
42 | return RTEMS_INVALID_PRIORITY; |
---|
43 | } |
---|
44 | |
---|
45 | switch ( the_semaphore->variant ) { |
---|
46 | case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING: |
---|
47 | _CORE_mutex_Acquire_critical( |
---|
48 | &the_semaphore->Core_control.Mutex.Recursive.Mutex, |
---|
49 | queue_context |
---|
50 | ); |
---|
51 | |
---|
52 | old_priority = the_semaphore->Core_control.Mutex.priority_ceiling; |
---|
53 | |
---|
54 | if ( new_priority != RTEMS_CURRENT_PRIORITY ) { |
---|
55 | the_semaphore->Core_control.Mutex.priority_ceiling = core_priority; |
---|
56 | } |
---|
57 | |
---|
58 | _CORE_mutex_Release( |
---|
59 | &the_semaphore->Core_control.Mutex.Recursive.Mutex, |
---|
60 | queue_context |
---|
61 | ); |
---|
62 | sc = RTEMS_SUCCESSFUL; |
---|
63 | break; |
---|
64 | #if defined(RTEMS_SMP) |
---|
65 | case SEMAPHORE_VARIANT_MRSP: |
---|
66 | mrsp = &the_semaphore->Core_control.MRSP; |
---|
67 | scheduler_index = _Scheduler_Get_index( scheduler ); |
---|
68 | |
---|
69 | _MRSP_Acquire_critical( mrsp, queue_context ); |
---|
70 | |
---|
71 | old_priority = _MRSP_Get_ceiling_priority( mrsp, scheduler_index ); |
---|
72 | |
---|
73 | if ( new_priority != RTEMS_CURRENT_PRIORITY ) { |
---|
74 | _MRSP_Set_ceiling_priority( mrsp, scheduler_index, core_priority ); |
---|
75 | } |
---|
76 | |
---|
77 | _MRSP_Release( mrsp, queue_context ); |
---|
78 | sc = RTEMS_SUCCESSFUL; |
---|
79 | break; |
---|
80 | #endif |
---|
81 | default: |
---|
82 | _Assert( |
---|
83 | the_semaphore->variant == SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY |
---|
84 | || the_semaphore->variant == SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL |
---|
85 | || the_semaphore->variant == SEMAPHORE_VARIANT_SIMPLE_BINARY |
---|
86 | || the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING |
---|
87 | ); |
---|
88 | _ISR_lock_ISR_enable( &queue_context->Lock_context ); |
---|
89 | old_priority = 0; |
---|
90 | sc = RTEMS_NOT_DEFINED; |
---|
91 | break; |
---|
92 | } |
---|
93 | |
---|
94 | *old_priority_p = _RTEMS_Priority_From_core( scheduler, old_priority ); |
---|
95 | |
---|
96 | return sc; |
---|
97 | } |
---|
98 | |
---|
99 | rtems_status_code rtems_semaphore_set_priority( |
---|
100 | rtems_id semaphore_id, |
---|
101 | rtems_id scheduler_id, |
---|
102 | rtems_task_priority new_priority, |
---|
103 | rtems_task_priority *old_priority |
---|
104 | ) |
---|
105 | { |
---|
106 | const Scheduler_Control *scheduler; |
---|
107 | Semaphore_Control *the_semaphore; |
---|
108 | Thread_queue_Context queue_context; |
---|
109 | |
---|
110 | if ( old_priority == NULL ) { |
---|
111 | return RTEMS_INVALID_ADDRESS; |
---|
112 | } |
---|
113 | |
---|
114 | if ( !_Scheduler_Get_by_id( scheduler_id, &scheduler ) ) { |
---|
115 | return RTEMS_INVALID_ID; |
---|
116 | } |
---|
117 | |
---|
118 | the_semaphore = _Semaphore_Get( semaphore_id, &queue_context ); |
---|
119 | |
---|
120 | if ( the_semaphore == NULL ) { |
---|
121 | #if defined(RTEMS_MULTIPROCESSING) |
---|
122 | if ( _Semaphore_MP_Is_remote( semaphore_id ) ) { |
---|
123 | return RTEMS_ILLEGAL_ON_REMOTE_OBJECT; |
---|
124 | } |
---|
125 | #endif |
---|
126 | |
---|
127 | return RTEMS_INVALID_ID; |
---|
128 | } |
---|
129 | |
---|
130 | return _Semaphore_Set_priority( |
---|
131 | the_semaphore, |
---|
132 | scheduler, |
---|
133 | new_priority, |
---|
134 | old_priority, |
---|
135 | &queue_context |
---|
136 | ); |
---|
137 | } |
---|