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

SV_Semaphore_Simple.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //==========================================================================
00004 /**
00005  *  @file    SV_Semaphore_Simple.h
00006  *
00007  *  $Id: SV_Semaphore_Simple.h,v 1.1.1.4 2003/02/21 18:36:32 chad Exp $
00008  *
00009  *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
00010  */
00011 //==========================================================================
00012 
00013 #ifndef ACE_SV_SEMAPHORE_SIMPLE_H
00014 #define ACE_SV_SEMAPHORE_SIMPLE_H
00015 
00016 #include "ace/pre.h"
00017 
00018 #include "ace/ACE_export.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 #include "ace/OS.h"
00025 
00026 /**
00027  * @class ACE_SV_Semaphore_Simple
00028  *
00029  * @brief This is a simple semaphore package that assumes there are
00030  * no race conditions for initialization (i.e., the order of
00031  * process startup must be well defined).
00032  */
00033 class ACE_Export ACE_SV_Semaphore_Simple
00034 {
00035 public:
00036   enum
00037   {
00038     ACE_CREATE = IPC_CREAT,
00039     ACE_EXCL = IPC_EXCL,
00040     ACE_OPEN = 0
00041   };
00042 
00043   // = Initialization and termination methods.
00044   ACE_SV_Semaphore_Simple (void);
00045   ACE_SV_Semaphore_Simple (key_t key,
00046                            int flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00047                            int initial_value = 1,
00048                            u_short nsems = 1,
00049                            int perms = ACE_DEFAULT_FILE_PERMS);
00050   ACE_SV_Semaphore_Simple (const char *name,
00051                            int flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00052                            int initial_value = 1,
00053                            u_short nsems = 1,
00054                            int perms = ACE_DEFAULT_FILE_PERMS);
00055   ~ACE_SV_Semaphore_Simple (void);
00056 
00057   int open (const char *name,
00058             int flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00059             int initial_value = 1,
00060             u_short nsems = 1,
00061             int perms = ACE_DEFAULT_FILE_PERMS);
00062 
00063   /// Open or create one or more SV_Semaphores.  We return 0 if all is
00064   /// OK, else -1.
00065   int open (key_t key,
00066             int flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00067             int initial_value = 1,
00068             u_short nsems = 1,
00069             int perms = ACE_DEFAULT_FILE_PERMS);
00070 
00071   /// Close a ACE_SV_Semaphore, marking it as invalid for subsequent
00072   /// operations...
00073   int close (void);
00074 
00075   /**
00076    * Remove all SV_Semaphores associated with a particular key.  This
00077    * call is intended to be called from a server, for example, when it
00078    * is being shut down, as we do an IPC_RMID on the ACE_SV_Semaphore,
00079    * regardless of whether other processes may be using it or not.
00080    * Most other processes should use <close> below.
00081    */
00082   int remove (void) const;
00083 
00084   // = Semaphore acquire and release methods.
00085   /**
00086    * Wait until a ACE_SV_Semaphore's value is greater than 0, the
00087    * decrement it by 1 and return. Dijkstra's P operation, Tannenbaums
00088    * DOWN operation.
00089    */
00090   int acquire (u_short n = 0, int flags = 0) const;
00091 
00092   /// Acquire a semaphore for reading.
00093   int acquire_read (u_short n = 0, int flags = 0) const;
00094 
00095   /// Acquire a semaphore for writing
00096   int acquire_write (u_short n = 0, int flags = 0) const;
00097 
00098   /// Non-blocking version of <acquire>.
00099   int tryacquire (u_short n = 0, int flags = 0) const;
00100 
00101   /// Try to acquire the semaphore for reading.
00102   int tryacquire_read (u_short n = 0, int flags = 0) const;
00103 
00104   /// Try to acquire the semaphore for writing.
00105   int tryacquire_write (u_short n = 0, int flags = 0) const;
00106 
00107   /// Increment ACE_SV_Semaphore by one. Dijkstra's V operation,
00108   /// Tannenbaums UP operation.
00109   int release (u_short n = 0, int flags = 0) const;
00110 
00111   // = Semaphore operation methods.
00112   /// General ACE_SV_Semaphore operation. Increment or decrement by a
00113   /// specific amount (positive or negative; amount can`t be zero).
00114   int op (int val, u_short semnum = 0, int flags = SEM_UNDO) const;
00115 
00116   /// General ACE_SV_Semaphore operation on an array of SV_Semaphores.
00117   int op (sembuf op_vec[], u_short nsems) const;
00118 
00119   // = Semaphore control methods.
00120   int control (int cmd, semun arg, u_short n = 0) const;
00121   int control (int cmd, int value = 0, u_short n = 0) const;
00122 
00123   /// Get underlying internal id.
00124   int get_id (void) const;
00125 
00126   /// Dump the state of an object.
00127   void dump (void) const;
00128 
00129   /// Declare the dynamic allocation hooks.
00130   ACE_ALLOC_HOOK_DECLARE;
00131 
00132 protected:
00133   /// Semaphore key.
00134   key_t key_;
00135 
00136   /// Internal ID to identify the semaphore group within this process.
00137   int internal_id_;
00138 
00139   /// Number of semaphores we're creating.
00140   int sem_number_;
00141 
00142   /**
00143    * Convert name to key This function is used internally to create
00144    * keys for the semaphores. A valid name contains letters and
00145    * digits only and MUST start with a letter.
00146    *
00147    * The method for generating names is not very sophisticated, so
00148    * caller should not pass strings which match each other for the first
00149    * LUSED characters when he wants to get a different key.
00150    */
00151   int init (key_t k = ACE_static_cast (key_t, ACE_INVALID_SEM_KEY),
00152             int i = -1);
00153   key_t name_2_key (const char *name);
00154 };
00155 
00156 #if !defined (ACE_LACKS_INLINE_FUNCTIONS)
00157 #include "ace/SV_Semaphore_Simple.i"
00158 #endif
00159 
00160 #include "ace/post.h"
00161 
00162 #endif /* _SV_SEMAPHORE_SIMPLE_H */

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