ACE_SV_Semaphore_Simple Class Reference

This is a simple semaphore package that assumes there are no race conditions for initialization (i.e., the order of process startup must be well defined). More...

#include <SV_Semaphore_Simple.h>

Inheritance diagram for ACE_SV_Semaphore_Simple:

Inheritance graph
[legend]
List of all members.

Public Types

enum  { ACE_CREATE = IPC_CREAT, ACE_EXCL = IPC_EXCL, ACE_OPEN = 0 }

Public Member Functions

 ACE_SV_Semaphore_Simple (void)
 ACE_SV_Semaphore_Simple (key_t key, short flags=ACE_SV_Semaphore_Simple::ACE_CREATE, int initial_value=1, u_short nsems=1, mode_t perms=ACE_DEFAULT_FILE_PERMS)
 ACE_SV_Semaphore_Simple (const char *name, short flags=ACE_SV_Semaphore_Simple::ACE_CREATE, int initial_value=1, u_short nsems=1, mode_t perms=ACE_DEFAULT_FILE_PERMS)
 ACE_SV_Semaphore_Simple (const wchar_t *name, short flags=ACE_SV_Semaphore_Simple::ACE_CREATE, int initial_value=1, u_short nsems=1, mode_t perms=ACE_DEFAULT_FILE_PERMS)
 ~ACE_SV_Semaphore_Simple (void)
int open (const char *name, short flags=ACE_SV_Semaphore_Simple::ACE_CREATE, int initial_value=1, u_short nsems=1, mode_t perms=ACE_DEFAULT_FILE_PERMS)
int open (const wchar_t *name, short flags=ACE_SV_Semaphore_Simple::ACE_CREATE, int initial_value=1, u_short nsems=1, mode_t perms=ACE_DEFAULT_FILE_PERMS)
int open (key_t key, short flags=ACE_SV_Semaphore_Simple::ACE_CREATE, int initial_value=1, u_short nsems=1, mode_t perms=ACE_DEFAULT_FILE_PERMS)
int close (void)
int remove (void) const
int acquire (u_short n=0, short flags=0) const
int acquire_read (u_short n=0, short flags=0) const
 Acquire a semaphore for reading.

int acquire_write (u_short n=0, short flags=0) const
 Acquire a semaphore for writing.

int tryacquire (u_short n=0, short flags=0) const
 Non-blocking version of .

int tryacquire_read (u_short n=0, short flags=0) const
 Try to acquire the semaphore for reading.

int tryacquire_write (u_short n=0, short flags=0) const
 Try to acquire the semaphore for writing.

int release (u_short n=0, short flags=0) const
int op (short val, u_short semnum=0, short flags=SEM_UNDO) const
int op (sembuf op_vec[], u_short nsems) const
 General ACE_SV_Semaphore operation on an array of SV_Semaphores.

int control (int cmd, semun arg, u_short n=0) const
int control (int cmd, int value=0, u_short n=0) const
int get_id (void) const
 Get underlying internal id.

void dump (void) const
 Dump the state of an object.


Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.


Protected Member Functions

int init (key_t k=static_cast< key_t >(ACE_INVALID_SEM_KEY), int i=-1)
key_t name_2_key (const char *name)

Protected Attributes

key_t key_
 Semaphore key.

int internal_id_
 Internal ID to identify the semaphore group within this process.

int sem_number_
 Number of semaphores we're creating.


Detailed Description

This is a simple semaphore package that assumes there are no race conditions for initialization (i.e., the order of process startup must be well defined).

Definition at line 50 of file SV_Semaphore_Simple.h.


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
ACE_CREATE 
ACE_EXCL 
ACE_OPEN 

Definition at line 53 of file SV_Semaphore_Simple.h.

00054   {
00055     ACE_CREATE = IPC_CREAT,
00056     ACE_EXCL = IPC_EXCL,
00057     ACE_OPEN = 0
00058   };


Constructor & Destructor Documentation

ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple void   ) 
 

Definition at line 216 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE, and init().

