1 | /* |
---|
2 | * Semaphore Manager |
---|
3 | * |
---|
4 | * DESCRIPTION: |
---|
5 | * |
---|
6 | * This package is the implementation of the Semaphore Manager. |
---|
7 | * This manager utilizes standard Dijkstra counting semaphores to provide |
---|
8 | * synchronization and mutual exclusion capabilities. |
---|
9 | * |
---|
10 | * Directives provided are: |
---|
11 | * |
---|
12 | * + create a semaphore |
---|
13 | * + get an ID of a semaphore |
---|
14 | * + delete a semaphore |
---|
15 | * + acquire a semaphore |
---|
16 | * + release a semaphore |
---|
17 | * |
---|
18 | * COPYRIGHT (c) 1989-2007. |
---|
19 | * On-Line Applications Research Corporation (OAR). |
---|
20 | * |
---|
21 | * The license and distribution terms for this file may be |
---|
22 | * found in the file LICENSE in this distribution or at |
---|
23 | * http://www.rtems.com/license/LICENSE. |
---|
24 | * |
---|
25 | * $Id$ |
---|
26 | */ |
---|
27 | |
---|
28 | #if HAVE_CONFIG_H |
---|
29 | #include "config.h" |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <rtems/system.h> |
---|
33 | #include <rtems/rtems/status.h> |
---|
34 | #include <rtems/rtems/support.h> |
---|
35 | #include <rtems/rtems/attr.h> |
---|
36 | #include <rtems/score/isr.h> |
---|
37 | #include <rtems/score/object.h> |
---|
38 | #include <rtems/rtems/options.h> |
---|
39 | #include <rtems/rtems/sem.h> |
---|
40 | #include <rtems/score/coremutex.h> |
---|
41 | #include <rtems/score/coresem.h> |
---|
42 | #include <rtems/score/states.h> |
---|
43 | #include <rtems/score/thread.h> |
---|
44 | #include <rtems/score/threadq.h> |
---|
45 | #if defined(RTEMS_MULTIPROCESSING) |
---|
46 | #include <rtems/score/mpci.h> |
---|
47 | #endif |
---|
48 | #include <rtems/score/sysstate.h> |
---|
49 | |
---|
50 | #include <rtems/score/interr.h> |
---|
51 | |
---|
52 | /*PAGE |
---|
53 | * |
---|
54 | * rtems_semaphore_delete |
---|
55 | * |
---|
56 | * This directive allows a thread to delete a semaphore specified by |
---|
57 | * the semaphore id. The semaphore is freed back to the inactive |
---|
58 | * semaphore chain. |
---|
59 | * |
---|
60 | * Input parameters: |
---|
61 | * id - semaphore id |
---|
62 | * |
---|
63 | * Output parameters: |
---|
64 | * RTEMS_SUCCESSFUL - if successful |
---|
65 | * error code - if unsuccessful |
---|
66 | */ |
---|
67 | |
---|
68 | #if defined(RTEMS_MULTIPROCESSING) |
---|
69 | #define SEMAPHORE_MP_OBJECT_WAS_DELETED _Semaphore_MP_Send_object_was_deleted |
---|
70 | #else |
---|
71 | #define SEMAPHORE_MP_OBJECT_WAS_DELETED NULL |
---|
72 | #endif |
---|
73 | |
---|
74 | rtems_status_code rtems_semaphore_delete( |
---|
75 | rtems_id id |
---|
76 | ) |
---|
77 | { |
---|
78 | register Semaphore_Control *the_semaphore; |
---|
79 | Objects_Locations location; |
---|
80 | |
---|
81 | the_semaphore = _Semaphore_Get( id, &location ); |
---|
82 | switch ( location ) { |
---|
83 | |
---|
84 | case OBJECTS_LOCAL: |
---|
85 | if ( !_Attributes_Is_counting_semaphore(the_semaphore->attribute_set) ) { |
---|
86 | if ( _CORE_mutex_Is_locked( &the_semaphore->Core_control.mutex ) && |
---|
87 | !_Attributes_Is_simple_binary_semaphore( |
---|
88 | the_semaphore->attribute_set ) ) { |
---|
89 | _Thread_Enable_dispatch(); |
---|
90 | return RTEMS_RESOURCE_IN_USE; |
---|
91 | } |
---|
92 | _CORE_mutex_Flush( |
---|
93 | &the_semaphore->Core_control.mutex, |
---|
94 | SEMAPHORE_MP_OBJECT_WAS_DELETED, |
---|
95 | CORE_MUTEX_WAS_DELETED |
---|
96 | ); |
---|
97 | } else { |
---|
98 | _CORE_semaphore_Flush( |
---|
99 | &the_semaphore->Core_control.semaphore, |
---|
100 | SEMAPHORE_MP_OBJECT_WAS_DELETED, |
---|
101 | CORE_SEMAPHORE_WAS_DELETED |
---|
102 | ); |
---|
103 | } |
---|
104 | |
---|
105 | _Objects_Close( &_Semaphore_Information, &the_semaphore->Object ); |
---|
106 | |
---|
107 | _Semaphore_Free( the_semaphore ); |
---|
108 | |
---|
109 | #if defined(RTEMS_MULTIPROCESSING) |
---|
110 | if ( _Attributes_Is_global( the_semaphore->attribute_set ) ) { |
---|
111 | |
---|
112 | _Objects_MP_Close( &_Semaphore_Information, the_semaphore->Object.id ); |
---|
113 | |
---|
114 | _Semaphore_MP_Send_process_packet( |
---|
115 | SEMAPHORE_MP_ANNOUNCE_DELETE, |
---|
116 | the_semaphore->Object.id, |
---|
117 | 0, /* Not used */ |
---|
118 | 0 /* Not used */ |
---|
119 | ); |
---|
120 | } |
---|
121 | #endif |
---|
122 | _Thread_Enable_dispatch(); |
---|
123 | return RTEMS_SUCCESSFUL; |
---|
124 | |
---|
125 | #if defined(RTEMS_MULTIPROCESSING) |
---|
126 | case OBJECTS_REMOTE: |
---|
127 | _Thread_Dispatch(); |
---|
128 | return RTEMS_ILLEGAL_ON_REMOTE_OBJECT; |
---|
129 | #endif |
---|
130 | |
---|
131 | case OBJECTS_ERROR: |
---|
132 | break; |
---|
133 | } |
---|
134 | |
---|
135 | return RTEMS_INVALID_ID; |
---|
136 | } |
---|