[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / ACE_wrappers / ace / SV_Semaphore_Simple.cpp
blob97395055b38a641aa18f61fd333ab1e57145c4be
1 #include "ace/SV_Semaphore_Simple.h"
2 #include "ace/Log_Msg.h"
3 #include "ace/ACE.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__ */
10 ACE_RCSID (ace,
11 SV_Semaphore_Simple,
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)
18 void
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 */
26 int
27 ACE_SV_Semaphore_Simple::control (int cmd,
28 int value,
29 u_short semnum) const
31 ACE_TRACE ("ACE_SV_Semaphore_Simple::control");
32 if (this->internal_id_ == -1)
33 return -1;
34 else
36 semun semctl_arg;
38 semctl_arg.val = value;
39 return ACE_OS::semctl (this->internal_id_,
40 semnum,
41 cmd,
42 semctl_arg);
46 int
47 ACE_SV_Semaphore_Simple::init (key_t k, int i)
49 ACE_TRACE ("ACE_SV_Semaphore_Simple::init");
50 this->key_ = k;
51 this->internal_id_ = i;
52 return 0;
55 // General ACE_SV_Semaphore operation. Increment or decrement by a
56 // specific amount (positive or negative; amount can`t be zero).
58 int
59 ACE_SV_Semaphore_Simple::op (short val, u_short n, short flags) const
61 ACE_TRACE ("ACE_SV_Semaphore_Simple::op");
62 sembuf op_op;
64 op_op.sem_num = n;
65 op_op.sem_flg = flags;
67 if (this->internal_id_ == -1)
68 return -1;
69 else if ((op_op.sem_op = val) == 0)
70 return -1;
71 else
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
76 // OK, else -1.
78 int
79 ACE_SV_Semaphore_Simple::open (key_t k,
80 short flags,
81 int initial_value,
82 u_short n,
83 mode_t perms)
85 ACE_TRACE ("ACE_SV_Semaphore_Simple::open");
86 union semun ivalue;
88 if (k == IPC_PRIVATE || k == static_cast<key_t> (ACE_INVALID_SEM_KEY))
89 return -1;
91 ivalue.val = initial_value;
92 this->key_ = k;
93 this->sem_number_ = n;
95 this->internal_id_ = ACE_OS::semget (this->key_, n, perms | flags);
97 if (this->internal_id_ == -1)
98 return -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)
103 return -1;
105 return 0;
108 ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple (key_t k,
109 short flags,
110 int initial_value,
111 u_short n,
112 mode_t perms)
113 : key_ (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.
127 key_t
128 ACE_SV_Semaphore_Simple::name_2_key (const char *name)
130 ACE_TRACE ("ACE_SV_Semaphore_Simple::name_2_key");
132 if (name == 0)
134 errno = EINVAL;
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
153 // 0.
156 ACE_SV_Semaphore_Simple::open (const char *name,
157 short flags,
158 int initial_value,
159 u_short n,
160 mode_t perms)
162 ACE_TRACE ("ACE_SV_Semaphore_Simple::open");
164 key_t key;
166 if (name == 0)
167 key = ACE_DEFAULT_SEM_KEY;
168 else
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,
175 short flags,
176 int initial_value,
177 u_short n,
178 mode_t perms)
180 ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
181 if (this->open (name,
182 flags,
183 initial_value,
185 perms) == -1)
186 ACE_ERROR ((LM_ERROR,
187 ACE_TEXT ("%p\n"),
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,
193 short flags,
194 int initial_value,
195 u_short nsems,
196 mode_t perms)
198 ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple(wchar_t)");
199 if (this->open (ACE_Wide_To_Ascii (name).char_rep (),
200 flags,
201 initial_value,
202 nsems,
203 perms) == -1)
204 ACE_ERROR ((LM_ERROR,
205 ACE_TEXT ("%p\n"),
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");
213 this->close ();
216 ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple (void)
218 ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
219 this->init ();
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 ();
234 return result;
237 ACE_END_VERSIONED_NAMESPACE_DECL