ACE_Semaphore Class Reference

Wrapper for Dijkstra style general semaphores. More...

#include <Semaphore.h>

List of all members.

Public Member Functions

 ACE_Semaphore (unsigned int count=1, int type=USYNC_THREAD, const ACE_TCHAR *name=0, void *=0, int max=0x7fffffff)
 Initialize the semaphore, with initial value of "count".
 ~ACE_Semaphore (void)
 Implicitly destroy the semaphore.
int remove (void)
int acquire (void)
int acquire (ACE_Time_Value &tv)
int acquire (ACE_Time_Value *tv)
int tryacquire (void)
int release (void)
int release (unsigned int release_count)
int acquire_read (void)
int acquire_write (void)
int tryacquire_read (void)
int tryacquire_write (void)
int tryacquire_write_upgrade (void)
void dump (void) const
 Dump the state of an object.
const ACE_sema_tlock (void) const
 Return the underlying lock.

Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.

Protected Attributes

ACE_sema_t semaphore_
bool removed_

Private Member Functions

void operator= (const ACE_Semaphore &)
 ACE_Semaphore (const ACE_Semaphore &)


Detailed Description

Wrapper for Dijkstra style general semaphores.

Definition at line 36 of file Semaphore.h.


Constructor & Destructor Documentation

ACE_Semaphore::ACE_Semaphore ( unsigned int  count = 1,
int  type = USYNC_THREAD,
const ACE_TCHAR name = 0,
void *  = 0,
int  max = 0x7fffffff 
)

Initialize the semaphore, with initial value of "count".

Definition at line 30 of file Semaphore.cpp.

References ACE_ERROR, ACE_TEXT, ACE_UNIQUE_NAME_LEN, LM_ERROR, ACE_OS::sema_init(), and ACE::unique_name().

00035   : removed_ (false)
00036 {
00037 // ACE_TRACE ("ACE_Semaphore::ACE_Semaphore");
00038 #if defined(ACE_LACKS_UNNAMED_SEMAPHORE)
00039 // if the user does not provide a name, we generate a unique name here
00040   ACE_TCHAR iname[ACE_UNIQUE_NAME_LEN];
00041   if (name == 0)
00042     ACE::unique_name (this, iname, ACE_UNIQUE_NAME_LEN);
00043   if (ACE_OS::sema_init (&this->semaphore_, count, type,
00044                          name ? name : iname,
00045                          arg, max) != 0)
00046 #else
00047   if (ACE_OS::sema_init (&this->semaphore_, count, type,
00048                          name, arg, max) != 0)
00049 #endif
00050     ACE_ERROR ((LM_ERROR,
00051                 ACE_TEXT ("%p\n"),
00052                 ACE_TEXT ("ACE_Semaphore::ACE_Semaphore")));
00053 }

ACE_Semaphore::~ACE_Semaphore ( void   ) 

Implicitly destroy the semaphore.

Definition at line 55 of file Semaphore.cpp.

References remove().

00056 {
00057 // ACE_TRACE ("ACE_Semaphore::~ACE_Semaphore");
00058 
00059   this->remove ();
00060 }

ACE_Semaphore::ACE_Semaphore ( const ACE_Semaphore  )  [private]


Member Function Documentation

ACE_INLINE int ACE_Semaphore::acquire ( ACE_Time_Value tv  ) 

If tv == 0 then call <acquire()> directly. Otherwise, Block the thread until the semaphore count becomes greater than 0 (at which point it is decremented) or until tv times out (in which case -1 is returned and errno == ETIME). Note that <*tv> is assumed to be in "absolute" rather than "relative" time. The value of <*tv> is updated upon return to show the actual (absolute) acquisition time.

Note:
Solaris threads do not support timed semaphores. Therefore, if you're running on Solaris you might want to consider using the ACE POSIX pthreads implementation instead, which can be enabled by compiling ACE with -DACE_HAS_PTHREADS, rather than -DACE_HAS_STHREADS or -DACE_HAS_POSIX_SEM.

Definition at line 42 of file Semaphore.inl.

References ACE_OS::sema_wait().

00043 {
00044 // ACE_TRACE ("ACE_Semaphore::acquire");
00045   return ACE_OS::sema_wait (&this->semaphore_, tv);
00046 }

ACE_INLINE int ACE_Semaphore::acquire ( ACE_Time_Value tv  ) 

Block the thread until the semaphore count becomes greater than 0 (at which point it is decremented) or until tv times out (in which case -1 is returned and errno == ETIME). Note that tv is assumed to be in "absolute" rather than "relative" time. The value of tv is updated upon return to show the actual (absolute) acquisition time.

Note:
Solaris threads do not support timed semaphores. Therefore, if you're running on Solaris you might want to consider using the ACE POSIX pthreads implementation instead, which can be enabled by compiling ACE with -DACE_HAS_PTHREADS, rather than -DACE_HAS_STHREADS or -DACE_HAS_POSIX_SEM.

Definition at line 35 of file Semaphore.inl.

References ACE_OS::sema_wait().

00036 {
00037 // ACE_TRACE ("ACE_Semaphore::acquire");
00038   return ACE_OS::sema_wait (&this->semaphore_, tv);
00039 }

ACE_INLINE int ACE_Semaphore::acquire ( void   ) 

Block the thread until the semaphore count becomes greater than 0, then decrement it.

Definition at line 28 of file Semaphore.inl.

References ACE_OS::sema_wait().

Referenced by acquire_read(), and acquire_write().

00029 {
00030 // ACE_TRACE ("ACE_Semaphore::acquire");
00031   return ACE_OS::sema_wait (&this->semaphore_);
00032 }

