1 #include "ace/SV_Semaphore_Simple.h"
2 #include "ace/Log_Msg.h"
4 #include "ace/os_include/sys/os_sem.h"
6 #if !defined (__ACE_INLINE__)
7 #include "ace/SV_Semaphore_Simple.inl"
8 #endif /* !__ACE_INLINE__ */
12 "$Id: SV_Semaphore_Simple.cpp 82559 2008-08-07 20:23:07Z parsons $")
14 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
16 ACE_ALLOC_HOOK_DEFINE (ACE_SV_Semaphore_Simple
)
19 ACE_SV_Semaphore_Simple::dump (void) const
21 #if defined (ACE_HAS_DUMP)
22 ACE_TRACE ("ACE_SV_Semaphore_Simple::dump");
23 #endif /* ACE_HAS_DUMP */
27 ACE_SV_Semaphore_Simple::control (int cmd
,
31 ACE_TRACE ("ACE_SV_Semaphore_Simple::control");
32 if (this->internal_id_
== -1)
38 semctl_arg
.val
= value
;
39 return ACE_OS::semctl (this->internal_id_
,
47 ACE_SV_Semaphore_Simple::init (key_t k
, int i
)
49 ACE_TRACE ("ACE_SV_Semaphore_Simple::init");
51 this->internal_id_
= i
;
55 // General ACE_SV_Semaphore operation. Increment or decrement by a
56 // specific amount (positive or negative; amount can`t be zero).
59 ACE_SV_Semaphore_Simple::op (short val
, u_short n
, short flags
) const
61 ACE_TRACE ("ACE_SV_Semaphore_Simple::op");
65 op_op
.sem_flg
= flags
;
67 if (this->internal_id_
== -1)
69 else if ((op_op
.sem_op
= val
) == 0)
72 return ACE_OS::semop (this->internal_id_
, &op_op
, 1);
75 // Open or create one or more SV_Semaphores. We return 0 if all is
79 ACE_SV_Semaphore_Simple::open (key_t k
,
85 ACE_TRACE ("ACE_SV_Semaphore_Simple::open");
88 if (k
== IPC_PRIVATE
|| k
== static_cast<key_t
> (ACE_INVALID_SEM_KEY
))
91 ivalue
.val
= initial_value
;
93 this->sem_number_
= n
;
95 this->internal_id_
= ACE_OS::semget (this->key_
, n
, perms
| flags
);
97 if (this->internal_id_
== -1)
100 if (ACE_BIT_ENABLED (flags
, IPC_CREAT
))
101 for (int i
= 0; i
< n
; i
++)
102 if (ACE_OS::semctl (this->internal_id_
, i
, SETVAL
, ivalue
) == -1)
108 ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple (key_t k
,
115 ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
116 if (this->open (k
, flags
, initial_value
, n
, perms
) == -1)
117 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("ACE_SV_Semaphore::ACE_SV_Semaphore")));
120 // Convert name to key. This function is used internally to create keys
121 // for the semaphores.
123 // The method for generating names is a 32 bit CRC, but still we
124 // measured close to collition ratio of nearly 0.1% for
125 // ACE::unique_name()-like strings.
128 ACE_SV_Semaphore_Simple::name_2_key (const char *name
)
130 ACE_TRACE ("ACE_SV_Semaphore_Simple::name_2_key");
135 return static_cast<key_t
> (ACE_INVALID_SEM_KEY
);
138 // Basically "hash" the values in the <name>. This won't
139 // necessarily guarantee uniqueness of all keys.
140 // But (IMHO) CRC32 is good enough for most purposes (Carlos)
141 #if defined (ACE_WIN64) || defined (ACE_WIN32)
142 // The cast below is legit...
143 # pragma warning(push)
144 # pragma warning(disable : 4312)
145 #endif /* ACE_WIN64 */
146 return (key_t
) ACE::crc32 (name
);
147 #if defined (ACE_WIN64) || defined (ACE_WIN32)
148 # pragma warning(pop)
149 #endif /* ACE_WIN64 */
152 // Open or create a ACE_SV_Semaphore. We return 1 if all is OK, else
156 ACE_SV_Semaphore_Simple::open (const char *name
,
162 ACE_TRACE ("ACE_SV_Semaphore_Simple::open");
167 key
= ACE_DEFAULT_SEM_KEY
;
169 key
= this->name_2_key (name
);
171 return this->open (key
, flags
, initial_value
, n
, perms
);
174 ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple (const char *name
,
180 ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
181 if (this->open (name
,
186 ACE_ERROR ((LM_ERROR
,
188 ACE_TEXT ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple")));
191 #if defined (ACE_HAS_WCHAR)
192 ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple (const wchar_t *name
,
198 ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple(wchar_t)");
199 if (this->open (ACE_Wide_To_Ascii (name
).char_rep (),
204 ACE_ERROR ((LM_ERROR
,
206 ACE_TEXT ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple")));
208 #endif /* ACE_HAS_WCHAR */
210 ACE_SV_Semaphore_Simple::~ACE_SV_Semaphore_Simple (void)
212 ACE_TRACE ("ACE_SV_Semaphore_Simple::~ACE_SV_Semaphore_Simple");
216 ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple (void)
218 ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
222 // Remove all SV_Semaphores associated with a particular key. This
223 // call is intended to be called from a server, for example, when it
224 // is being shut down, as we do an IPC_RMID on the ACE_SV_Semaphore,
225 // regardless of whether other processes may be using it or not. Most
226 // other processes should use close() below.
229 ACE_SV_Semaphore_Simple::remove (void) const
231 ACE_TRACE ("ACE_SV_Semaphore_Simple::remove");
232 int const result
= this->control (IPC_RMID
);
233 ((ACE_SV_Semaphore_Simple
*) this)->init ();
237 ACE_END_VERSIONED_NAMESPACE_DECL