1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief RTEMS Delete Semaphore |
---|
5 | * @ingroup ClassicSem |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2014. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/rtems/semimpl.h> |
---|
22 | #include <rtems/rtems/attrimpl.h> |
---|
23 | |
---|
24 | rtems_status_code rtems_semaphore_delete( |
---|
25 | rtems_id id |
---|
26 | ) |
---|
27 | { |
---|
28 | Semaphore_Control *the_semaphore; |
---|
29 | ISR_lock_Context lock_context; |
---|
30 | rtems_attribute attribute_set; |
---|
31 | |
---|
32 | _Objects_Allocator_lock(); |
---|
33 | the_semaphore = _Semaphore_Get_interrupt_disable( id, &lock_context ); |
---|
34 | |
---|
35 | if ( the_semaphore == NULL ) { |
---|
36 | _Objects_Allocator_unlock(); |
---|
37 | |
---|
38 | #if defined(RTEMS_MULTIPROCESSING) |
---|
39 | if ( _Semaphore_MP_Is_remote( id ) ) { |
---|
40 | return RTEMS_ILLEGAL_ON_REMOTE_OBJECT; |
---|
41 | } |
---|
42 | #endif |
---|
43 | |
---|
44 | return RTEMS_INVALID_ID; |
---|
45 | } |
---|
46 | |
---|
47 | attribute_set = the_semaphore->attribute_set; |
---|
48 | |
---|
49 | #if defined(RTEMS_SMP) |
---|
50 | if ( _Attributes_Is_multiprocessor_resource_sharing( attribute_set ) ) { |
---|
51 | MRSP_Status mrsp_status; |
---|
52 | |
---|
53 | _MRSP_Acquire_critical( |
---|
54 | &the_semaphore->Core_control.mrsp, |
---|
55 | &lock_context |
---|
56 | ); |
---|
57 | mrsp_status = _MRSP_Can_destroy( &the_semaphore->Core_control.mrsp ); |
---|
58 | if ( mrsp_status != MRSP_SUCCESSFUL ) { |
---|
59 | _MRSP_Release( |
---|
60 | &the_semaphore->Core_control.mrsp, |
---|
61 | &lock_context |
---|
62 | ); |
---|
63 | _Objects_Allocator_unlock(); |
---|
64 | return _Semaphore_Translate_MRSP_status_code( mrsp_status ); |
---|
65 | } |
---|
66 | } else |
---|
67 | #endif |
---|
68 | if ( !_Attributes_Is_counting_semaphore( attribute_set ) ) { |
---|
69 | _CORE_mutex_Acquire_critical( |
---|
70 | &the_semaphore->Core_control.mutex, |
---|
71 | &lock_context |
---|
72 | ); |
---|
73 | |
---|
74 | if ( |
---|
75 | _CORE_mutex_Is_locked( &the_semaphore->Core_control.mutex ) |
---|
76 | && !_Attributes_Is_simple_binary_semaphore( attribute_set ) |
---|
77 | ) { |
---|
78 | _CORE_mutex_Release( |
---|
79 | &the_semaphore->Core_control.mutex, |
---|
80 | &lock_context |
---|
81 | ); |
---|
82 | _Objects_Allocator_unlock(); |
---|
83 | return RTEMS_RESOURCE_IN_USE; |
---|
84 | } |
---|
85 | } else { |
---|
86 | _CORE_semaphore_Acquire_critical( |
---|
87 | &the_semaphore->Core_control.semaphore, |
---|
88 | &lock_context |
---|
89 | ); |
---|
90 | } |
---|
91 | |
---|
92 | _Objects_Close( &_Semaphore_Information, &the_semaphore->Object ); |
---|
93 | |
---|
94 | #if defined(RTEMS_SMP) |
---|
95 | if ( _Attributes_Is_multiprocessor_resource_sharing( attribute_set ) ) { |
---|
96 | _MRSP_Destroy( &the_semaphore->Core_control.mrsp, &lock_context ); |
---|
97 | } else |
---|
98 | #endif |
---|
99 | if ( !_Attributes_Is_counting_semaphore( attribute_set ) ) { |
---|
100 | _CORE_mutex_Flush( |
---|
101 | &the_semaphore->Core_control.mutex, |
---|
102 | _CORE_mutex_Was_deleted, |
---|
103 | _Semaphore_MP_Send_object_was_deleted, |
---|
104 | id, |
---|
105 | &lock_context |
---|
106 | ); |
---|
107 | _CORE_mutex_Destroy( &the_semaphore->Core_control.mutex ); |
---|
108 | } else { |
---|
109 | _CORE_semaphore_Destroy( |
---|
110 | &the_semaphore->Core_control.semaphore, |
---|
111 | _Semaphore_MP_Send_object_was_deleted, |
---|
112 | id, |
---|
113 | &lock_context |
---|
114 | ); |
---|
115 | } |
---|
116 | |
---|
117 | #if defined(RTEMS_MULTIPROCESSING) |
---|
118 | if ( _Attributes_Is_global( attribute_set ) ) { |
---|
119 | |
---|
120 | _Objects_MP_Close( &_Semaphore_Information, id ); |
---|
121 | |
---|
122 | _Semaphore_MP_Send_process_packet( |
---|
123 | SEMAPHORE_MP_ANNOUNCE_DELETE, |
---|
124 | id, |
---|
125 | 0, /* Not used */ |
---|
126 | 0 /* Not used */ |
---|
127 | ); |
---|
128 | } |
---|
129 | #endif |
---|
130 | |
---|
131 | _Semaphore_Free( the_semaphore ); |
---|
132 | _Objects_Allocator_unlock(); |
---|
133 | return RTEMS_SUCCESSFUL; |
---|
134 | } |
---|