Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

# ACE_SV_Semaphore_Complex Class Reference

This is a more complex semaphore wrapper that handles race conditions for initialization correctly... More...

`#include <SV_Semaphore_Complex.h>`

Inheritance diagram for ACE_SV_Semaphore_Complex:

[legend]
Collaboration diagram for ACE_SV_Semaphore_Complex:

[legend]
List of all members.

## Public Types
enum | { ACE_CREATE = IPC_CREAT, ACE_OPEN = 0 }
## Public Member Functions
| ACE_SV_Semaphore_Complex (void)
| ACE_SV_Semaphore_Complex (key_t key, int create=ACE_SV_Semaphore_Complex::ACE_CREATE, int initial_value=1, u_short nsems=1, int perms=ACE_DEFAULT_FILE_PERMS)
| ACE_SV_Semaphore_Complex (const char *name, int create=ACE_SV_Semaphore_Complex::ACE_CREATE, int initial_value=1, u_short nsems=1, int perms=ACE_DEFAULT_FILE_PERMS)
| ~ACE_SV_Semaphore_Complex (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)
int | close (void)
int | acquire (u_short n=0, int flags=0) const
| Acquire the semaphore.
int | acquire_read (u_short n=0, int flags=0) const
| Acquire a semaphore for reading.
int | acquire_write (u_short n=0, int flags=0) const
| Acquire a semaphore for writing.
int | tryacquire (u_short n=0, int flags=0) const
| Try to acquire the semaphore.
int | tryacquire_read (u_short n=0, int flags=0) const
| Try to acquire the semaphore for reading.
int | tryacquire_write (u_short n=0, int flags=0) const
| Try to acquire the semaphore for writing.
int | release (u_short n=0, int flags=0) const
| Release the semaphore.
int | op (int val, u_short n=0, int flags=0) const
int | op (sembuf op_vec[], u_short n) 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
void | dump (void) const
| Dump the state of an object.
int | get_id (void) const
| Get underlying internal id.
int | remove (void) const
## Public Attributes
| ACE_ALLOC_HOOK_DECLARE
| Declare the dynamic allocation hooks.
## Static Private Attributes
const int | BIGCOUNT_ = 10000
sembuf | op_lock_ [2]
sembuf | op_endcreate_ [2]
sembuf | op_open_ [1]
sembuf | op_close_ [3]
sembuf | op_unlock_ [1]

## Detailed Description

This is a more complex semaphore wrapper that handles race conditions for initialization correctly...

This code is a port to C++, inspired by: W. Richard Stevens from his book: UNIX Network Programming (Prentice Hall, ISBN 0-13-949876-1 - 1990). We provide a simpler and easier to understand interface to the System V Semaphore system calls. We create and use a 2 + n-member set for the requested <ace_sv_semaphore_complex>. The first member, [0], is a counter used to know when all processes have finished with the <ace_sv_semaphore_complex>. The counter is initialized to a large number, decremented on every create or open and incremented on every close. This way we can use the "adjust" feature provided by System V so that any process that exit's without calling <close> is accounted for. It doesn't help us if the last process does this (as we have no way of getting control to remove the <ace_sv_semaphore_complex>) but it will work if any process other than the last does an exit (intentional or unintentional). The second member, [1], of the semaphore is used as a lock variable to avoid any race conditions in the <create> and <close> functions. The members beyond [1] are actual semaphore values in the array of semaphores, which may be sized by the user in the constructor.

## Member Enumeration Documentation

anonymous enum

  | Enumeration values:
  ACE_CREATE |
  ACE_OPEN |

## Constructor & Destructor Documentation

ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex | ( | void |  | ) |

ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex | ( | key_t | key,
|  | int | create = `ACE_SV_Semaphore_Complex::ACE_CREATE`,
|  | int | initial_value = `1`,
|  | u_short | nsems = `1`,
|  | int | perms = `ACE_DEFAULT_FILE_PERMS`
| ) |

ACE_SV_Semaphore_Complex::ACE_SV_Semaphore_Complex | ( | const char * | name,
|  | int | create = `ACE_SV_Semaphore_Complex::ACE_CREATE`,
|  | int | initial_value = `1`,
|  | u_short | nsems = `1`,
|  | int | perms = `ACE_DEFAULT_FILE_PERMS`
| ) |

ACE_SV_Semaphore_Complex::~ACE_SV_Semaphore_Complex | ( | void |  | ) |

## Member Function Documentation

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_INLINE int ACE_SV_Semaphore_Complex::acquire | ( | u_short | n = `0`,
|  | int | flags = `0`
| ) | const

| Acquire the semaphore. Reimplemented from ACE_SV_Semaphore_Simple.

ACE_INLINE int ACE_SV_Semaphore_Complex::acquire_read | ( | u_short | n = `0`,
|  | int | flags = `0`
| ) | const

| Acquire a semaphore for reading. Reimplemented from ACE_SV_Semaphore_Simple.

