00001 // -*- C++ -*- 00002 // 00003 // $Id: Mutex.inl 73676 2006-07-20 08:54:49Z jwillemsen $ 00004 00005 00006 #include "ace/OS_NS_sys_mman.h" 00007 00008 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00009 00010 ACE_INLINE int 00011 ACE_Mutex::acquire_read (void) 00012 { 00013 // ACE_TRACE ("ACE_Mutex::acquire_read"); 00014 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) 00015 if (this->process_lock_) 00016 return ACE_OS::mutex_lock (this->process_lock_); 00017 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ 00018 return ACE_OS::mutex_lock (&this->lock_); 00019 } 00020 00021 ACE_INLINE int 00022 ACE_Mutex::acquire_write (void) 00023 { 00024 // ACE_TRACE ("ACE_Mutex::acquire_write"); 00025 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) 00026 if (this->process_lock_) 00027 return ACE_OS::mutex_lock (this->process_lock_); 00028 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ 00029 return ACE_OS::mutex_lock (&this->lock_); 00030 } 00031 00032 ACE_INLINE int 00033 ACE_Mutex::tryacquire_read (void) 00034 { 00035 // ACE_TRACE ("ACE_Mutex::tryacquire_read"); 00036 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) 00037 if (this->process_lock_) 00038 return ACE_OS::mutex_trylock (this->process_lock_); 00039 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ 00040 return ACE_OS::mutex_trylock (&this->lock_); 00041 } 00042 00043 ACE_INLINE const ACE_mutex_t & 00044 ACE_Mutex::lock (void) const 00045 { 00046 // ACE_TRACE ("ACE_Mutex::lock"); 00047 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) 00048 if (this->process_lock_) 00049 return *this->process_lock_; 00050 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ 00051 return this->lock_; 00052 } 00053 00054 ACE_INLINE int 00055 ACE_Mutex::tryacquire_write (void) 00056 { 00057 // ACE_TRACE ("ACE_Mutex::tryacquire_write"); 00058 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) 00059 if (this->process_lock_) 00060 return ACE_OS::mutex_trylock (this->process_lock_); 00061 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ 00062 return ACE_OS::mutex_trylock (&this->lock_); 00063 } 00064 00065 ACE_INLINE int 00066 ACE_Mutex::tryacquire_write_upgrade (void) 00067 { 00068 // ACE_TRACE ("ACE_Mutex::tryacquire_write_upgrade"); 00069 return 0; 00070 } 00071 00072 ACE_INLINE int 00073 ACE_Mutex::acquire (void) 00074 { 00075 // ACE_TRACE ("ACE_Mutex::acquire"); 00076 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) 00077 if (this->process_lock_) 00078 return ACE_OS::mutex_lock (this->process_lock_); 00079 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ 00080 return ACE_OS::mutex_lock (&this->lock_); 00081 } 00082 00083 ACE_INLINE int 00084 ACE_Mutex::acquire (ACE_Time_Value &tv) 00085 { 00086 // ACE_TRACE ("ACE_Mutex::acquire"); 00087 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) 00088 if (this->process_lock_) 00089 return ACE_OS::mutex_lock (this->process_lock_, tv); 00090 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS*/ 00091 return ACE_OS::mutex_lock (&this->lock_, tv); 00092 } 00093 00094 ACE_INLINE int 00095 ACE_Mutex::acquire (ACE_Time_Value *tv) 00096 { 00097 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) 00098 if (this->process_lock_) 00099 return ACE_OS::mutex_lock (this->process_lock_, tv); 00100 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS*/ 00101 return ACE_OS::mutex_lock (&this->lock_, tv); 00102 } 00103 00104 ACE_INLINE int 00105 ACE_Mutex::tryacquire (void) 00106 { 00107 // ACE_TRACE ("ACE_Mutex::tryacquire"); 00108 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) 00109 if (this->process_lock_) 00110 return ACE_OS::mutex_trylock (this->process_lock_); 00111 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ 00112 return ACE_OS::mutex_trylock (&this->lock_); 00113 } 00114 00115 ACE_INLINE int 00116 ACE_Mutex::release (void) 00117 { 00118 // ACE_TRACE ("ACE_Mutex::release"); 00119 #if defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS) 00120 if (this->process_lock_) 00121 return ACE_OS::mutex_unlock (this->process_lock_); 00122 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ 00123 return ACE_OS::mutex_unlock (&this->lock_); 00124 } 00125 00126 ACE_INLINE int 00127 ACE_Mutex::remove (void) 00128 { 00129 // ACE_TRACE ("ACE_Mutex::remove"); 00130 int result = 0; 00131 #if defined (ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS) 00132 // In the case of a interprocess mutex, the owner is the first 00133 // process that created the shared memory object. In this case, the 00134 // lockname_ pointer will be non-zero (points to allocated memory 00135 // for the name). Owner or not, the memory needs to be unmapped 00136 // from the process. If we are the owner, the file used for 00137 // shm_open needs to be deleted as well. 00138 if (this->process_lock_) 00139 { 00140 if (this->removed_ == false) 00141 { 00142 this->removed_ = true; 00143 // Only destroy the lock if we're the ones who initialized 00144 // it. 00145 if (!this->lockname_) 00146 ACE_OS::munmap ((void *) this->process_lock_, 00147 sizeof (ACE_mutex_t)); 00148 else 00149 { 00150 result = ACE_OS::mutex_destroy (this->process_lock_); 00151 ACE_OS::munmap ((void *) this->process_lock_, 00152 sizeof (ACE_mutex_t)); 00153 ACE_OS::shm_unlink (this->lockname_); 00154 ACE_OS::free ( 00155 static_cast<void *> ( 00156 const_cast<ACE_TCHAR *> (this->lockname_))); 00157 } 00158 } 00159 } 00160 else 00161 { 00162 #else /* !ACE_HAS_PTHREADS && !ACE_HAS_STHREADS */ 00163 if (this->removed_ == false) 00164 { 00165 this->removed_ = true; 00166 result = ACE_OS::mutex_destroy (&this->lock_); 00167 } 00168 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ 00169 #if defined (ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS) 00170 } 00171 #endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */ 00172 return result; 00173 } 00174 00175 ACE_END_VERSIONED_NAMESPACE_DECL