1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup RTEMSImplClassicSemaphore |
---|
5 | * |
---|
6 | * @brief This source file contains the implementation of |
---|
7 | * rtems_semaphore_delete(). |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * COPYRIGHT (c) 1989-2014. |
---|
12 | * On-Line Applications Research Corporation (OAR). |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.org/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #ifdef HAVE_CONFIG_H |
---|
20 | #include "config.h" |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <rtems/rtems/semimpl.h> |
---|
24 | #include <rtems/rtems/statusimpl.h> |
---|
25 | |
---|
26 | rtems_status_code rtems_semaphore_delete( |
---|
27 | rtems_id id |
---|
28 | ) |
---|
29 | { |
---|
30 | Semaphore_Control *the_semaphore; |
---|
31 | Thread_queue_Context queue_context; |
---|
32 | uintptr_t flags; |
---|
33 | Semaphore_Variant variant; |
---|
34 | Status_Control status; |
---|
35 | |
---|
36 | _Objects_Allocator_lock(); |
---|
37 | the_semaphore = _Semaphore_Get( id, &queue_context ); |
---|
38 | |
---|
39 | if ( the_semaphore == NULL ) { |
---|
40 | _Objects_Allocator_unlock(); |
---|
41 | |
---|
42 | #if defined(RTEMS_MULTIPROCESSING) |
---|
43 | if ( _Semaphore_MP_Is_remote( id ) ) { |
---|
44 | return RTEMS_ILLEGAL_ON_REMOTE_OBJECT; |
---|
45 | } |
---|
46 | #endif |
---|
47 | |
---|
48 | return RTEMS_INVALID_ID; |
---|
49 | } |
---|
50 | |
---|
51 | _Thread_queue_Acquire_critical( |
---|
52 | &the_semaphore->Core_control.Wait_queue, |
---|
53 | &queue_context |
---|
54 | ); |
---|
55 | flags = _Semaphore_Get_flags( the_semaphore ); |
---|
56 | variant = _Semaphore_Get_variant( flags ); |
---|
57 | |
---|
58 | switch ( variant ) { |
---|
59 | case SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY: |
---|
60 | case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING: |
---|
61 | case SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL: |
---|
62 | if ( |
---|
63 | _CORE_mutex_Is_locked( |
---|
64 | &the_semaphore->Core_control.Mutex.Recursive.Mutex |
---|
65 | ) |
---|
66 | ) { |
---|
67 | status = STATUS_RESOURCE_IN_USE; |
---|
68 | } else { |
---|
69 | status = STATUS_SUCCESSFUL; |
---|
70 | } |
---|
71 | |
---|
72 | break; |
---|
73 | #if defined(RTEMS_SMP) |
---|
74 | case SEMAPHORE_VARIANT_MRSP: |
---|
75 | status = _MRSP_Can_destroy( &the_semaphore->Core_control.MRSP ); |
---|
76 | break; |
---|
77 | #endif |
---|
78 | default: |
---|
79 | _Assert( |
---|
80 | variant == SEMAPHORE_VARIANT_SIMPLE_BINARY |
---|
81 | || variant == SEMAPHORE_VARIANT_COUNTING |
---|
82 | ); |
---|
83 | status = STATUS_SUCCESSFUL; |
---|
84 | break; |
---|
85 | } |
---|
86 | |
---|
87 | if ( status != STATUS_SUCCESSFUL ) { |
---|
88 | _Thread_queue_Release( |
---|
89 | &the_semaphore->Core_control.Wait_queue, |
---|
90 | &queue_context |
---|
91 | ); |
---|
92 | _Objects_Allocator_unlock(); |
---|
93 | return _Status_Get( status ); |
---|
94 | } |
---|
95 | |
---|
96 | _Objects_Close( &_Semaphore_Information, &the_semaphore->Object ); |
---|
97 | |
---|
98 | switch ( variant ) { |
---|
99 | #if defined(RTEMS_SMP) |
---|
100 | case SEMAPHORE_VARIANT_MRSP: |
---|
101 | _MRSP_Destroy( &the_semaphore->Core_control.MRSP, &queue_context ); |
---|
102 | break; |
---|
103 | #endif |
---|
104 | default: |
---|
105 | _Assert( |
---|
106 | variant == SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY |
---|
107 | || variant == SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING |
---|
108 | || variant == SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL |
---|
109 | || variant == SEMAPHORE_VARIANT_SIMPLE_BINARY |
---|
110 | || variant == SEMAPHORE_VARIANT_COUNTING |
---|
111 | ); |
---|
112 | _Thread_queue_Flush_critical( |
---|
113 | &the_semaphore->Core_control.Wait_queue.Queue, |
---|
114 | _Semaphore_Get_operations( flags ), |
---|
115 | _Thread_queue_Flush_status_object_was_deleted, |
---|
116 | &queue_context |
---|
117 | ); |
---|
118 | _Thread_queue_Destroy( &the_semaphore->Core_control.Wait_queue ); |
---|
119 | break; |
---|
120 | } |
---|
121 | |
---|
122 | #if defined(RTEMS_MULTIPROCESSING) |
---|
123 | if ( _Semaphore_Is_global( flags ) ) { |
---|
124 | |
---|
125 | _Objects_MP_Close( &_Semaphore_Information, id ); |
---|
126 | |
---|
127 | _Semaphore_MP_Send_process_packet( |
---|
128 | SEMAPHORE_MP_ANNOUNCE_DELETE, |
---|
129 | id, |
---|
130 | 0, /* Not used */ |
---|
131 | 0 /* Not used */ |
---|
132 | ); |
---|
133 | } |
---|
134 | #endif |
---|
135 | |
---|
136 | _Semaphore_Free( the_semaphore ); |
---|
137 | _Objects_Allocator_unlock(); |
---|
138 | return RTEMS_SUCCESSFUL; |
---|
139 | } |
---|