SV_Semaphore_Simple.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //==========================================================================
00004 /**
00005  *  @file    SV_Semaphore_Simple.h
00006  *
00007  *  SV_Semaphore_Simple.h,v 4.35 2006/05/30 13:15:25 schmidt 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_include/sys/os_stat.h"
00025 #include "ace/os_include/sys/os_ipc.h"
00026 #include "ace/os_include/sys/os_sem.h"
00027 #include "ace/Default_Constants.h"
00028 
00029 #if defined (ACE_WIN32) 
00030    // Default semaphore key and mutex name
00031 #  if !defined (ACE_DEFAULT_SEM_KEY)
00032 #    define ACE_DEFAULT_SEM_KEY "ACE_SEM_KEY"
00033 #  endif /* ACE_DEFAULT_SEM_KEY */
00034 #else /* !defined (ACE_WIN32) */
00035    // Default semaphore key
00036 #  if !defined (ACE_DEFAULT_SEM_KEY)
00037 #    define ACE_DEFAULT_SEM_KEY 1234
00038 #  endif /* ACE_DEFAULT_SEM_KEY */
00039 #endif /* ACE_WIN32 */
00040 
00041 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00042 
00043 /**
00044  * @class ACE_SV_Semaphore_Simple
00045  *
00046  * @brief This is a simple semaphore package that assumes there are
00047  * no race conditions for initialization (i.e., the order of
00048  * process startup must be well defined).
00049  */
00050 class ACE_Export ACE_SV_Semaphore_Simple
00051 {
00052 public:
00053   enum
00054   {
00055     ACE_CREATE = IPC_CREAT,
00056     ACE_EXCL = IPC_EXCL,
00057     ACE_OPEN = 0
00058   };
00059 
00060   // = Initialization and termination methods.
00061   ACE_SV_Semaphore_Simple (void);
00062   ACE_SV_Semaphore_Simple (key_t key,
00063                            short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00064                            int initial_value = 1,
00065                            u_short nsems = 1,
00066                            mode_t perms = ACE_DEFAULT_FILE_PERMS);
00067   ACE_SV_Semaphore_Simple (const char *name,
00068                            short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00069                            int initial_value = 1,
00070                            u_short nsems = 1,
00071                            mode_t perms = ACE_DEFAULT_FILE_PERMS);
00072 #if defined (ACE_HAS_WCHAR)
00073   ACE_SV_Semaphore_Simple (const wchar_t *name,
00074                            short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00075                            int initial_value = 1,
00076                            u_short nsems = 1,
00077                            mode_t perms = ACE_DEFAULT_FILE_PERMS);
00078 #endif /* ACE_HAS_WCHAR */
00079 
00080   ~ACE_SV_Semaphore_Simple (void);
00081 
00082   int open (const char *name,
00083             short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00084             int initial_value = 1,
00085             u_short nsems = 1,
00086             mode_t perms = ACE_DEFAULT_FILE_PERMS);
00087 
00088 #if defined (ACE_HAS_WCHAR)
00089   int open (const wchar_t *name,
00090             short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00091             int initial_value = 1,
00092             u_short nsems = 1,
00093             mode_t perms = ACE_DEFAULT_FILE_PERMS);
00094 #endif /* ACE_HAS_WCHAR */
00095 
00096   /// Open or create one or more SV_Semaphores.  We return 0 if all is
00097   /// OK, else -1.
00098   int open (key_t key,
00099             short flags = ACE_SV_Semaphore_Simple::ACE_CREATE,
00100             int initial_value = 1,
00101             u_short nsems = 1,
00102             mode_t perms = ACE_DEFAULT_FILE_PERMS);
00103 
00104   /// Close a ACE_SV_Semaphore, marking it as invalid for subsequent
00105   /// operations...
00106   int close (void);
00107 
00108   /**
00109    * Remove all SV_Semaphores associated with a particular key.  This
00110    * call is intended to be called from a server, for example, when it
00111    * is being shut down, as we do an IPC_RMID on the ACE_SV_Semaphore,
00112    * regardless of whether other processes may be using it or not.
00113    * Most other processes should use <close> below.
00114    */
00115   int remove (void) const;
00116 
00117   // = Semaphore acquire and release methods.
00118   /**
00119    * Wait until a ACE_SV_Semaphore's value is greater than 0, the
00120    * decrement it by 1 and return. Dijkstra's P operation, Tannenbaums
00121    * DOWN operation.
00122    */
00123   int acquire (u_short n = 0, short flags = 0) const;
00124 
00125   /// Acquire a semaphore for reading.
00126   int acquire_read (u_short n = 0, short flags = 0) const;
00127 
00128   /// Acquire a semaphore for writing
00129   int acquire_write (u_short n = 0, short flags = 0) const;
00130 
00131   /// Non-blocking version of <acquire>.
00132   int tryacquire (u_short n = 0, short flags = 0) const;
00133 
00134   /// Try to acquire the semaphore for reading.
00135   int tryacquire_read (u_short n = 0, short flags = 0) const;
00136 
00137   /// Try to acquire the semaphore for writing.
00138   int tryacquire_write (u_short n = 0, short flags = 0) const;
00139 
00140   /// Increment ACE_SV_Semaphore by one. Dijkstra's V operation,
00141   /// Tannenbaums UP operation.
00142   int release (u_short n = 0, short flags = 0) const;
00143 
00144   // = Semaphore operation methods.
00145   /// General ACE_SV_Semaphore operation. Increment or decrement by a
00146   /// specific amount (positive or negative; amount can`t be zero).
00147   int op (short val, u_short semnum = 0, short flags = SEM_UNDO) const;
00148 
00149   /// General ACE_SV_Semaphore operation on an array of SV_Semaphores.
00150   int op (sembuf op_vec[], u_short nsems) const;
00151 
00152   // = Semaphore control methods.
00153   int control (int cmd, semun arg, u_short n = 0) const;
00154   int control (int cmd, int value = 0, u_short n = 0) const;
00155 
00156   /// Get underlying internal id.
00157   int get_id (void) const;
00158 
00159   /// Dump the state of an object.
00160   void dump (void) const;
00161 
00162   /// Declare the dynamic allocation hooks.
00163   ACE_ALLOC_HOOK_DECLARE;
00164 
00165 protected:
00166   /// Semaphore key.
00167   key_t key_;
00168 
00169   /// Internal ID to identify the semaphore group within this process.
00170   int internal_id_;
00171 
00172   /// Number of semaphores we're creating.
00173   int sem_number_;
00174 
00175   /**
00176    * Convert name to key This function is used internally to create
00177    * keys for the semaphores. A valid name contains letters and
00178    * digits only and MUST start with a letter.
00179    *
00180    * The method for generating names is not very sophisticated, so
00181    * caller should not pass strings which match each other for the first
00182    * LUSED characters when he wants to get a different key.
00183    */
00184   int init (key_t k = static_cast<key_t> (ACE_INVALID_SEM_KEY),
00185             int i = -1);
00186   key_t name_2_key (const char *name);
00187 };
00188 
00189 ACE_END_VERSIONED_NAMESPACE_DECL
00190 
00191 #if defined (__ACE_INLINE__)
00192 #include "ace/SV_Semaphore_Simple.inl"
00193 #endif /* __ACE_INLINE__ */
00194 
00195 #include /**/ "ace/post.h"
00196 
00197 #endif /* _SV_SEMAPHORE_SIMPLE_H */

Generated on Thu Nov 9 09:42:06 2006 for ACE by doxygen 1.3.6