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

Synch_T.i

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 // $Id: Synch_T.i,v 1.1.1.3 2001/12/04 14:33:10 chad Exp $
00003 
00004 #include "ace/Thread.h"
00005 
00006 template <class ACE_LOCK> ACE_INLINE int
00007 ACE_Guard<ACE_LOCK>::acquire (void)
00008 {
00009   return this->owner_ = this->lock_->acquire ();
00010 }
00011 
00012 template <class ACE_LOCK> ACE_INLINE int
00013 ACE_Guard<ACE_LOCK>::tryacquire (void)
00014 {
00015   return this->owner_ = this->lock_->tryacquire ();
00016 }
00017 
00018 template <class ACE_LOCK> ACE_INLINE int
00019 ACE_Guard<ACE_LOCK>::release (void)
00020 {
00021   if (this->owner_ == -1)
00022     return -1;
00023   else
00024     {
00025       this->owner_ = -1;
00026       return this->lock_->release ();
00027     }
00028 }
00029 
00030 template <class ACE_LOCK> ACE_INLINE
00031 ACE_Guard<ACE_LOCK>::ACE_Guard (ACE_LOCK &l)
00032   : lock_ (&l),
00033     owner_ (0)
00034 {
00035   this->acquire ();
00036 }
00037 
00038 template <class ACE_LOCK> ACE_INLINE
00039 ACE_Guard<ACE_LOCK>::ACE_Guard (ACE_LOCK &l, int block)
00040   : lock_ (&l),
00041     owner_ (0)
00042 {
00043   if (block)
00044     this->acquire ();
00045   else
00046     this->tryacquire ();
00047 }
00048 
00049 template <class ACE_LOCK> ACE_INLINE
00050 ACE_Guard<ACE_LOCK>::ACE_Guard (ACE_LOCK &l, int block, int become_owner)
00051   : lock_ (&l),
00052     owner_ (become_owner == 0 ? -1 : 0)
00053 {
00054   ACE_UNUSED_ARG (block);
00055 }
00056 
00057 // Implicitly and automatically acquire (or try to acquire) the
00058 // lock.
00059 
00060 template <class ACE_LOCK> ACE_INLINE
00061 ACE_Guard<ACE_LOCK>::~ACE_Guard (void)
00062 {
00063   this->release ();
00064 }
00065 
00066 template <class ACE_LOCK> ACE_INLINE int
00067 ACE_Guard<ACE_LOCK>::locked (void) const
00068 {
00069   return this->owner_ != -1;
00070 }
00071 
00072 template <class ACE_LOCK> ACE_INLINE int
00073 ACE_Guard<ACE_LOCK>::remove (void)
00074 {
00075   return this->lock_->remove ();
00076 }
00077 
00078 template <class ACE_LOCK> ACE_INLINE void
00079 ACE_Guard<ACE_LOCK>::disown (void)
00080 {
00081   this->owner_ = -1;
00082 }
00083 
00084 template <class ACE_LOCK> ACE_INLINE
00085 ACE_Write_Guard<ACE_LOCK>::ACE_Write_Guard (ACE_LOCK &m)
00086   : ACE_Guard<ACE_LOCK> (&m)
00087 {
00088   this->acquire_write ();
00089 }
00090 
00091 template <class ACE_LOCK> ACE_INLINE int
00092 ACE_Write_Guard<ACE_LOCK>::acquire_write (void)
00093 {
00094   return this->owner_ = this->lock_->acquire_write ();
00095 }
00096 
00097 template <class ACE_LOCK> ACE_INLINE int
00098 ACE_Write_Guard<ACE_LOCK>::acquire (void)
00099 {
00100   return this->owner_ = this->lock_->acquire_write ();
00101 }
00102 
00103 template <class ACE_LOCK> ACE_INLINE int
00104 ACE_Write_Guard<ACE_LOCK>::tryacquire_write (void)
00105 {
00106   return this->owner_ = this->lock_->tryacquire_write ();
00107 }
00108 
00109 template <class ACE_LOCK> ACE_INLINE int
00110 ACE_Write_Guard<ACE_LOCK>::tryacquire (void)
00111 {
00112   return this->owner_ = this->lock_->tryacquire_write ();
00113 }
00114 
00115 template <class ACE_LOCK> ACE_INLINE
00116 ACE_Write_Guard<ACE_LOCK>::ACE_Write_Guard (ACE_LOCK &m,
00117                                             int block)
00118   : ACE_Guard<ACE_LOCK> (&m)
00119 {
00120   if (block)
00121     this->acquire_write ();
00122   else
00123     this->tryacquire_write ();
00124 }
00125 
00126 template <class ACE_LOCK> ACE_INLINE int
00127 ACE_Read_Guard<ACE_LOCK>::acquire_read (void)
00128 {
00129   return this->owner_ = this->lock_->acquire_read ();
00130 }
00131 
00132 template <class ACE_LOCK> ACE_INLINE int
00133 ACE_Read_Guard<ACE_LOCK>::acquire (void)
00134 {
00135   return this->owner_ = this->lock_->acquire_read ();
00136 }
00137 
00138 template <class ACE_LOCK> ACE_INLINE int
00139 ACE_Read_Guard<ACE_LOCK>::tryacquire_read (void)
00140 {
00141   return this->owner_ = this->lock_->tryacquire_read ();
00142 }
00143 
00144 template <class ACE_LOCK> ACE_INLINE int
00145 ACE_Read_Guard<ACE_LOCK>::tryacquire (void)
00146 {
00147   return this->owner_ = this->lock_->tryacquire_read ();
00148 }
00149 
00150 template <class ACE_LOCK> ACE_INLINE
00151 ACE_Read_Guard<ACE_LOCK>::ACE_Read_Guard (ACE_LOCK &m)
00152   : ACE_Guard<ACE_LOCK> (&m)
00153 {
00154   this->acquire_read ();
00155 }
00156 
00157 template <class ACE_LOCK> ACE_INLINE
00158 ACE_Read_Guard<ACE_LOCK>::ACE_Read_Guard (ACE_LOCK &m,
00159                                           int block)
00160   : ACE_Guard<ACE_LOCK> (&m)
00161 {
00162   if (block)
00163     this->acquire_read ();
00164   else
00165     this->tryacquire_read ();
00166 }
00167 
00168 template <class ACE_LOCKING_MECHANISM> ACE_INLINE
00169 ACE_Lock_Adapter<ACE_LOCKING_MECHANISM>::ACE_Lock_Adapter (ACE_LOCKING_MECHANISM &lock)
00170   : lock_ (&lock),
00171     delete_lock_ (0)
00172 {
00173 }
00174 
00175 template <class ACE_LOCKING_MECHANISM> ACE_INLINE
00176 ACE_Lock_Adapter<ACE_LOCKING_MECHANISM>::~ACE_Lock_Adapter (void)
00177 {
00178   if (this->delete_lock_)
00179     delete this->lock_;
00180 }
00181 
00182 // Explicitly destroy the lock.
00183 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00184 ACE_Lock_Adapter<ACE_LOCKING_MECHANISM>::remove (void)
00185 {
00186   return this->lock_->remove ();
00187 }
00188 
00189 // Block the thread until the lock is acquired.
00190 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00191 ACE_Lock_Adapter<ACE_LOCKING_MECHANISM>::acquire (void)
00192 {
00193   return this->lock_->acquire ();
00194 }
00195 
00196 // Conditionally acquire the lock (i.e., won't block).
00197 
00198 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00199 ACE_Lock_Adapter<ACE_LOCKING_MECHANISM>::tryacquire (void)
00200 {
00201   return this->lock_->tryacquire ();
00202 }
00203 
00204 // Release the lock.
00205 
00206 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00207 ACE_Lock_Adapter<ACE_LOCKING_MECHANISM>::release (void)
00208 {
00209   return this->lock_->release ();
00210 }
00211 
00212 // Block until the thread acquires a read lock.  If the locking
00213 // mechanism doesn't support read locks then this just calls
00214 // <acquire>.
00215 
00216 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00217 ACE_Lock_Adapter<ACE_LOCKING_MECHANISM>::acquire_read (void)
00218 {
00219   return this->lock_->acquire_read ();
00220 }
00221 
00222 // Block until the thread acquires a write lock.  If the locking
00223 // mechanism doesn't support read locks then this just calls
00224 // <acquire>.
00225 
00226 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00227 ACE_Lock_Adapter<ACE_LOCKING_MECHANISM>::acquire_write (void)
00228 {
00229   return this->lock_->acquire_write ();
00230 }
00231 
00232 // Conditionally acquire a read lock.  If the locking mechanism
00233 // doesn't support read locks then this just calls <acquire>.
00234 
00235 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00236 ACE_Lock_Adapter<ACE_LOCKING_MECHANISM>::tryacquire_read (void)
00237 {
00238   return this->lock_->tryacquire_read ();
00239 }
00240 
00241 // Conditionally acquire a write lock.  If the locking mechanism
00242 // doesn't support write locks then this just calls <acquire>.
00243 
00244 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00245 ACE_Lock_Adapter<ACE_LOCKING_MECHANISM>::tryacquire_write (void)
00246 {
00247   return this->lock_->tryacquire_write ();
00248 }
00249 
00250 // Conditionally try to upgrade a lock held for read to a write lock.
00251 // If the locking mechanism doesn't support read locks then this just
00252 // calls <acquire>. Returns 0 on success, -1 on failure.
00253 
00254 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00255 ACE_Lock_Adapter<ACE_LOCKING_MECHANISM>::tryacquire_write_upgrade (void)
00256 {
00257   return this->lock_->tryacquire_write_upgrade ();
00258 }
00259 
00260 template <class ACE_LOCKING_MECHANISM> ACE_INLINE
00261 ACE_Reverse_Lock<ACE_LOCKING_MECHANISM>::ACE_Reverse_Lock (ACE_LOCKING_MECHANISM &lock,
00262                                                            ACE_Acquire_Method::METHOD_TYPE acquire_method)
00263   : lock_ (lock),
00264     acquire_method_ (acquire_method)
00265 {
00266 }
00267 
00268 // Explicitly destroy the lock.
00269 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00270 ACE_Reverse_Lock<ACE_LOCKING_MECHANISM>::remove (void)
00271 {
00272   return this->lock_.remove ();
00273 }
00274 
00275 // Release the lock.
00276 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00277 ACE_Reverse_Lock<ACE_LOCKING_MECHANISM>::acquire (void)
00278 {
00279   return this->lock_.release ();
00280 }
00281 
00282 // Release the lock.
00283 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00284 ACE_Reverse_Lock<ACE_LOCKING_MECHANISM>::tryacquire (void)
00285 {
00286   ACE_NOTSUP_RETURN (-1);
00287 }
00288 
00289 // Acquire the lock.
00290 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00291 ACE_Reverse_Lock<ACE_LOCKING_MECHANISM>::release (void)
00292 {
00293   if (this->acquire_method_ == ACE_Acquire_Method::ACE_READ)
00294     return this->lock_.acquire_read ();
00295   else if (this->acquire_method_ == ACE_Acquire_Method::ACE_WRITE)
00296     return this->lock_.acquire_write ();
00297   else
00298     return this->lock_.acquire ();
00299 }
00300 
00301 // Release the lock.
00302 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00303 ACE_Reverse_Lock<ACE_LOCKING_MECHANISM>::acquire_read (void)
00304 {
00305   ACE_NOTSUP_RETURN (-1);
00306 }
00307 
00308 // Release the lock.
00309 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00310 ACE_Reverse_Lock<ACE_LOCKING_MECHANISM>::acquire_write (void)
00311 {
00312   ACE_NOTSUP_RETURN (-1);
00313 }
00314 
00315 // Release the lock.
00316 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00317 ACE_Reverse_Lock<ACE_LOCKING_MECHANISM>::tryacquire_read (void)
00318 {
00319   ACE_NOTSUP_RETURN (-1);
00320 }
00321 
00322 // Release the lock.
00323 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00324 ACE_Reverse_Lock<ACE_LOCKING_MECHANISM>::tryacquire_write (void)
00325 {
00326   ACE_NOTSUP_RETURN (-1);
00327 }
00328 
00329 // Release the lock.
00330 template <class ACE_LOCKING_MECHANISM> ACE_INLINE int
00331 ACE_Reverse_Lock<ACE_LOCKING_MECHANISM>::tryacquire_write_upgrade (void)
00332 {
00333   ACE_NOTSUP_RETURN (-1);
00334 }
00335 
00336 #if defined (ACE_HAS_THREADS)
00337 
00338 template<class MUTEX> ACE_INLINE int
00339 ACE_Condition<MUTEX>::remove (void)
00340 {
00341   // ACE_TRACE ("ACE_Condition<MUTEX>::remove");
00342 
00343   // cond_destroy() is called in a loop if the condition variable is
00344   // BUSY.  This avoids a condition where a condition is signaled and
00345   // because of some timing problem, the thread that is to be signaled
00346   // has called the cond_wait routine after the signal call.  Since
00347   // the condition signal is not queued in any way, deadlock occurs.
00348 
00349   int result = 0;
00350 
00351 #if defined (CHORUS)
00352   // Are we the owner?
00353   if (this->process_cond_ && this->condname_)
00354     {
00355       // Only destroy the condition if we're the ones who initialized
00356       // it.
00357       while ((result = ACE_OS::cond_destroy (this->process_cond_)) == -1
00358              && errno == EBUSY)
00359         {
00360           ACE_OS::cond_broadcast (this->process_cond_);
00361           ACE_OS::thr_yield ();
00362         }
00363       ACE_OS::munmap (this->process_cond_,
00364                       sizeof (ACE_cond_t));
00365       ACE_OS::shm_unlink (this->condname_);
00366       ACE_OS::free (ACE_static_cast (void *,
00367                                      ACE_const_cast (ACE_TCHAR *,
00368                                                      this->condname_)));
00369     }
00370   else if (this->process_cond_)
00371     {
00372       ACE_OS::munmap (this->process_cond_,
00373                       sizeof (ACE_cond_t));
00374       result = 0;
00375     }
00376   else
00377 #endif /* CHORUS */
00378 
00379     while ((result = ACE_OS::cond_destroy (&this->cond_)) == -1
00380            && errno == EBUSY)
00381       {
00382         ACE_OS::cond_broadcast (&this->cond_);
00383         ACE_OS::thr_yield ();
00384       }
00385 
00386   return result;
00387 }
00388 
00389 template<class MUTEX> ACE_INLINE MUTEX &
00390 ACE_Condition<MUTEX>::mutex (void)
00391 {
00392   // ACE_TRACE ("ACE_Condition<MUTEX>::mutex");
00393   return this->mutex_;
00394 }
00395 
00396 template <class MUTEX> ACE_INLINE int
00397 ACE_Condition<MUTEX>::signal (void)
00398 {
00399 // ACE_TRACE ("ACE_Condition<MUTEX>::signal");
00400 #if defined (CHORUS)
00401   if (this->process_cond_ != 0)
00402     return ACE_OS::cond_signal (this->process_cond_);
00403 #endif /* CHORUS */
00404   return ACE_OS::cond_signal (&this->cond_);
00405 }
00406 
00407 template <class MUTEX> ACE_INLINE int
00408 ACE_Condition<MUTEX>::broadcast (void)
00409 {
00410 // ACE_TRACE ("ACE_Condition<MUTEX>::broadcast");
00411 #if defined (CHORUS)
00412   if (this->process_cond_ != 0)
00413     return ACE_OS::cond_broadcast (this->process_cond_);
00414 #endif /* CHORUS */
00415   return ACE_OS::cond_broadcast (&this->cond_);
00416 }
00417 
00418 #endif /* ACE_HAS_THREADS */
00419 
00420 #if !(defined (ACE_HAS_THREADS) && (defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || defined (ACE_HAS_TSS_EMULATION)))
00421 template <class TYPE> ACE_INLINE
00422 ACE_TSS<TYPE>::ACE_TSS (TYPE *type)
00423   : type_ (type)
00424 {
00425 }
00426 
00427 template <class TYPE> ACE_INLINE int
00428 ACE_TSS<TYPE>::ts_init (void) const
00429 {
00430   return 0;
00431 }
00432 
00433 template <class TYPE> ACE_INLINE TYPE *
00434 ACE_TSS<TYPE>::ts_object (void) const
00435 {
00436   return this->type_;
00437 }
00438 
00439 template <class TYPE> ACE_INLINE TYPE *
00440 ACE_TSS<TYPE>::ts_object (TYPE *type)
00441 {
00442   this->type_ = type;
00443   return this->type_;
00444 }
00445 
00446 template <class TYPE> ACE_INLINE TYPE *
00447 ACE_TSS<TYPE>::ts_get (void) const
00448 {
00449   return this->type_;
00450 }
00451 
00452 #endif /* ! (defined (ACE_HAS_THREADS) && (defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || defined (ACE_HAS_TSS_EMULATION))) */

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