00217 {
00218   ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
00219   this->init ();
00220 }

ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple key_t  key,
short  flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
int  initial_value = 1,
u_short  nsems = 1,
mode_t  perms = ACE_DEFAULT_FILE_PERMS
 

Definition at line 108 of file SV_Semaphore_Simple.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TRACE, LM_ERROR, mode_t, and open().

00113   : key_ (k)
00114 {
00115   ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
00116   if (this->open (k, flags, initial_value, n, perms) == -1)
00117     ACE_ERROR ((LM_ERROR,  ACE_LIB_TEXT ("%p\n"),  ACE_LIB_TEXT ("ACE_SV_Semaphore::ACE_SV_Semaphore")));
00118 }

ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple const char *  name,
short  flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
int  initial_value = 1,
u_short  nsems = 1,
mode_t  perms = ACE_DEFAULT_FILE_PERMS
 

Definition at line 174 of file SV_Semaphore_Simple.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TRACE, LM_ERROR, mode_t, and open().

00179 {
00180   ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
00181   if (this->open (name,
00182                   flags,
00183                   initial_value,
00184                   n,
00185                   perms) == -1)
00186     ACE_ERROR ((LM_ERROR,
00187                 ACE_LIB_TEXT ("%p\n"),
00188                 ACE_LIB_TEXT ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple")));
00189 }

ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple const wchar_t *  name,
short  flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
int  initial_value = 1,
u_short  nsems = 1,
mode_t  perms = ACE_DEFAULT_FILE_PERMS
 

Definition at line 192 of file SV_Semaphore_Simple.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TRACE, LM_ERROR, mode_t, and open().

00197 {
00198   ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple(wchar_t)");
00199   if (this->open (ACE_Wide_To_Ascii (name).char_rep (),
00200                   flags,
00201                   initial_value,
00202                   nsems,
00203                   perms) == -1)
00204     ACE_ERROR ((LM_ERROR,
00205                 ACE_LIB_TEXT ("%p\n"),
00206                 ACE_LIB_TEXT ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple")));
00207 }

ACE_SV_Semaphore_Simple::~ACE_SV_Semaphore_Simple void   ) 
 

Definition at line 210 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE, and close().

00211 {
00212   ACE_TRACE ("ACE_SV_Semaphore_Simple::~ACE_SV_Semaphore_Simple");
00213   this->close ();
00214 }


Member Function Documentation

ACE_INLINE int ACE_SV_Semaphore_Simple::acquire u_short  n = 0,
short  flags = 0
const
 

Wait until a ACE_SV_Semaphore's value is greater than 0, the decrement it by 1 and return. Dijkstra's P operation, Tannenbaums DOWN operation.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 64 of file SV_Semaphore_Simple.inl.

References ACE_TRACE, and op().

Referenced by ACE_SV_Semaphore_Complex::acquire(), acquire_read(), and acquire_write().

00065 {
00066   ACE_TRACE ("ACE_SV_Semaphore_Simple::acquire");
00067   return this->op (-1, n, flags);
00068 }

ACE_INLINE int ACE_SV_Semaphore_Simple::acquire_read u_short  n = 0,
short  flags = 0
const
 

Acquire a semaphore for reading.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 71 of file SV_Semaphore_Simple.inl.

References ACE_TRACE, and acquire().

00072 {
00073   ACE_TRACE ("ACE_SV_Semaphore_Simple::acquire_read");
00074   return this->acquire (n, flags);
00075 }

ACE_INLINE int ACE_SV_Semaphore_Simple::acquire_write u_short  n = 0,
short  flags = 0
const
 

Acquire a semaphore for writing.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 78 of file SV_Semaphore_Simple.inl.

References ACE_TRACE, and acquire().

00079 {
00080   ACE_TRACE ("ACE_SV_Semaphore_Simple::acquire_write");
00081   return this->acquire (n, flags);
00082 }

ACE_INLINE int ACE_SV_Semaphore_Simple::close void   ) 
 

Close a ACE_SV_Semaphore, marking it as invalid for subsequent operations...

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 43 of file SV_Semaphore_Simple.inl.

References ACE_TRACE, and init().

Referenced by ~ACE_SV_Semaphore_Simple().

00044 {
00045   ACE_TRACE ("ACE_SV_Semaphore_Simple::close");
00046   return this->init ();
00047 }

int ACE_SV_Semaphore_Simple::control int  cmd,
int  value = 0,
u_short  n = 0
const
 

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 27 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE, ACE_OS::semctl(), and semun::val.

00030 {
00031   ACE_TRACE ("ACE_SV_Semaphore_Simple::control");
00032   if (this->internal_id_ == -1)
00033     return -1;
00034   else
00035     {
00036       semun semctl_arg;
00037 
00038       semctl_arg.val = value;
00039       return ACE_OS::semctl (this->internal_id_,
00040                              semnum,
00041                              cmd,
00042                              semctl_arg);
00043     }
00044 }

ACE_INLINE int ACE_SV_Semaphore_Simple::control int  cmd,
semun  arg,
u_short  n = 0
const
 

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 30 of file SV_Semaphore_Simple.inl.

References ACE_TRACE, and ACE_OS::semctl().

Referenced by ACE_SV_Semaphore_Complex::close(), ACE_SV_Semaphore_Complex::control(), ACE_SV_Semaphore_Complex::open(), and remove().

00033 {
00034   ACE_TRACE ("ACE_SV_Semaphore_Simple::control");
00035   return this->internal_id_ == -1 ?
00036     -1 : ACE_OS::semctl (this->internal_id_, n, cmd, arg);
00037 }

ACE_BEGIN_VERSIONED_NAMESPACE_DECL void ACE_SV_Semaphore_Simple::dump void   )  const
 

Dump the state of an object.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 19 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE.

00020 {
00021 #if defined (ACE_HAS_DUMP)
00022   ACE_TRACE ("ACE_SV_Semaphore_Simple::dump");
00023 #endif /* ACE_HAS_DUMP */
00024 }

ACE_INLINE int ACE_SV_Semaphore_Simple::get_id void   )  const
 

