Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

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 Methods

 ACE_SV_Semaphore_Simple (void)
 ACE_SV_Semaphore_Simple (key_t key, int flags=ACE_SV_Semaphore_Simple::ACE_CREATE, int initial_value=1, u_short nsems=1, int perms=ACE_DEFAULT_FILE_PERMS)
 ACE_SV_Semaphore_Simple (const char *name, int flags=ACE_SV_Semaphore_Simple::ACE_CREATE, int initial_value=1, u_short nsems=1, int perms=ACE_DEFAULT_FILE_PERMS)
 ~ACE_SV_Semaphore_Simple (void)
int open (const char *name, int flags=ACE_SV_Semaphore_Simple::ACE_CREATE, int initial_value=1, u_short nsems=1, int perms=ACE_DEFAULT_FILE_PERMS)
int open (key_t key, int flags=ACE_SV_Semaphore_Simple::ACE_CREATE, int initial_value=1, u_short nsems=1, int perms=ACE_DEFAULT_FILE_PERMS)
 Open or create one or more SV_Semaphores. We return 0 if all is OK, else -1. More...

int close (void)
 Close a ACE_SV_Semaphore, marking it as invalid for subsequent operations... More...

int remove (void) const
int acquire (u_short n=0, int flags=0) const
int acquire_read (u_short n=0, int flags=0) const
 Acquire a semaphore for reading. More...

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

int tryacquire (u_short n=0, int flags=0) const
 Non-blocking version of <acquire>. More...

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

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

int release (u_short n=0, int flags=0) const
 Increment ACE_SV_Semaphore by one. Dijkstra's V operation, Tannenbaums UP operation. More...

