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

SV_Semaphore_Complex.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 //=============================================================================
00004 /**
00005  *  @file   SV_Semaphore_Complex.h
00006  *
00007  *  $Id: SV_Semaphore_Complex.h,v 1.1.1.3 2001/12/04 14:33:08 chad Exp $
00008  *
00009  *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACE_SV_SEMAPHORE_COMPLEX_H
00014 #define ACE_SV_SEMAPHORE_COMPLEX_H
00015 #include "ace/pre.h"
00016 
00017 #include "ace/SV_Semaphore_Simple.h"
00018 
00019 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00020 # pragma once
00021 #endif /* ACE_LACKS_PRAGMA_ONCE */
00022 
00023 /**
00024  * @class ACE_SV_Semaphore_Complex
00025  *
00026  * @brief This is a more complex semaphore wrapper that handles race
00027  * conditions for initialization correctly...
00028  *
00029  * This code is a port to C++, inspired by: W. Richard Stevens
00030  * from his book: UNIX Network Programming (Prentice Hall, ISBN
00031  * 0-13-949876-1 - 1990).  We provide a simpler and easier to
00032  * understand interface to the System V Semaphore system calls.
00033  * We create and use a 2 + n-member set for the requested
00034  * <ACE_SV_Semaphore_Complex>. The first member, [0], is a
00035  * counter used to know when all processes have finished with
00036  * the <ACE_SV_Semaphore_Complex>.  The counter is initialized
00037  * to a large number, decremented on every create or open and
00038  * incremented on every close. This way we can use the "adjust"
00039  * feature provided by System V so that any process that exit's
00040  * without calling <close> is accounted for. It doesn't help us
00041  * if the last process does this (as we have no way of getting
00042  * control to remove the <ACE_SV_Semaphore_Complex>) but it
00043  * will work if any process other than the last does an exit
00044  * (intentional or unintentional).
00045  * The second member, [1], of the semaphore is used as a lock
00046  * variable to avoid any race conditions in the <create> and
00047  * <close> functions.
00048  * The members beyond [1] are actual semaphore values in the
00049  * array of semaphores, which may be sized by the user in the
00050  * constructor.
00051  */
00052 class ACE_Export ACE_SV_Semaphore_Complex : private ACE_SV_Semaphore_Simple
00053 {
00054 public:
00055   enum
00056   {
00057     ACE_CREATE = IPC_CREAT,
00058     ACE_OPEN   = 0
00059   };
00060 
00061   // = Initialization and termination methods.
00062   ACE_SV_Semaphore_Complex (void);
00063   ACE_SV_Semaphore_Complex (key_t key,
00064                             int create = ACE_SV_Semaphore_Complex::ACE_CREATE,
00065                             int initial_value = 1,
00066                             u_short nsems = 1,
00067                             int perms = ACE_DEFAULT_FILE_PERMS);
00068   ACE_SV_Semaphore_Complex (const char *name,
00069                             int create = ACE_SV_Semaphore_Complex::ACE_CREATE,
00070                             int initial_value = 1,
00071                             u_short nsems = 1,
00072                             int perms = ACE_DEFAULT_FILE_PERMS);
00073   ~ACE_SV_Semaphore_Complex (void);
00074 
00075   /// Open or create an array of SV_Semaphores.  We return 0 if all is
00076   /// OK, else -1.
00077   int open (const char *name,
00078             int flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00079             int initial_value = 1,
00080             u_short nsems = 1,
00081             int perms = ACE_DEFAULT_FILE_PERMS);
00082 
00083   /// Open or create an array of SV_Semaphores.  We return 0 if all is
00084   /// OK, else -1.
00085   int open (key_t key,
00086             int flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00087             int initial_value = 1,
00088             u_short nsems = 1,
00089             int perms = ACE_DEFAULT_FILE_PERMS);
00090 
00091   /**
00092    * Close an ACE_SV_Semaphore. Unlike the <remove> method, this
00093    * method is for a process to call before it exits, when it is done
00094    * with the ACE_SV_Semaphore. We "decrement" the counter of
00095    * processes using the ACE_SV_Semaphore, and if this was the last
00096    * one, we can remove the ACE_SV_Semaphore.
00097    */
00098   int close (void);
00099 
00100   // = Semaphore acquire and release methods.
00101 
00102   /// Acquire the semaphore.
00103   int acquire (u_short n = 0, int flags = 0) const;
00104 
00105   /// Acquire a semaphore for reading.
00106   int acquire_read (u_short n = 0, int flags = 0) const;
00107 
00108   /// Acquire a semaphore for writing
00109   int acquire_write (u_short n = 0, int flags = 0) const;
00110 
00111   /// Try to acquire the semaphore.
00112   int tryacquire (u_short n = 0, int flags = 0) const;
00113 
00114   /// Try to acquire the semaphore for reading.
00115   int tryacquire_read (u_short n = 0, int flags = 0) const;
00116 
00117   /// Try to acquire the semaphore for writing.
00118   int tryacquire_write (u_short n = 0, int flags = 0) const;
00119 
00120   /// Release the semaphore.
00121   int release (u_short n = 0, int flags = 0) const;
00122 
00123   // = Semaphore operation methods.
00124   int op (int val, u_short n = 0, int flags = 0) const;
00125   int op (sembuf op_vec[], u_short n) const;
00126 
00127   // = Semaphore control methods.
00128   int control (int cmd, semun arg, u_short n = 0) const;
00129   int control (int cmd, int value = 0, u_short n = 0) const;
00130 
00131   // = Upgrade access control...
00132   ACE_USING ACE_SV_Semaphore_Simple::get_id;
00133   ACE_USING ACE_SV_Semaphore_Simple::remove;
00134 
00135   /// Dump the state of an object.
00136   void dump (void) const;
00137 
00138   /// Declare the dynamic allocation hooks.
00139   ACE_ALLOC_HOOK_DECLARE;
00140 
00141 private:
00142   static const int BIGCOUNT_;
00143   static sembuf op_lock_[2];
00144   static sembuf op_endcreate_[2];
00145   static sembuf op_open_[1];
00146   static sembuf op_close_[3];
00147   static sembuf op_unlock_[1];
00148 };
00149 
00150 #if !defined (ACE_LACKS_INLINE_FUNCTIONS)
00151 #include "ace/SV_Semaphore_Complex.i"
00152 #endif
00153 
00154 #include "ace/post.h"
00155 #endif /* ACE_SV_SEMAPHORE_COMPLEX_H */

Generated on Mon Jun 16 11:21:32 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002