ACE_INLINE int ACE_SV_Semaphore_Complex::acquire_write | ( | u_short | n = `0`,
|  | int | flags = `0`
| ) | const

| Acquire a semaphore for writing. Reimplemented from ACE_SV_Semaphore_Simple.

int ACE_SV_Semaphore_Complex::close | ( | void |  | ) |

| Close an ACE_SV_Semaphore. Unlike the <remove> method, this method is for a process to call before it exits, when it is done with the ACE_SV_Semaphore. We "decrement" the counter of processes using the ACE_SV_Semaphore, and if this was the last one, we can remove the ACE_SV_Semaphore. Reimplemented from ACE_SV_Semaphore_Simple.

ACE_INLINE int ACE_SV_Semaphore_Complex::control | ( | int | cmd,
|  | int | value = `0`,
|  | u_short | n = `0`
| ) | const

| Reimplemented from ACE_SV_Semaphore_Simple.

ACE_INLINE int ACE_SV_Semaphore_Complex::control | ( | int | cmd,
|  | semun | arg,
|  | u_short | n = `0`
| ) | const

| Reimplemented from ACE_SV_Semaphore_Simple.

ACE_BEGIN_VERSIONED_NAMESPACE_DECL void ACE_SV_Semaphore_Complex::dump | ( | void |  | ) | const

| Dump the state of an object. Reimplemented from ACE_SV_Semaphore_Simple.

int ACE_SV_Semaphore_Simple::get_id | ( | void |  | ) | const

| Get underlying internal id.

ACE_INLINE int ACE_SV_Semaphore_Complex::op | ( | sembuf | op_vec[],
|  | u_short | n
| ) | const

| General ACE_SV_Semaphore operation on an array of SV_Semaphores. Reimplemented from ACE_SV_Semaphore_Simple.

ACE_INLINE int ACE_SV_Semaphore_Complex::op | ( | int | val,
|  | u_short | n = `0`,
|  | int | flags = `0`
| ) | const

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

int ACE_SV_Semaphore_Complex::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 an array of SV_Semaphores. We return 0 if all is OK, else -1. Reimplemented from ACE_SV_Semaphore_Simple.

int ACE_SV_Semaphore_Complex::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`
| ) |

| Open or create an array of SV_Semaphores. We return 0 if all is OK, else -1. Reimplemented from ACE_SV_Semaphore_Simple.

ACE_INLINE int ACE_SV_Semaphore_Complex::release | ( | u_short | n = `0`,
|  | int | flags = `0`
| ) | const

| Release the semaphore. Reimplemented from ACE_SV_Semaphore_Simple.

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.

ACE_INLINE int ACE_SV_Semaphore_Complex::tryacquire | ( | u_short | n = `0`,
|  | int | flags = `0`
| ) | const

| Try to acquire the semaphore. Reimplemented from ACE_SV_Semaphore_Simple.

ACE_INLINE int ACE_SV_Semaphore_Complex::tryacquire_read | ( | u_short | n = `0`,
|  | int | flags = `0`
| ) | const

| Try to acquire the semaphore for reading. Reimplemented from ACE_SV_Semaphore_Simple.

ACE_INLINE int ACE_SV_Semaphore_Complex::tryacquire_write | ( | u_short | n = `0`,
|  | int | flags = `0`
| ) | const

| Try to acquire the semaphore for writing. Reimplemented from ACE_SV_Semaphore_Simple.

## Member Data Documentation

ACE_SV_Semaphore_Complex::ACE_ALLOC_HOOK_DECLARE

| Declare the dynamic allocation hooks. Reimplemented from ACE_SV_Semaphore_Simple.

const int ACE_SV_Semaphore_Complex::BIGCOUNT_ = 10000`[static, private]`

sembuf ACE_SV_Semaphore_Complex::op_close_`[static, private]`

| Initial value:
    {
      {0, 0, 0},
      {0, 1, SEM_UNDO},
      {1, 1, SEM_UNDO},
    }

sembuf ACE_SV_Semaphore_Complex::op_endcreate_`[static, private]`

| Initial value:
    {
      {1, -1, SEM_UNDO},

      {0, -1, SEM_UNDO},
    }

sembuf ACE_SV_Semaphore_Complex::op_lock_`[static, private]`

| Initial value:
    {
      {0, 0, 0},
      {0, 1, SEM_UNDO},

    }

sembuf ACE_SV_Semaphore_Complex::op_open_`[static, private]`

| Initial value:
    {
      {1, -1, SEM_UNDO},

    }

sembuf ACE_SV_Semaphore_Complex::op_unlock_`[static, private]`

| Initial value:
    {
      {0, -1, SEM_UNDO},
    }

The documentation for this class was generated from the following files:

- SV_Semaphore_Complex.h
- SV_Semaphore_Complex.cpp
- SV_Semaphore_Complex.inl