Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes

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 (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 (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 <acquire>.
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
Enumerator:
ACE_CREATE 
ACE_EXCL 
ACE_OPEN 

Definition at line 53 of file SV_Semaphore_Simple.h.

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


Constructor & Destructor Documentation

ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple ( void   ) 

Definition at line 216 of file SV_Semaphore_Simple.cpp.

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
  this->init ();
}

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.

  : key_ (k)
{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
  if (this->open (k, flags, initial_value, n, perms) == -1)
    ACE_ERROR ((LM_ERROR,  ACE_TEXT ("%p\n"),  ACE_TEXT ("ACE_SV_Semaphore::ACE_SV_Semaphore")));
}

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.

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple");
  if (this->open (name,
                  flags,
                  initial_value,
                  n,
                  perms) == -1)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("%p\n"),
                ACE_TEXT ("ACE_SV_Semaphore_Simple::ACE_SV_Semaphore_Simple")));
}

ACE_SV_Semaphore_Simple::~ACE_SV_Semaphore_Simple ( void   ) 

Definition at line 210 of file SV_Semaphore_Simple.cpp.

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::~ACE_SV_Semaphore_Simple");
  this->close ();
}


Member Function Documentation

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.

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

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.

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

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.

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

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.

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::close");
  return this->init ();
}

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.

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

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.

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::control");
  if (this->internal_id_ == -1)
    return -1;
  else
    {
      semun semctl_arg;

      semctl_arg.val = value;
      return ACE_OS::semctl (this->internal_id_,
                             semnum,
                             cmd,
                             semctl_arg);
    }
}

void ACE_SV_Semaphore_Simple::dump ( void   )  const

Dump the state of an object.

Reimplemented in ACE_SV_Semaphore_Complex.

int ACE_SV_Semaphore_Simple::get_id ( void   )  const

Get underlying internal id.

Definition at line 122 of file SV_Semaphore_Simple.inl.

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::get_id");
  return this->internal_id_;
}

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.

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::init");
  this->key_ = k;
  this->internal_id_ = i;
  return 0;
}

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

Definition at line 128 of file SV_Semaphore_Simple.cpp.

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::name_2_key");

  if (name == 0)
    {
      errno = EINVAL;
      return static_cast<key_t> (ACE_INVALID_SEM_KEY);
    }

  // Basically "hash" the values in the <name>.  This won't
  // necessarily guarantee uniqueness of all keys.
  // But (IMHO) CRC32 is good enough for most purposes (Carlos)
#if defined (ACE_WIN32) && defined (_MSC_VER)
  // The cast below is legit...
#  pragma warning(push)
#  pragma warning(disable : 4312)
#endif /* defined (ACE_WIN32) && defined (_MSC_VER) */
  return (key_t) ACE::crc32 (name);
#if defined (ACE_WIN32) && defined (_MSC_VER)
#  pragma warning(pop)
#endif /* defined (ACE_WIN32) && defined (_MSC_VER) */
}

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.

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

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.

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::op");
  sembuf op_op;

  op_op.sem_num = n;
  op_op.sem_flg = flags;

  if (this->internal_id_ == -1)
    return -1;
  else if ((op_op.sem_op = val) == 0)
    return -1;
  else
    return ACE_OS::semop (this->internal_id_, &op_op, 1);
}

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.

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::open");

  key_t key;

  if (name == 0)
    key = ACE_DEFAULT_SEM_KEY;
  else
    key = this->name_2_key (name);

  return this->open (key, flags, initial_value, n, perms);
}

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.

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::open");
  union semun ivalue;

  if (k == IPC_PRIVATE || k == static_cast<key_t> (ACE_INVALID_SEM_KEY))
    return -1;

  ivalue.val = initial_value;
  this->key_ = k;
  this->sem_number_ = n;

  this->internal_id_ = ACE_OS::semget (this->key_, n, perms | flags);

  if (this->internal_id_ == -1)
    return -1;

  if (ACE_BIT_ENABLED (flags, IPC_CREAT))
    for (int i = 0; i < n; i++)
      if (ACE_OS::semctl (this->internal_id_, i, SETVAL, ivalue) == -1)
        return -1;

  return 0;
}

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.

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

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

{
  ACE_TRACE ("ACE_SV_Semaphore_Simple::remove");
  int const result = this->control (IPC_RMID);
  ((ACE_SV_Semaphore_Simple *) this)->init ();
  return result;
}

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

Non-blocking version of <acquire>.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 87 of file SV_Semaphore_Simple.inl.

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

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.

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

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.

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


Member Data Documentation

Declare the dynamic allocation hooks.

Reimplemented in ACE_SV_Semaphore_Complex.

Definition at line 163 of file SV_Semaphore_Simple.h.

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.

Number of semaphores we're creating.

Definition at line 173 of file SV_Semaphore_Simple.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines