Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / ace / SV_Semaphore_Simple.h
blob600f29488a99d898f94fe76a8a7ecea5e9c9989e
1 // -*- C++ -*-
3 //==========================================================================
4 /**
5 * @file SV_Semaphore_Simple.h
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
8 */
9 //==========================================================================
11 #ifndef ACE_SV_SEMAPHORE_SIMPLE_H
12 #define ACE_SV_SEMAPHORE_SIMPLE_H
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/ACE_export.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 # pragma once
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 #include "ace/os_include/sys/os_stat.h"
23 #include "ace/os_include/sys/os_ipc.h"
24 #include "ace/os_include/sys/os_sem.h"
25 #include "ace/Default_Constants.h"
27 #if defined (ACE_WIN32)
28 // Default semaphore key and mutex name
29 # if !defined (ACE_DEFAULT_SEM_KEY)
30 # define ACE_DEFAULT_SEM_KEY const_cast <char*>("ACE_SEM_KEY")
31 # endif /* ACE_DEFAULT_SEM_KEY */
32 #else /* !defined (ACE_WIN32) */
33 // Default semaphore key
34 # if !defined (ACE_DEFAULT_SEM_KEY)
35 # define ACE_DEFAULT_SEM_KEY 1234
36 # endif /* ACE_DEFAULT_SEM_KEY */
37 #endif /* ACE_WIN32 */
39 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
41 /**
42 * @class ACE_SV_Semaphore_Simple
44 * @brief This is a simple semaphore package that assumes there are
45 * no race conditions for initialization (i.e., the order of
46 * process startup must be well defined).
48 class ACE_Export ACE_SV_Semaphore_Simple
50 public:
51 enum
53 ACE_CREATE = IPC_CREAT,
54 ACE_EXCL = IPC_EXCL,
55 ACE_OPEN = 0
58 ACE_SV_Semaphore_Simple ();
59 ACE_SV_Semaphore_Simple (key_t key,
60 short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
61 int initial_value = 1,
62 u_short nsems = 1,
63 mode_t perms = ACE_DEFAULT_FILE_PERMS);
64 ACE_SV_Semaphore_Simple (const char *name,
65 short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
66 int initial_value = 1,
67 u_short nsems = 1,
68 mode_t perms = ACE_DEFAULT_FILE_PERMS);
69 #if defined (ACE_HAS_WCHAR)
70 ACE_SV_Semaphore_Simple (const wchar_t *name,
71 short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
72 int initial_value = 1,
73 u_short nsems = 1,
74 mode_t perms = ACE_DEFAULT_FILE_PERMS);
75 #endif /* ACE_HAS_WCHAR */
77 ~ACE_SV_Semaphore_Simple ();
79 int open (const char *name,
80 short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
81 int initial_value = 1,
82 u_short nsems = 1,
83 mode_t perms = ACE_DEFAULT_FILE_PERMS);
85 #if defined (ACE_HAS_WCHAR)
86 int open (const wchar_t *name,
87 short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
88 int initial_value = 1,
89 u_short nsems = 1,
90 mode_t perms = ACE_DEFAULT_FILE_PERMS);
91 #endif /* ACE_HAS_WCHAR */
93 /// Open or create one or more SV_Semaphores. We return 0 if all is
94 /// OK, else -1.
95 int open (key_t key,
96 short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
97 int initial_value = 1,
98 u_short nsems = 1,
99 mode_t perms = ACE_DEFAULT_FILE_PERMS);
101 /// Close a ACE_SV_Semaphore, marking it as invalid for subsequent
102 /// operations...
103 int close ();
106 * Remove all SV_Semaphores associated with a particular key. This
107 * call is intended to be called from a server, for example, when it
108 * is being shut down, as we do an IPC_RMID on the ACE_SV_Semaphore,
109 * regardless of whether other processes may be using it or not.
110 * Most other processes should use close() below.
112 int remove () const;
114 // = Semaphore acquire and release methods.
116 * Wait until a ACE_SV_Semaphore's value is greater than 0, the
117 * decrement it by 1 and return. Dijkstra's P operation, Tannenbaums
118 * DOWN operation.
120 int acquire (u_short n = 0, short flags = 0) const;
122 /// Acquire a semaphore for reading.
123 int acquire_read (u_short n = 0, short flags = 0) const;
125 /// Acquire a semaphore for writing
126 int acquire_write (u_short n = 0, short flags = 0) const;
128 /// Non-blocking version of <acquire>.
129 int tryacquire (u_short n = 0, short flags = 0) const;
131 /// Try to acquire the semaphore for reading.
132 int tryacquire_read (u_short n = 0, short flags = 0) const;
134 /// Try to acquire the semaphore for writing.
135 int tryacquire_write (u_short n = 0, short flags = 0) const;
137 /// Increment ACE_SV_Semaphore by one. Dijkstra's V operation,
138 /// Tannenbaums UP operation.
139 int release (u_short n = 0, short flags = 0) const;
141 // = Semaphore operation methods.
142 /// General ACE_SV_Semaphore operation. Increment or decrement by a
143 /// specific amount (positive or negative; amount can`t be zero).
144 int op (short val, u_short semnum = 0, short flags = SEM_UNDO) const;
146 /// General ACE_SV_Semaphore operation on an array of SV_Semaphores.
147 int op (sembuf op_vec[], u_short nsems) const;
149 // = Semaphore control methods.
150 int control (int cmd, semun arg, u_short n = 0) const;
151 int control (int cmd, int value = 0, u_short n = 0) const;
153 /// Get underlying internal id.
154 int get_id () const;
156 /// Dump the state of an object.
157 void dump () const;
159 /// Declare the dynamic allocation hooks.
160 ACE_ALLOC_HOOK_DECLARE;
162 protected:
163 /// Semaphore key.
164 key_t key_;
166 /// Internal ID to identify the semaphore group within this process.
167 int internal_id_;
169 /// Number of semaphores we're creating.
170 int sem_number_;
172 #ifdef ACE_HAS_SYSV_IPC
174 * Convert name to key This function is used internally to create
175 * keys for the semaphores. A valid name contains letters and
176 * digits only and MUST start with a letter.
178 * The method for generating names is not very sophisticated, so
179 * caller should not pass strings which match each other for the first
180 * LUSED characters when he wants to get a different key.
182 int init (key_t k = static_cast<key_t> (ACE_INVALID_SEM_KEY),
183 int i = -1);
184 #endif
186 key_t name_2_key (const char *name);
189 ACE_END_VERSIONED_NAMESPACE_DECL
191 #if defined (__ACE_INLINE__)
192 #include "ace/SV_Semaphore_Simple.inl"
193 #endif /* __ACE_INLINE__ */
195 #include /**/ "ace/post.h"
197 #endif /* _SV_SEMAPHORE_SIMPLE_H */