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/statusimpl.h> |
---|
23 | |
---|
24 | rtems_status_code rtems_semaphore_delete( |
---|
25 | rtems_id id |
---|
26 | ) |
---|
27 | { |
---|
28 | Semaphore_Control *the_semaphore; |
---|
29 | Thread_queue_Context queue_context; |
---|
30 | Status_Control status; |
---|
31 | |
---|
32 | _Objects_Allocator_lock(); |
---|
33 | the_semaphore = _Semaphore_Get( id, &queue_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 | _Thread_queue_Acquire_critical( |
---|
48 | &the_semaphore->Core_control.Wait_queue, |
---|
49 | &queue_context.Lock_context |
---|
50 | ); |
---|
51 | |
---|
52 | switch ( the_semaphore->variant ) { |
---|
53 | case SEMAPHORE_VARIANT_MUTEX: |
---|
54 | case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING: |
---|
55 | case SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL: |
---|
56 | if ( |
---|
57 | _CORE_mutex_Is_locked( |
---|
58 | &the_semaphore->Core_control.Mutex.Recursive.Mutex |
---|
59 | ) |
---|
60 | ) { |
---|
61 | status = STATUS_RESOURCE_IN_USE; |
---|
62 | } else { |
---|
63 | status = STATUS_SUCCESSFUL; |
---|
64 | } |
---|
65 | |
---|
66 | break; |
---|
67 | #if defined(RTEMS_SMP) |
---|
68 | case SEMAPHORE_VARIANT_MRSP: |
---|
69 | status = _MRSP_Can_destroy( &the_semaphore->Core_control.mrsp ); |
---|
70 | break; |
---|
71 | #endif |
---|
72 | default: |
---|
73 | _Assert( |
---|
74 | the_semaphore->variant == SEMAPHORE_VARIANT_SIMPLE_BINARY |
---|
75 | || the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING |
---|
76 | ); |
---|
77 | status = STATUS_SUCCESSFUL; |
---|
78 | break; |
---|
79 | } |
---|
80 | |
---|
81 | if ( status != STATUS_SUCCESSFUL ) { |
---|
82 | _Thread_queue_Release( |
---|
83 | &the_semaphore->Core_control.Wait_queue, |
---|
84 | &queue_context.Lock_context |
---|
85 | ); |
---|
86 | _Objects_Allocator_unlock(); |
---|
87 | return _Status_Get( status ); |
---|
88 | } |
---|
89 | |
---|
90 | _Objects_Close( &_Semaphore_Information, &the_semaphore->Object ); |
---|
91 | |
---|
92 | switch ( the_semaphore->variant ) { |
---|
93 | #if defined(RTEMS_SMP) |
---|
94 | case SEMAPHORE_VARIANT_MRSP: |
---|
95 | _MRSP_Destroy( &the_semaphore->Core_control.mrsp, &queue_context ); |
---|
96 | break; |
---|
97 | #endif |
---|
98 | default: |
---|
99 | _Assert( |
---|
100 | the_semaphore->variant == SEMAPHORE_VARIANT_MUTEX |
---|
101 | || the_semaphore->variant == SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING |
---|
102 | || the_semaphore->variant == SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL |
---|
103 | || the_semaphore->variant == SEMAPHORE_VARIANT_SIMPLE_BINARY |
---|
104 | || the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING |
---|
105 | ); |
---|
106 | _Thread_queue_Flush_critical( |
---|
107 | &the_semaphore->Core_control.Wait_queue.Queue, |
---|
108 | _Semaphore_Get_operations( the_semaphore ), |
---|
109 | _Thread_queue_Flush_status_object_was_deleted, |
---|
110 | &queue_context |
---|
111 | ); |
---|
112 | _Thread_queue_Destroy( &the_semaphore->Core_control.Wait_queue ); |
---|
113 | break; |
---|
114 | } |
---|
115 | |
---|
116 | #if defined(RTEMS_MULTIPROCESSING) |
---|
117 | if ( the_semaphore->is_global ) { |
---|
118 | |
---|
119 | _Objects_MP_Close( &_Semaphore_Information, id ); |
---|
120 | |
---|
121 | _Semaphore_MP_Send_process_packet( |
---|
122 | SEMAPHORE_MP_ANNOUNCE_DELETE, |
---|
123 | id, |
---|
124 | 0, /* Not used */ |
---|
125 | 0 /* Not used */ |
---|
126 | ); |
---|
127 | } |
---|
128 | #endif |
---|
129 | |
---|
130 | _Semaphore_Free( the_semaphore ); |
---|
131 | _Objects_Allocator_unlock(); |
---|
132 | return RTEMS_SUCCESSFUL; |
---|
133 | } |
---|