00001 /* -*- C++ -*- */ 00002 00003 //============================================================================= 00004 /** 00005 * @file Process_Semaphore.h 00006 * 00007 * $Id: Process_Semaphore.h,v 1.1.1.1 2001/12/04 14:33:07 chad Exp $ 00008 * 00009 * Wrapper for Dijkstra style general semaphores that work 00010 * across processes. 00011 * 00012 * 00013 * @author Doug Schmidt 00014 */ 00015 //============================================================================= 00016 00017 00018 #ifndef ACE_PROCESS_SEMAPHORE_H 00019 #define ACE_PROCESS_SEMAPHORE_H 00020 #include "ace/pre.h" 00021 00022 #include "ace/Synch.h" 00023 00024 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00025 # pragma once 00026 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00027 00028 #if !(defined (ACE_WIN32) || defined (ACE_HAS_POSIX_SEM) || defined (ACE_PSOS)) 00029 #include "ace/SV_Semaphore_Complex.h" 00030 #endif /* !(ACE_WIN32 || ACE_HAS_POSIX_SEM || ACE_PSOS) */ 00031 00032 /** 00033 * @class ACE_Process_Semaphore 00034 * 00035 * @brief Wrapper for Dijkstra style general semaphores that work 00036 * across processes. 00037 */ 00038 class ACE_Export ACE_Process_Semaphore 00039 { 00040 public: 00041 /// Initialize the semaphore, with an initial value of <count> and a 00042 /// maximum value of <max>. 00043 ACE_Process_Semaphore (u_int count = 1, // By default make this unlocked. 00044 const ACE_TCHAR *name = 0, 00045 void * = 0, 00046 int max = 0x7FFFFFFF); 00047 00048 /** 00049 * This method is a no-op, i.e., it doesn't remove the semaphore. 00050 * If you want to remove the semaphore, you must call the <remove> 00051 * method explicitly. 00052 */ 00053 ~ACE_Process_Semaphore (void); 00054 00055 /** 00056 * Explicitly destroy the semaphore. Note that only one thread 00057 * should call this method since it doesn't protect against race 00058 * conditions. 00059 */ 00060 int remove (void); 00061 00062 /// Block the thread until the semaphore count becomes greater than 00063 /// 0, then decrement it. 00064 int acquire (void); 00065 00066 /** 00067 * Conditionally decrement the semaphore if count is greater than 0 00068 * (i.e., won't block). Returns -1 on failure. If we "failed" 00069 * because someone else already had the lock, <errno> is set to 00070 * <EBUSY>. 00071 */ 00072 int tryacquire (void); 00073 00074 /// Increment the semaphore, potentially unblocking a waiting thread. 00075 int release (void); 00076 00077 /** 00078 * Acquire semaphore ownership. This calls <acquire> and is only 00079 * here to make the <ACE_Process_Semaphore> interface consistent 00080 * with the other synchronization APIs. 00081 */ 00082 int acquire_read (void); 00083 00084 /** 00085 * Acquire semaphore ownership. This calls <acquire> and is only 00086 * here to make the <ACE_Process_Semaphore> interface consistent 00087 * with the other synchronization APIs. 00088 */ 00089 int acquire_write (void); 00090 00091 /** 00092 * Conditionally acquire semaphore (i.e., won't block). This calls 00093 * <tryacquire> and is only here to make the <ACE_Process_Semaphore> 00094 * interface consistent with the other synchronization APIs. 00095 * Returns -1 on failure. If we "failed" because someone else 00096 * already had the lock, <errno> is set to <EBUSY>. 00097 */ 00098 int tryacquire_read (void); 00099 00100 /** 00101 * Conditionally acquire semaphore (i.e., won't block). This calls 00102 * <tryacquire> and is only here to make the <ACE_Process_Semaphore> 00103 * interface consistent with the other synchronization APIs. 00104 * Returns -1 on failure. If we "failed" because someone else 00105 * already had the lock, <errno> is set to <EBUSY>. 00106 */ 00107 int tryacquire_write (void); 00108 00109 /** 00110 * This is only here to make the <ACE_Process_Semaphore> 00111 * interface consistent with the other synchronization APIs. 00112 * Assumes the caller has already acquired the semaphore using one of 00113 * the above calls, and returns 0 (success) always. 00114 */ 00115 int tryacquire_write_upgrade (void); 00116 00117 #if defined (ACE_WIN32) || defined (ACE_HAS_POSIX_SEM) || defined (ACE_PSOS) 00118 /// Return the underlying lock. 00119 const ACE_sema_t &lock (void) const; 00120 #endif /* ACE_WIN32 || ACE_HAS_POSIX_SEM || ACE_PSOS */ 00121 00122 /// Dump the state of an object. 00123 void dump (void) const; 00124 00125 /// Declare the dynamic allocation hooks. 00126 ACE_ALLOC_HOOK_DECLARE; 00127 00128 protected: 00129 #if defined (ACE_WIN32) || defined (ACE_HAS_POSIX_SEM) || defined (ACE_PSOS) 00130 ACE_Semaphore lock_; 00131 #else 00132 /// We need this to get the right semantics... 00133 ACE_SV_Semaphore_Complex lock_; 00134 #endif /* ACE_WIN32 || ACE_HAS_POSIX_SEM || ACE_PSOS */ 00135 }; 00136 00137 #if defined (__ACE_INLINE__) 00138 #include "ace/Process_Semaphore.inl" 00139 #endif /* __ACE_INLINE__ */ 00140 00141 #include "ace/post.h" 00142 #endif /* ACE_PROCESS_SEMAPHORE_H */
1.2.14 written by Dimitri van Heesch,
© 1997-2002