[8fcafdd5] | 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 | |
---|
[77ff5599] | 23 | static rtems_status_code _Semaphore_Is_scheduler_valid( |
---|
| 24 | const CORE_ceiling_mutex_Control *the_mutex, |
---|
| 25 | const Scheduler_Control *scheduler |
---|
| 26 | ) |
---|
| 27 | { |
---|
| 28 | #if defined(RTEMS_SMP) |
---|
| 29 | if ( scheduler != _CORE_ceiling_mutex_Get_scheduler( the_mutex ) ) { |
---|
| 30 | return RTEMS_NOT_DEFINED; |
---|
| 31 | } |
---|
| 32 | #endif |
---|
| 33 | |
---|
| 34 | return RTEMS_SUCCESSFUL; |
---|
| 35 | } |
---|
| 36 | |
---|
[8fcafdd5] | 37 | static rtems_status_code _Semaphore_Set_priority( |
---|
[c82835a] | 38 | Semaphore_Control *the_semaphore, |
---|
| 39 | const Scheduler_Control *scheduler, |
---|
| 40 | rtems_task_priority new_priority, |
---|
| 41 | rtems_task_priority *old_priority_p, |
---|
| 42 | Thread_queue_Context *queue_context |
---|
[8fcafdd5] | 43 | ) |
---|
| 44 | { |
---|
[300f6a48] | 45 | rtems_status_code sc; |
---|
| 46 | bool valid; |
---|
| 47 | Priority_Control core_priority; |
---|
| 48 | Priority_Control old_priority; |
---|
| 49 | Per_CPU_Control *cpu_self; |
---|
[8fcafdd5] | 50 | |
---|
[c82835a] | 51 | core_priority = _RTEMS_Priority_To_core( scheduler, new_priority, &valid ); |
---|
| 52 | if ( new_priority != RTEMS_CURRENT_PRIORITY && !valid ) { |
---|
[70ddaa0e] | 53 | _ISR_lock_ISR_enable( &queue_context->Lock_context.Lock_context ); |
---|
[c82835a] | 54 | return RTEMS_INVALID_PRIORITY; |
---|
| 55 | } |
---|
[8fcafdd5] | 56 | |
---|
[300f6a48] | 57 | _Thread_queue_Context_clear_priority_updates( queue_context ); |
---|
[77ff5599] | 58 | _Thread_queue_Acquire_critical( |
---|
| 59 | &the_semaphore->Core_control.Wait_queue, |
---|
[114e408] | 60 | queue_context |
---|
[77ff5599] | 61 | ); |
---|
| 62 | |
---|
[33e250c9] | 63 | switch ( the_semaphore->variant ) { |
---|
| 64 | case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING: |
---|
[77ff5599] | 65 | sc = _Semaphore_Is_scheduler_valid( |
---|
| 66 | &the_semaphore->Core_control.Mutex, |
---|
| 67 | scheduler |
---|
[33e250c9] | 68 | ); |
---|
| 69 | |
---|
[77ff5599] | 70 | old_priority = _CORE_ceiling_mutex_Get_priority( |
---|
| 71 | &the_semaphore->Core_control.Mutex |
---|
| 72 | ); |
---|
[33e250c9] | 73 | |
---|
[77ff5599] | 74 | if ( sc == RTEMS_SUCCESSFUL && new_priority != RTEMS_CURRENT_PRIORITY ) { |
---|
| 75 | _CORE_ceiling_mutex_Set_priority( |
---|
| 76 | &the_semaphore->Core_control.Mutex, |
---|
[300f6a48] | 77 | core_priority, |
---|
| 78 | queue_context |
---|
[77ff5599] | 79 | ); |
---|
[33e250c9] | 80 | } |
---|
| 81 | |
---|
| 82 | break; |
---|
[8fcafdd5] | 83 | #if defined(RTEMS_SMP) |
---|
[33e250c9] | 84 | case SEMAPHORE_VARIANT_MRSP: |
---|
[77ff5599] | 85 | old_priority = _MRSP_Get_priority( |
---|
| 86 | &the_semaphore->Core_control.MRSP, |
---|
| 87 | scheduler |
---|
| 88 | ); |
---|
[8fcafdd5] | 89 | |
---|
[33e250c9] | 90 | if ( new_priority != RTEMS_CURRENT_PRIORITY ) { |
---|
[77ff5599] | 91 | _MRSP_Set_priority( |
---|
| 92 | &the_semaphore->Core_control.MRSP, |
---|
| 93 | scheduler, |
---|
| 94 | core_priority |
---|
| 95 | ); |
---|
[33e250c9] | 96 | } |
---|
[f009ed0] | 97 | |
---|
[33e250c9] | 98 | sc = RTEMS_SUCCESSFUL; |
---|
| 99 | break; |
---|
[8fcafdd5] | 100 | #endif |
---|
[33e250c9] | 101 | default: |
---|
| 102 | _Assert( |
---|
[0b713f89] | 103 | the_semaphore->variant == SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY |
---|
[33e250c9] | 104 | || the_semaphore->variant == SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL |
---|
| 105 | || the_semaphore->variant == SEMAPHORE_VARIANT_SIMPLE_BINARY |
---|
| 106 | || the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING |
---|
| 107 | ); |
---|
| 108 | old_priority = 0; |
---|
| 109 | sc = RTEMS_NOT_DEFINED; |
---|
| 110 | break; |
---|
[8fcafdd5] | 111 | } |
---|
| 112 | |
---|
[db3a3de] | 113 | cpu_self = _Thread_queue_Dispatch_disable( queue_context ); |
---|
[77ff5599] | 114 | _Thread_queue_Release( |
---|
| 115 | &the_semaphore->Core_control.Wait_queue, |
---|
[114e408] | 116 | queue_context |
---|
[77ff5599] | 117 | ); |
---|
[300f6a48] | 118 | _Thread_Priority_update( queue_context ); |
---|
| 119 | _Thread_Dispatch_enable( cpu_self ); |
---|
[8fcafdd5] | 120 | |
---|
[77ff5599] | 121 | *old_priority_p = _RTEMS_Priority_From_core( scheduler, old_priority ); |
---|
[8fcafdd5] | 122 | return sc; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | rtems_status_code rtems_semaphore_set_priority( |
---|
| 126 | rtems_id semaphore_id, |
---|
| 127 | rtems_id scheduler_id, |
---|
| 128 | rtems_task_priority new_priority, |
---|
| 129 | rtems_task_priority *old_priority |
---|
| 130 | ) |
---|
| 131 | { |
---|
[c82835a] | 132 | const Scheduler_Control *scheduler; |
---|
| 133 | Semaphore_Control *the_semaphore; |
---|
| 134 | Thread_queue_Context queue_context; |
---|
[8fcafdd5] | 135 | |
---|
| 136 | if ( old_priority == NULL ) { |
---|
| 137 | return RTEMS_INVALID_ADDRESS; |
---|
| 138 | } |
---|
| 139 | |
---|
[2612a0b] | 140 | scheduler = _Scheduler_Get_by_id( scheduler_id ); |
---|
| 141 | if ( scheduler == NULL ) { |
---|
[8fcafdd5] | 142 | return RTEMS_INVALID_ID; |
---|
| 143 | } |
---|
| 144 | |
---|
[0e1d11f3] | 145 | the_semaphore = _Semaphore_Get( semaphore_id, &queue_context ); |
---|
[62c528e6] | 146 | |
---|
| 147 | if ( the_semaphore == NULL ) { |
---|
[8fcafdd5] | 148 | #if defined(RTEMS_MULTIPROCESSING) |
---|
[62c528e6] | 149 | if ( _Semaphore_MP_Is_remote( semaphore_id ) ) { |
---|
[8fcafdd5] | 150 | return RTEMS_ILLEGAL_ON_REMOTE_OBJECT; |
---|
[62c528e6] | 151 | } |
---|
[8fcafdd5] | 152 | #endif |
---|
[62c528e6] | 153 | |
---|
| 154 | return RTEMS_INVALID_ID; |
---|
[8fcafdd5] | 155 | } |
---|
| 156 | |
---|
[62c528e6] | 157 | return _Semaphore_Set_priority( |
---|
| 158 | the_semaphore, |
---|
[c82835a] | 159 | scheduler, |
---|
[62c528e6] | 160 | new_priority, |
---|
| 161 | old_priority, |
---|
[631b3c8] | 162 | &queue_context |
---|
[62c528e6] | 163 | ); |
---|
[8fcafdd5] | 164 | } |
---|