Get underlying internal id.

Definition at line 122 of file SV_Semaphore_Simple.inl.

References ACE_TRACE.

00123 {
00124   ACE_TRACE ("ACE_SV_Semaphore_Simple::get_id");
00125   return this->internal_id_;
00126 }

int ACE_SV_Semaphore_Simple::init key_t  k = static_cast< key_t >(ACE_INVALID_SEM_KEY),
int  i = -1
[protected]
 

Convert name to key This function is used internally to create keys for the semaphores. A valid name contains letters and digits only and MUST start with a letter.

The method for generating names is not very sophisticated, so caller should not pass strings which match each other for the first LUSED characters when he wants to get a different key.

Definition at line 47 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE.

Referenced by ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex(), ACE_SV_Semaphore_Simple(), close(), ACE_SV_Semaphore_Complex::close(), and ACE_SV_Semaphore_Complex::open().

00048 {
00049   ACE_TRACE ("ACE_SV_Semaphore_Simple::init");
00050   this->key_ = k;
00051   this->internal_id_ = i;
00052   return 0;
00053 }

key_t ACE_SV_Semaphore_Simple::name_2_key const char *  name  )  [protected]
 

Definition at line 128 of file SV_Semaphore_Simple.cpp.

References ACE_INVALID_SEM_KEY, ACE_TRACE, and ACE::crc32().

Referenced by ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex(), and open().

00129 {
00130   ACE_TRACE ("ACE_SV_Semaphore_Simple::name_2_key");
00131 
00132   if (name == 0)
00133     {
00134       errno = EINVAL;
00135       return static_cast<key_t> (ACE_INVALID_SEM_KEY);
00136     }
00137 
00138   // Basically "hash" the values in the <name>.  This won't
00139   // necessarily guarantee uniqueness of all keys.
00140   // But (IMHO) CRC32 is good enough for most purposes (Carlos)
00141 #if defined (ACE_WIN64)
00142   // The cast below is legit...
00143 #  pragma warning(push)
00144 #  pragma warning(disable : 4312)
00145 #endif /* ACE_WIN64 */
00146   return (key_t) ACE::crc32 (name);
00147 #if defined (ACE_WIN64)
00148 #  pragma warning(pop)
00149 #endif /* ACE_WIN64 */
00150 }