int op (int val, u_short semnum=0, int flags=SEM_UNDO) const
 General ACE_SV_Semaphore operation. Increment or decrement by a specific amount (positive or negative; amount can`t be zero). More...

int op (sembuf op_vec[], u_short nsems) const
 General ACE_SV_Semaphore operation on an array of SV_Semaphores. More...

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. More...

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


Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks. More...


Protected Methods

int init (key_t k=ACE_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. More...

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

int sem_number_
 Number of semaphores we're creating. More...


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 33 of file SV_Semaphore_Simple.h.


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
ACE_CREATE 
ACE_EXCL 
ACE_OPEN 

Definition at line 36 of file SV_Semaphore_Simple.h.

00037   {
00038     ACE_CREATE = IPC_CREAT,
00039     ACE_EXCL = IPC_EXCL,
00040     ACE_OPEN = 0
00041   };


Constructor & Destructor Documentation

ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple void   
 

Definition at line 194 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE, and init.

00195 {
00196   ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
00197   this->init ();
00198 }

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

Definition at line 105 of file SV_Semaphore_Simple.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TRACE, LM_ERROR, and open.

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

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

Definition at line 171 of file SV_Semaphore_Simple.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TRACE, LM_ERROR, and open.

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

ACE_SV_Semaphore_Simple::~ACE_SV_Semaphore_Simple void   
 

Definition at line 188 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE, and close.

00189 {
00190   ACE_TRACE ("ACE_SV_Semaphore_Simple::~ACE_SV_Semaphore_Simple");
00191   this->close ();
00192 }


Member Function Documentation

ASYS_INLINE int ACE_SV_Semaphore_Simple::acquire u_short    n = 0,
int    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 43 of file SV_Semaphore_Simple.i.

References ACE_TRACE, and op.

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

00044 {
00045   ACE_TRACE ("ACE_SV_Semaphore_Simple::acquire");
00046   return this->op (-1, n, flags);
00047 }

ASYS_INLINE int ACE_SV_Semaphore_Simple::acquire_read u_short    n = 0,
int    flags = 0
const
 

Acquire a semaphore for reading.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 50 of file SV_Semaphore_Simple.i.

References ACE_TRACE, and acquire.

00051 {
00052   ACE_TRACE ("ACE_SV_Semaphore_Simple::acquire_read");
00053   return this->acquire (n, flags);
00054 }

ASYS_INLINE int ACE_SV_Semaphore_Simple::acquire_write u_short    n = 0,
int    flags = 0
const
 

Acquire a semaphore for writing.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 57 of file SV_Semaphore_Simple.i.

References ACE_TRACE, and acquire.

00058 {
00059   ACE_TRACE ("ACE_SV_Semaphore_Simple::acquire_write");
00060   return this->acquire (n, flags);
00061 }

ASYS_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 22 of file SV_Semaphore_Simple.i.

References ACE_TRACE, and init.

Referenced by ~ACE_SV_Semaphore_Simple.

00023 {
00024   ACE_TRACE ("ACE_SV_Semaphore_Simple::close");
00025   return this->init ();
00026 }

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 24 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE, internal_id_, ACE_OS::semctl, and semun::val.

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

ASYS_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 9 of file SV_Semaphore_Simple.i.

References ACE_TRACE, internal_id_, and ACE_OS::semctl.

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

00012 {
00013   ACE_TRACE ("ACE_SV_Semaphore_Simple::control");
00014   return this->internal_id_ == -1 ? 
00015     -1 : ACE_OS::semctl (this->internal_id_, n, cmd, arg);
00016 }

void ACE_SV_Semaphore_Simple::dump void    const
 

Dump the state of an object.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 18 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE.

00019 {
00020   ACE_TRACE ("ACE_SV_Semaphore_Simple::dump");
00021 }

ASYS_INLINE int ACE_SV_Semaphore_Simple::get_id void    const
 

Get underlying internal id.

Definition at line 101 of file SV_Semaphore_Simple.i.

References ACE_TRACE, and internal_id_.

00102 {
00103   ACE_TRACE ("ACE_SV_Semaphore_Simple::get_id");
00104   return this->internal_id_;
00105 }

int ACE_SV_Semaphore_Simple::init key_t    k = ACE_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 44 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE, internal_id_, and key_.

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.

00045 {
00046   ACE_TRACE ("ACE_SV_Semaphore_Simple::init");
00047   this->key_ = k;
00048   this->internal_id_ = i;
00049   return 0;
00050 }

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

Definition at line 125 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, open, and ACE_SV_Semaphore_Complex::open.

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

ASYS_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 31 of file SV_Semaphore_Simple.i.

References ACE_TRACE, internal_id_, and ACE_OS::semop.

00032 {
00033   ACE_TRACE ("ACE_SV_Semaphore_Simple::op");
00034   return this->internal_id_ == -1 
00035     ? -1 : ACE_OS::semop (this->internal_id_, op_vec, n);
00036 }

int ACE_SV_Semaphore_Simple::op int    val,
u_short    semnum = 0,
int    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 56 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE, internal_id_, and ACE_OS::semop.

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

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

int ACE_SV_Semaphore_Simple::open key_t    key,
int    flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
int    initial_value = 1,
u_short    nsems = 1,
int    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 76 of file SV_Semaphore_Simple.cpp.

References ACE_BIT_ENABLED, ACE_INVALID_SEM_KEY, ACE_TRACE, internal_id_, IPC_CREAT, IPC_PRIVATE, key_, sem_number_, ACE_OS::semctl, ACE_OS::semget, SETVAL, and semun::val.

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

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

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 153 of file SV_Semaphore_Simple.cpp.

References ACE_DEFAULT_SEM_KEY, ACE_TRACE, and name_2_key.

Referenced by ACE_SV_Semaphore_Simple.

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

ASYS_INLINE int ACE_SV_Semaphore_Simple::release u_short    n = 0,
int    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 94 of file SV_Semaphore_Simple.i.

References ACE_TRACE, and op.

Referenced by ACE_SV_Semaphore_Complex::release.

00095 {
00096   ACE_TRACE ("ACE_SV_Semaphore_Simple::release");
00097   return this->op (1, n, flags);
00098 }

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 <close> below.

Definition at line 207 of file SV_Semaphore_Simple.cpp.

References ACE_TRACE, control, and IPC_RMID.

Referenced by ACE_SV_Semaphore_Complex::close.

00208 {
00209   ACE_TRACE ("ACE_SV_Semaphore_Simple::remove");
00210   int result = this->control (IPC_RMID);
00211   ((ACE_SV_Semaphore_Simple *) this)->init ();
00212   return result;
00213 }

ASYS_INLINE int ACE_SV_Semaphore_Simple::tryacquire u_short    n = 0,
int    flags = 0
const
 

Non-blocking version of <acquire>.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 66 of file SV_Semaphore_Simple.i.

References ACE_TRACE, IPC_NOWAIT, and op.

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

00067 {
00068   ACE_TRACE ("ACE_SV_Semaphore_Simple::tryacquire");
00069   return this->op (-1, n, flags | IPC_NOWAIT);
00070 }

ASYS_INLINE int ACE_SV_Semaphore_Simple::tryacquire_read u_short    n = 0,
int    flags = 0
const
 

Try to acquire the semaphore for reading.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 75 of file SV_Semaphore_Simple.i.

References ACE_TRACE, and tryacquire.

00076 {
00077   ACE_TRACE ("ACE_SV_Semaphore_Simple::tryacquire_read");
00078   return this->tryacquire (n, flags);
00079 }

ASYS_INLINE int ACE_SV_Semaphore_Simple::tryacquire_write u_short    n = 0,
int    flags = 0
const
 

Try to acquire the semaphore for writing.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 84 of file SV_Semaphore_Simple.i.

References ACE_TRACE, and tryacquire.

00085 {
00086   ACE_TRACE ("ACE_SV_Semaphore_Simple::tryacquire_write");
00087   return this->tryacquire (n, flags);
00088 }


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 130 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 137 of file SV_Semaphore_Simple.h.

Referenced by ACE_SV_Semaphore_Complex::close, control, get_id, init, op, open, ACE_SV_Semaphore_Complex::open, and ACE_SV_Semaphore_Complex::~ACE_SV_Semaphore_Complex.

key_t ACE_SV_Semaphore_Simple::key_ [protected]
 

Semaphore key.

Definition at line 134 of file SV_Semaphore_Simple.h.

Referenced by ACE_SV_Semaphore_Complex::close, init, open, and ACE_SV_Semaphore_Complex::open.

int ACE_SV_Semaphore_Simple::sem_number_ [protected]
 

Number of semaphores we're creating.

Definition at line 140 of file SV_Semaphore_Simple.h.

Referenced by open, and ACE_SV_Semaphore_Complex::open.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 12:57:07 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002