ACE_INLINE int ACE_Semaphore::acquire_read ( void   ) 

Acquire semaphore ownership. This calls <acquire> and is only here to make the ACE_Semaphore interface consistent with the other synchronization APIs.

Definition at line 74 of file Semaphore.inl.

References acquire().

00075 {
00076   return this->acquire ();
00077 }

ACE_INLINE int ACE_Semaphore::acquire_write ( void   ) 

Acquire semaphore ownership. This calls <acquire> and is only here to make the ACE_Semaphore interface consistent with the other synchronization APIs.

Definition at line 84 of file Semaphore.inl.

References acquire().

00085 {
00086   return this->acquire ();
00087 }

ACE_BEGIN_VERSIONED_NAMESPACE_DECL void ACE_Semaphore::dump ( void   )  const

Dump the state of an object.

Definition at line 21 of file Semaphore.cpp.

References ACE_BEGIN_DUMP, ACE_DEBUG, ACE_END_DUMP, ACE_TEXT, and LM_DEBUG.

00022 {
00023 // ACE_TRACE ("ACE_Semaphore::dump");
00024 
00025   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00026   ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
00027   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00028 }

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_INLINE const ACE_sema_t & ACE_Semaphore::lock ( void   )  const

Return the underlying lock.

Definition at line 8 of file Semaphore.inl.

References semaphore_.

00009 {
00010 // ACE_TRACE ("ACE_Semaphore::lock");
00011   return this->semaphore_;
00012 }

void ACE_Semaphore::operator= ( const ACE_Semaphore  )  [private]

ACE_INLINE int ACE_Semaphore::release ( unsigned int  release_count  ) 

Increment the semaphore by release_count, potentially unblocking waiting threads.

Definition at line 63 of file Semaphore.inl.

References ACE_OS::sema_post().

00064 {
00065 // ACE_TRACE ("ACE_Semaphore::release");
00066   return ACE_OS::sema_post (&this->semaphore_, release_count);
00067 }

ACE_INLINE int ACE_Semaphore::release ( void   ) 

Increment the semaphore by 1, potentially unblocking a waiting thread.

Definition at line 56 of file Semaphore.inl.

References ACE_OS::sema_post().

00057 {
00058 // ACE_TRACE ("ACE_Semaphore::release");
00059   return ACE_OS::sema_post (&this->semaphore_);
00060 }

ACE_INLINE int ACE_Semaphore::remove ( void   ) 

Explicitly destroy the semaphore. Note that only one thread should call this method since it doesn't protect against race conditions.

Definition at line 15 of file Semaphore.inl.

References removed_, and ACE_OS::sema_destroy().

Referenced by ~ACE_Semaphore().

00016 {
00017 // ACE_TRACE ("ACE_Semaphore::remove");
00018   int result = 0;
00019   if (!this->removed_)
00020     {
00021       this->removed_ = true;
00022       result = ACE_OS::sema_destroy (&this->semaphore_);
00023     }
00024   return result;
00025 }

ACE_INLINE int ACE_Semaphore::tryacquire ( void   ) 

Conditionally decrement the semaphore if count is greater than 0 (i.e., won't block). Returns -1 on failure. If we "failed" because someone else already had the lock, errno is set to EBUSY.

Definition at line 49 of file Semaphore.inl.

References ACE_OS::sema_trywait().

Referenced by tryacquire_read(), and tryacquire_write().

00050 {
00051 // ACE_TRACE ("ACE_Semaphore::tryacquire");
00052   return ACE_OS::sema_trywait (&this->semaphore_);
00053 }

ACE_INLINE int ACE_Semaphore::tryacquire_read ( void   ) 

Conditionally acquire semaphore (i.e., won't block). This calls <tryacquire> and is only here to make the ACE_Semaphore interface consistent with the other synchronization APIs. Returns -1 on failure. If we "failed" because someone else already had the lock, errno is set to EBUSY.

Definition at line 94 of file Semaphore.inl.

References tryacquire().

00095 {
00096   return this->tryacquire ();
00097 }

ACE_INLINE int ACE_Semaphore::tryacquire_write ( void   ) 

Conditionally acquire semaphore (i.e., won't block). This calls <tryacquire> and is only here to make the ACE_Semaphore interface consistent with the other synchronization APIs. Returns -1 on failure. If we "failed" because someone else already had the lock, errno is set to EBUSY.

Definition at line 104 of file Semaphore.inl.

References tryacquire().

00105 {
00106   return this->tryacquire ();
00107 }

ACE_INLINE int ACE_Semaphore::tryacquire_write_upgrade ( void   ) 

This is only here to make the ACE_Semaphore interface consistent with the other synchronization APIs. Assumes the caller has already acquired the semaphore using one of the above calls, and returns 0 (success) always.

Definition at line 114 of file Semaphore.inl.

00115 {
00116   return 0;
00117 }


Member Data Documentation

ACE_Semaphore::ACE_ALLOC_HOOK_DECLARE

Declare the dynamic allocation hooks.

Definition at line 155 of file Semaphore.h.

bool ACE_Semaphore::removed_ [protected]

Keeps track of whether remove() has been called yet to avoid multiple remove() calls, e.g., explicitly and implicitly in the destructor. This flag isn't protected by a lock, so make sure that you don't have multiple threads simultaneously calling remove () on the same object, which is a bad idea anyway...

Definition at line 168 of file Semaphore.h.

Referenced by remove().

ACE_sema_t ACE_Semaphore::semaphore_ [protected]

Definition at line 161 of file Semaphore.h.

Referenced by lock().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:35:33 2010 for ACE by  doxygen 1.4.7