ACE_INLINE int ACE_SV_Semaphore_Simple::op sembuf  op_vec[],
u_short  nsems
const
 

General ACE_SV_Semaphore operation on an array of SV_Semaphores.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 52 of file SV_Semaphore_Simple.inl.

References ACE_TRACE, and ACE_OS::semop().

00053 {
00054   ACE_TRACE ("ACE_SV_Semaphore_Simple::op");
00055   return this->internal_id_ == -1
00056     ? -1 : ACE_OS::semop (this->internal_id_, op_vec, n);
00057 }

int ACE_SV_Semaphore_Simple::op short  val,
u_short  semnum = 0,
short  flags = SEM_UNDO
const
 

General ACE_SV_Semaphore operation. Increment or decrement by a specific amount (positive or negative; amount can`t be zero).

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 59 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE, and ACE_OS::semop().

Referenced by acquire(), ACE_SV_Semaphore_Complex::op(), release(), and tryacquire().

00060 {
00061   ACE_TRACE ("ACE_SV_Semaphore_Simple::op");
00062   sembuf op_op;
00063 
00064   op_op.sem_num = n;
00065   op_op.sem_flg = flags;
00066 
00067   if (this->internal_id_ == -1)
00068     return -1;
00069   else if ((op_op.sem_op = val) == 0)
00070     return -1;
00071   else
00072     return ACE_OS::semop (this->internal_id_, &op_op, 1);
00073 }

int ACE_SV_Semaphore_Simple::open key_t  key,
short  flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
int  initial_value = 1,
u_short  nsems = 1,
mode_t  perms = ACE_DEFAULT_FILE_PERMS
 

Open or create one or more SV_Semaphores. We return 0 if all is OK, else -1.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 79 of file SV_Semaphore_Simple.cpp.

References ACE_BIT_ENABLED, ACE_INVALID_SEM_KEY, ACE_TRACE, IPC_CREAT, IPC_PRIVATE, mode_t, sem_number_, ACE_OS::semctl(), ACE_OS::semget(), SETVAL, and semun::val.

00084 {
00085   ACE_TRACE ("ACE_SV_Semaphore_Simple::open");
00086   union semun ivalue;
00087 
00088   if (k == IPC_PRIVATE || k == static_cast<key_t> (ACE_INVALID_SEM_KEY))
00089     return -1;
00090 
00091   ivalue.val = initial_value;
00092   this->key_ = k;
00093   this->sem_number_ = n;
00094 
00095   this->internal_id_ = ACE_OS::semget (this->key_, n, perms | flags);
00096 
00097   if (this->internal_id_ == -1)
00098     return -1;
00099 
00100   if (ACE_BIT_ENABLED (flags, IPC_CREAT))
00101     for (int i = 0; i < n; i++)
00102       if (ACE_OS::semctl (this->internal_id_, i, SETVAL, ivalue) == -1)
00103         return -1;
00104 
00105   return 0;
00106 }

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_INLINE int ACE_SV_Semaphore_Simple::open const wchar_t *  name,
short  flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
int  initial_value = 1,
u_short  nsems = 1,
mode_t  perms = ACE_DEFAULT_FILE_PERMS
 

Definition at line 14 of file SV_Semaphore_Simple.inl.

References ACE_TRACE, mode_t, and open().

00019 {
00020   ACE_TRACE ("ACE_SV_Semaphore_Simple::open (wchar_t)");
00021   return this->open (ACE_Wide_To_Ascii (name).char_rep (),
00022                      flags,
00023                      initial_value,
00024                      nsems,
00025                      perms);
00026 }

int ACE_SV_Semaphore_Simple::open const char *  name,
short  flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
int  initial_value = 1,
u_short  nsems = 1,
mode_t  perms = ACE_DEFAULT_FILE_PERMS
 

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 156 of file SV_Semaphore_Simple.cpp.

References ACE_DEFAULT_SEM_KEY, ACE_TRACE, mode_t, and name_2_key().

Referenced by ACE_SV_Semaphore_Simple(), and open().

00161 {
00162   ACE_TRACE ("ACE_SV_Semaphore_Simple::open");
00163 
00164   key_t key;
00165 
00166   if (name == 0)
00167     key = ACE_DEFAULT_SEM_KEY;
00168   else
00169     key = this->name_2_key (name);
00170 
00171   return this->open (key, flags, initial_value, n, perms);
00172 }

ACE_INLINE int ACE_SV_Semaphore_Simple::release u_short  n = 0,
short  flags = 0
const
 

Increment ACE_SV_Semaphore by one. Dijkstra's V operation, Tannenbaums UP operation.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 115 of file SV_Semaphore_Simple.inl.

References ACE_TRACE, and op().

Referenced by ACE_SV_Semaphore_Complex::release().

00116 {
00117   ACE_TRACE ("ACE_SV_Semaphore_Simple::release");
00118   return this->op (1, n, flags);
00119 }

int ACE_SV_Semaphore_Simple::remove void   )  const
 

Remove all SV_Semaphores associated with a particular key. This call is intended to be called from a server, for example, when it is being shut down, as we do an IPC_RMID on the ACE_SV_Semaphore, regardless of whether other processes may be using it or not. Most other processes should use below.

Definition at line 229 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE, control(), and IPC_RMID.

Referenced by ACE_SV_Semaphore_Complex::close().

00230 {
00231   ACE_TRACE ("ACE_SV_Semaphore_Simple::remove");
00232   int result = this->control (IPC_RMID);
00233   ((ACE_SV_Semaphore_Simple *) this)->init ();
00234   return result;
00235 }

ACE_INLINE int ACE_SV_Semaphore_Simple::tryacquire u_short  n = 0,
short  flags = 0
const
 

Non-blocking version of .

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 87 of file SV_Semaphore_Simple.inl.

References ACE_TRACE, IPC_NOWAIT, and op().

Referenced by ACE_SV_Semaphore_Complex::tryacquire(), tryacquire_read(), and tryacquire_write().

00088 {
00089   ACE_TRACE ("ACE_SV_Semaphore_Simple::tryacquire");
00090   return this->op (-1, n, flags | IPC_NOWAIT);
00091 }

ACE_INLINE int ACE_SV_Semaphore_Simple::tryacquire_read u_short  n = 0,
short  flags = 0
const
 

Try to acquire the semaphore for reading.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 96 of file SV_Semaphore_Simple.inl.

References ACE_TRACE, and tryacquire().

00097 {
00098   ACE_TRACE ("ACE_SV_Semaphore_Simple::tryacquire_read");
00099   return this->tryacquire (n, flags);
00100 }

ACE_INLINE int ACE_SV_Semaphore_Simple::tryacquire_write u_short  n = 0,
short  flags = 0
const
 

Try to acquire the semaphore for writing.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 105 of file SV_Semaphore_Simple.inl.

References ACE_TRACE, and tryacquire().

00106 {
00107   ACE_TRACE ("ACE_SV_Semaphore_Simple::tryacquire_write");
00108   return this->tryacquire (n, flags);
00109 }


Member Data Documentation

ACE_SV_Semaphore_Simple::ACE_ALLOC_HOOK_DECLARE
 

Declare the dynamic allocation hooks.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 163 of file SV_Semaphore_Simple.h.

int ACE_SV_Semaphore_Simple::internal_id_ [protected]
 

Internal ID to identify the semaphore group within this process.

Definition at line 170 of file SV_Semaphore_Simple.h.

key_t ACE_SV_Semaphore_Simple::key_ [protected]
 

Semaphore key.

Definition at line 167 of file SV_Semaphore_Simple.h.

int ACE_SV_Semaphore_Simple::sem_number_ [protected]
 

Number of semaphores we're creating.

Definition at line 173 of file SV_Semaphore_Simple.h.

Referenced by open().


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:30:35 2006 for ACE by doxygen 1.3.6