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

Synch.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 #ifndef ACE_SYNCH_C
00003 #define ACE_SYNCH_C
00004 
00005 #include "ace/Thread.h"
00006 #include "ace/ACE.h"
00007 
00008 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00009 # pragma once
00010 #endif /* ACE_LACKS_PRAGMA_ONCE */
00011 
00012 #include "ace/Synch.h"
00013 #include "ace/Log_Msg.h"
00014 
00015 
00016 ACE_RCSID (ace,
00017            Synch,
00018            "$Id: Synch.cpp,v 1.1.1.4.2.2 2003/05/06 16:52:25 chad Exp $")
00019 
00020 
00021 #if !defined (__ACE_INLINE__)
00022 #include "ace/Synch.i"
00023 #endif /* __ACE_INLINE__ */
00024 
00025 
00026 ACE_ALLOC_HOOK_DEFINE(ACE_Null_Mutex)
00027 
00028 ACE_Lock::~ACE_Lock (void)
00029 {
00030 }
00031 
00032 ACE_Adaptive_Lock::ACE_Adaptive_Lock (void)
00033   : lock_ (0)
00034 {
00035 }
00036 
00037 ACE_Adaptive_Lock::~ACE_Adaptive_Lock (void)
00038 {
00039 }
00040 
00041 int
00042 ACE_Adaptive_Lock::remove (void)
00043 {
00044   return this->lock_->remove ();
00045 }
00046 
00047 int
00048 ACE_Adaptive_Lock::acquire (void)
00049 {
00050   return this->lock_->acquire ();
00051 }
00052 
00053 int
00054 ACE_Adaptive_Lock::tryacquire (void)
00055 {
00056   return this->lock_->tryacquire ();
00057 }
00058 
00059 int
00060 ACE_Adaptive_Lock::release (void)
00061 {
00062   return this->lock_->release ();
00063 }
00064 
00065 int
00066 ACE_Adaptive_Lock::acquire_read (void)
00067 {
00068   return this->lock_->acquire_read ();
00069 }
00070 
00071 int
00072 ACE_Adaptive_Lock::acquire_write (void)
00073 {
00074   return this->lock_->acquire_write ();
00075 }
00076 
00077 int
00078 ACE_Adaptive_Lock::tryacquire_read (void)
00079 {
00080   return this->lock_->tryacquire_read ();
00081 }
00082 
00083 int
00084 ACE_Adaptive_Lock::tryacquire_write (void)
00085 {
00086   return this->lock_->tryacquire_write ();
00087 }
00088 
00089 int
00090 ACE_Adaptive_Lock::tryacquire_write_upgrade (void)
00091 {
00092   return this->lock_->tryacquire_write_upgrade ();
00093 }
00094 
00095 void
00096 ACE_Adaptive_Lock::dump (void) const
00097 {
00098   //  return this->lock_->dump ();
00099 }
00100 
00101 ACE_TSS_Adapter::ACE_TSS_Adapter (void *object, ACE_THR_DEST f)
00102   : ts_obj_ (object),
00103     func_ (f)
00104 {
00105   // ACE_TRACE ("ACE_TSS_Adapter::ACE_TSS_Adapter");
00106 }
00107 
00108 void
00109 ACE_TSS_Adapter::cleanup (void)
00110 {
00111   // ACE_TRACE ("ACE_TSS_Adapter::cleanup");
00112   (*this->func_)(this->ts_obj_);  // call cleanup routine for ts_obj_
00113 }
00114 
00115 extern "C" void
00116 ACE_TSS_C_cleanup (void *object)
00117 {
00118   // ACE_TRACE ("ACE_TSS_C_cleanup");
00119   if (object != 0)
00120     {
00121       ACE_TSS_Adapter *tss_adapter = (ACE_TSS_Adapter *) object;
00122       // Perform cleanup on the real TS object.
00123       tss_adapter->cleanup ();
00124       // Delete the adapter object.
00125       delete tss_adapter;
00126     }
00127 }
00128 
00129 ACE_ALLOC_HOOK_DEFINE(ACE_Semaphore)
00130 
00131 void
00132 ACE_Semaphore::dump (void) const
00133 {
00134 // ACE_TRACE ("ACE_Semaphore::dump");
00135 
00136   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00137   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00138   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00139 }
00140 
00141 ACE_Semaphore::ACE_Semaphore (u_int count,
00142                               int type,
00143                               const ACE_TCHAR *name,
00144                               void *arg,
00145                               int max)
00146   : removed_ (0)
00147 {
00148 // ACE_TRACE ("ACE_Semaphore::ACE_Semaphore");
00149 #if defined(ACE_LACKS_UNNAMED_SEMAPHORE)
00150 // if the user does not provide a name, we generate a unique name here
00151   ACE_TCHAR iname[ACE_UNIQUE_NAME_LEN];
00152   if (name == 0) 
00153     ACE::unique_name (this, iname, ACE_UNIQUE_NAME_LEN);
00154   if (ACE_OS::sema_init (&this->semaphore_, count, type,
00155                          name ? name : iname, 
00156                          arg, max) != 0)
00157 #else
00158   if (ACE_OS::sema_init (&this->semaphore_, count, type,
00159                          name, arg, max) != 0)
00160 #endif
00161     ACE_ERROR ((LM_ERROR,
00162                 ACE_LIB_TEXT ("%p\n"),
00163                 ACE_LIB_TEXT ("ACE_Semaphore::ACE_Semaphore")));
00164 }
00165 
00166 ACE_Semaphore::~ACE_Semaphore (void)
00167 {
00168 // ACE_TRACE ("ACE_Semaphore::~ACE_Semaphore");
00169 
00170   this->remove ();
00171 }
00172 
00173 ACE_ALLOC_HOOK_DEFINE(ACE_Mutex)
00174 
00175 void
00176 ACE_Mutex::dump (void) const
00177 {
00178 // ACE_TRACE ("ACE_Mutex::dump");
00179 
00180   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00181 #if defined (CHORUS)
00182   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("lockname_ = %s\n"), this->lockname_));
00183   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("process_lock_ = %x\n"), this->process_lock_));
00184 #endif /* CHORUS */
00185   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00186   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00187 }
00188 
00189 ACE_Mutex::ACE_Mutex (int type, const ACE_TCHAR *name,
00190                       ACE_mutexattr_t *arg, mode_t mode)
00191   :
00192 #if defined (CHORUS) || defined (ACE_HAS_PTHREADS) || defined(ACE_HAS_STHREADS)
00193     process_lock_ (0),
00194     lockname_ (0),
00195 #endif /* CHORUS */
00196     removed_ (0)
00197 {
00198   // ACE_TRACE ("ACE_Mutex::ACE_Mutex");
00199 
00200   // These platforms need process-wide mutex to be in shared memory.
00201 #if defined (CHORUS) || defined(ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS)
00202   if (type == USYNC_PROCESS)
00203     {
00204       // Let's see if the shared memory entity already exists.
00205       ACE_HANDLE fd = ACE_OS::shm_open (name, O_RDWR | O_CREAT | O_EXCL, mode);
00206       if (fd == ACE_INVALID_HANDLE)
00207         {
00208           if (errno == EEXIST)
00209             fd = ACE_OS::shm_open (name, O_RDWR | O_CREAT, mode);
00210           else
00211             return;
00212         }
00213       else
00214         {
00215           // We own this shared memory object!  Let's set its size.
00216           if (ACE_OS::ftruncate (fd,
00217                                  sizeof (ACE_mutex_t)) == -1)
00218             {
00219               ACE_OS::close (fd);
00220               return;
00221             }
00222           this->lockname_ = ACE_OS::strdup (name);
00223           if (this->lockname_ == 0)
00224             {
00225               ACE_OS::close (fd);
00226               return;
00227             }
00228         }
00229 
00230       this->process_lock_ =
00231         (ACE_mutex_t *) ACE_OS::mmap (0,
00232                                       sizeof (ACE_mutex_t),
00233                                       PROT_RDWR,
00234                                       MAP_SHARED,
00235                                       fd,
00236                                       0);
00237       ACE_OS::close (fd);
00238       if (this->process_lock_ == MAP_FAILED)
00239         return;
00240 
00241       if (this->lockname_
00242           && ACE_OS::mutex_init (this->process_lock_,
00243                                  type,
00244                                  name,
00245                                  arg) != 0)
00246         return;
00247     }
00248    // It is ok to fall through into the <mutex_init> below if the
00249    // USYNC_PROCESS flag is not enabled.
00250 #else
00251   ACE_UNUSED_ARG (mode);
00252 #endif /* CHORUS */
00253 
00254   if (ACE_OS::mutex_init (&this->lock_,
00255                           type,
00256                           name,
00257                           arg) != 0)
00258     ACE_ERROR ((LM_ERROR,
00259                 ACE_LIB_TEXT ("%p\n"),
00260                 ACE_LIB_TEXT ("ACE_Mutex::ACE_Mutex")));
00261 }
00262 
00263 ACE_Mutex::~ACE_Mutex (void)
00264 {
00265 // ACE_TRACE ("ACE_Mutex::~ACE_Mutex");
00266   this->remove ();
00267 }
00268 
00269 ACE_Event::ACE_Event (int manual_reset,
00270                       int initial_state,
00271                       int type,
00272                       const ACE_TCHAR *name,
00273                       void *arg)
00274   : removed_ (0)
00275 {
00276   if (ACE_OS::event_init (&this->handle_,
00277                           manual_reset,
00278                           initial_state,
00279                           type,
00280                           name,
00281                           arg) != 0)
00282     ACE_ERROR ((LM_ERROR,
00283                 ACE_LIB_TEXT ("%p\n"),
00284                 ACE_LIB_TEXT ("ACE_Event::ACE_Event")));
00285 }
00286 
00287 ACE_Event::~ACE_Event (void)
00288 {
00289   this->remove ();
00290 }
00291 
00292 int
00293 ACE_Event::remove (void)
00294 {
00295   int result = 0;
00296   if (this->removed_ == 0)
00297     {
00298       this->removed_ = 1;
00299       result = ACE_OS::event_destroy (&this->handle_);
00300     }
00301   return result;
00302 }
00303 
00304 ACE_event_t
00305 ACE_Event::handle (void) const
00306 {
00307   return this->handle_;
00308 }
00309 
00310 void
00311 ACE_Event::handle (ACE_event_t new_handle)
00312 {
00313   this->handle_ = new_handle;
00314 }
00315 
00316 int
00317 ACE_Event::wait (void)
00318 {
00319   return ACE_OS::event_wait (&this->handle_);
00320 }
00321 
00322 int
00323 ACE_Event::wait (const ACE_Time_Value *abstime, int use_absolute_time)
00324 {
00325   return ACE_OS::event_timedwait (&this->handle_,
00326                                   (ACE_Time_Value *) abstime,
00327                                   use_absolute_time);
00328 }
00329 
00330 int
00331 ACE_Event::signal (void)
00332 {
00333   return ACE_OS::event_signal (&this->handle_);
00334 }
00335 
00336 int
00337 ACE_Event::pulse (void)
00338 {
00339   return ACE_OS::event_pulse (&this->handle_);
00340 }
00341 
00342 int
00343 ACE_Event::reset (void)
00344 {
00345   return ACE_OS::event_reset (&this->handle_);
00346 }
00347 
00348 void
00349 ACE_Event::dump (void) const
00350 {
00351   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00352   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00353 }
00354 
00355 ACE_Manual_Event::ACE_Manual_Event (int initial_state,
00356                                     int type,
00357                                     const char *name,
00358                                     void *arg)
00359   : ACE_Event (1,
00360                initial_state,
00361                type,
00362                ACE_TEXT_CHAR_TO_TCHAR (name),
00363                arg)
00364 {
00365 }
00366 
00367 #if defined (ACE_HAS_WCHAR)
00368 ACE_Manual_Event::ACE_Manual_Event (int initial_state,
00369                                     int type,
00370                                     const wchar_t *name,
00371                                     void *arg)
00372   : ACE_Event (1,
00373                initial_state,
00374                type,
00375                ACE_TEXT_WCHAR_TO_TCHAR (name),
00376                arg)
00377 {
00378 }
00379 #endif /* ACE_HAS_WCHAR */
00380 
00381 void
00382 ACE_Manual_Event::dump (void) const
00383 {
00384   ACE_Event::dump ();
00385 }
00386 
00387 ACE_Auto_Event::ACE_Auto_Event (int initial_state,
00388                                 int type,
00389                                 const char *name,
00390                                 void *arg)
00391   : ACE_Event (0,
00392                initial_state,
00393                type,
00394                ACE_TEXT_CHAR_TO_TCHAR (name),
00395                arg)
00396 {
00397 }
00398 
00399 #if defined (ACE_HAS_WCHAR)
00400 ACE_Auto_Event::ACE_Auto_Event (int initial_state,
00401                                 int type,
00402                                 const wchar_t *name,
00403                                 void *arg)
00404   : ACE_Event (0,
00405                initial_state,
00406                type,
00407                ACE_TEXT_WCHAR_TO_TCHAR (name),
00408                arg)
00409 {
00410 }
00411 #endif /* ACE_HAS_WCHAR */
00412 
00413 void
00414 ACE_Auto_Event::dump (void) const
00415 {
00416   ACE_Event::dump ();
00417 }
00418 
00419 #if defined (ACE_HAS_THREADS)
00420 
00421 ACE_ALLOC_HOOK_DEFINE(ACE_Thread_Mutex_Guard)
00422 
00423 void
00424 ACE_Thread_Semaphore::dump (void) const
00425 {
00426 // ACE_TRACE ("ACE_Thread_Semaphore::dump");
00427 
00428   ACE_Semaphore::dump ();
00429 }
00430 
00431 ACE_Thread_Semaphore::ACE_Thread_Semaphore (u_int count,
00432                                             const ACE_TCHAR *name,
00433                                             void *arg,
00434                                             int max)
00435   : ACE_Semaphore (count, USYNC_THREAD, name, arg, max)
00436 {
00437 // ACE_TRACE ("ACE_Thread_Semaphore::ACE_Thread_Semaphore");
00438 }
00439 
00440 #if defined (ACE_USES_OBSOLETE_GUARD_CLASSES)
00441 void
00442 ACE_Thread_Mutex_Guard::dump (void) const
00443 {
00444 // ACE_TRACE ("ACE_Thread_Mutex_Guard::dump");
00445 
00446   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00447   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00448   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00449 }
00450 #endif /* ACE_USES_OBSOLETE_GUARD_CLASSES */
00451 
00452 ACE_Recursive_Thread_Mutex::ACE_Recursive_Thread_Mutex (const ACE_TCHAR *name,
00453                                                         ACE_mutexattr_t *arg)
00454   : removed_ (0)
00455 {
00456   // ACE_TRACE ("ACE_Recursive_Thread_Mutex::ACE_Recursive_Thread_Mutex");
00457 #if defined (ACE_HAS_FSU_PTHREADS) && ! defined (ACE_WIN32)
00458   // Initialize FSU pthreads package.  If called more than once,
00459   // pthread_init does nothing and so does no harm.
00460    pthread_init ();
00461 #endif  /*  ACE_HAS_FSU_PTHREADS && ! ACE_WIN32 */
00462    if (ACE_OS::recursive_mutex_init (&this->recursive_mutex_,
00463                                      name,
00464                                      arg) == -1)
00465      ACE_ERROR ((LM_ERROR,
00466                  ACE_LIB_TEXT ("%p\n"),
00467                  ACE_LIB_TEXT ("recursive_mutex_init")));
00468 }
00469 
00470 ACE_ALLOC_HOOK_DEFINE(ACE_Recursive_Thread_Mutex)
00471 
00472 ACE_Recursive_Thread_Mutex::~ACE_Recursive_Thread_Mutex (void)
00473 {
00474   // ACE_TRACE ("ACE_Recursive_Thread_Mutex::~ACE_Recursive_Thread_Mutex");
00475   this->remove ();
00476 }
00477 
00478 int
00479 ACE_Recursive_Thread_Mutex::remove (void)
00480 {
00481 // ACE_TRACE ("ACE_Recursive_Thread_Mutex::remove");
00482   int result = 0;
00483   if (this->removed_ == 0)
00484     {
00485       this->removed_ = 1;
00486       result = ACE_OS::recursive_mutex_destroy (&this->recursive_mutex_);
00487     }
00488   return result;
00489 }
00490 
00491 // The counter part of the following two functions for Win32 are
00492 // located in file Synch.i
00493 ACE_thread_t
00494 ACE_Recursive_Thread_Mutex::get_thread_id (void)
00495 {
00496   // ACE_TRACE ("ACE_Recursive_Thread_Mutex::get_thread_id");
00497 #if defined (ACE_HAS_RECURSIVE_MUTEXES)
00498   // @@ The structure CriticalSection in Win32 doesn't hold the thread
00499   // handle of the thread that owns the lock.  However it is still not
00500   // clear at this point how to translate a thread handle to its
00501   // corresponding thread id.
00502   errno = ENOTSUP;
00503   return ACE_OS::NULL_thread;
00504 #else
00505   ACE_thread_t owner_id;
00506   ACE_OS::mutex_lock (&this->recursive_mutex_.nesting_mutex_);
00507   owner_id = this->recursive_mutex_.owner_id_;
00508   ACE_OS::mutex_unlock (&this->recursive_mutex_.nesting_mutex_);
00509   return owner_id;
00510 #endif /* ACE_WIN32 */
00511 }
00512 
00513 int
00514 ACE_Recursive_Thread_Mutex::get_nesting_level (void)
00515 {
00516   // ACE_TRACE ("ACE_Recursive_Thread_Mutex::get_nesting_level");
00517 #if defined (ACE_HAS_WINCE) || defined (VXWORKS) || defined (ACE_PSOS)
00518   ACE_NOTSUP_RETURN (-1);
00519 #elif defined (ACE_HAS_RECURSIVE_MUTEXES)
00520   // Nothing inside of a CRITICAL_SECTION object should ever be
00521   // accessed directly.  It is documented to change at any time.
00522 # if defined (ACE_WIN64)
00523   // Things are different on Windows XP 64-bit
00524   return this->recursive_mutex_.LockCount + 1;
00525 # elif defined (ACE_WIN32)
00526   // This is really a Win32-ism...
00527   return this->recursive_mutex_.RecursionCount;
00528 # else
00529   ACE_NOTSUP_RETURN (-1);
00530 # endif /* ACE_HAS_RECURSIVE_MUTEXES */
00531 #else
00532   int nesting_level = 0;
00533   ACE_OS::mutex_lock (&this->recursive_mutex_.nesting_mutex_);
00534   nesting_level = this->recursive_mutex_.nesting_level_;
00535   ACE_OS::mutex_unlock (&this->recursive_mutex_.nesting_mutex_);
00536   return nesting_level;
00537 #endif /* !ACE_HAS_WINCE */
00538 }
00539 
00540 ACE_Recursive_Thread_Mutex::ACE_Recursive_Thread_Mutex (const ACE_Recursive_Thread_Mutex &)
00541 {
00542 }
00543 
00544 int
00545 ACE_Recursive_Thread_Mutex::acquire (void)
00546 {
00547   return ACE_OS::recursive_mutex_lock (&this->recursive_mutex_);
00548 }
00549 
00550 int
00551 ACE_Recursive_Thread_Mutex::release (void)
00552 {
00553   return ACE_OS::recursive_mutex_unlock (&this->recursive_mutex_);
00554 }
00555 
00556 int
00557 ACE_Recursive_Thread_Mutex::tryacquire (void)
00558 {
00559   return ACE_OS::recursive_mutex_trylock (&this->recursive_mutex_);
00560 }
00561 
00562 void
00563 ACE_Recursive_Thread_Mutex::dump (void) const
00564 {
00565 // ACE_TRACE ("ACE_Recursive_Thread_Mutex::dump");
00566 
00567   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00568   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00569 }
00570 
00571 ACE_ALLOC_HOOK_DEFINE(ACE_Condition_Thread_Mutex)
00572 
00573 void
00574 ACE_Condition_Thread_Mutex::dump (void) const
00575 {
00576 // ACE_TRACE ("ACE_Condition_Thread_Mutex::dump");
00577 
00578   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00579   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00580 #if defined (ACE_WIN32)
00581   ACE_DEBUG ((LM_DEBUG,
00582               ACE_LIB_TEXT ("waiters = %d\n"),
00583               this->cond_.waiters ()));
00584 #endif /* ACE_WIN32 */
00585   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00586 }
00587 
00588 ACE_Condition_Thread_Mutex::ACE_Condition_Thread_Mutex (const ACE_Thread_Mutex &m,
00589                                                         const ACE_TCHAR *name,
00590                                                         void *arg)
00591   : mutex_ ((ACE_Thread_Mutex &) m),
00592     removed_ (0)
00593 {
00594 #if defined (ACE_HAS_FSU_PTHREADS)
00595 //      Initialize FSU pthreads package.
00596 //      If called more than once, pthread_init does nothing
00597 //      and so does no harm.
00598    pthread_init ();
00599 #endif  /*  ACE_HAS_FSU_PTHREADS */
00600 
00601 // ACE_TRACE ("ACE_Condition_Thread_Mutex::ACE_Condition_Thread_Mutex");
00602   if (ACE_OS::cond_init (&this->cond_,
00603                          (short) USYNC_THREAD,
00604                          name,
00605                          arg) != 0)
00606     ACE_ERROR ((LM_ERROR,
00607                 ACE_LIB_TEXT ("%p\n"),
00608                 ACE_LIB_TEXT ("ACE_Condition_Thread_Mutex::ACE_Condition_Thread_Mutex")));
00609 }
00610 
00611 
00612 ACE_Condition_Thread_Mutex::ACE_Condition_Thread_Mutex (const ACE_Thread_Mutex &m,
00613                             ACE_Condition_Attributes &attributes,
00614                             const ACE_TCHAR *name,
00615                             void *arg)
00616   : mutex_ ((ACE_Thread_Mutex &) m),
00617     removed_ (0)
00618 {
00619 #if defined (ACE_HAS_FSU_PTHREADS)
00620 //      Initialize FSU pthreads package.
00621 //      If called more than once, pthread_init does nothing
00622 //      and so does no harm.
00623    pthread_init ();
00624 #endif  /*  ACE_HAS_FSU_PTHREADS */
00625 
00626 // ACE_TRACE ("ACE_Condition_Thread_Mutex::ACE_Condition_Thread_Mutex");
00627   if (ACE_OS::cond_init (&this->cond_, attributes.attributes_,
00628                          name, arg) != 0)
00629     ACE_ERROR ((LM_ERROR, ACE_LIB_TEXT ("%p\n"),
00630                 ACE_LIB_TEXT ("ACE_Condition_Thread_Mutex::ACE_Condition_Thread_Mutex")));
00631 }
00632 
00633 ACE_Condition_Thread_Mutex::~ACE_Condition_Thread_Mutex (void)
00634 {
00635 // ACE_TRACE ("ACE_Condition_Thread_Mutex::~ACE_Condition_Thread_Mutex");
00636   this->remove ();
00637 }
00638 
00639 // Peform an "alertable" timed wait.  If the argument <abstime> == 0
00640 // then we do a regular <cond_wait>, else we do a timed wait for up to
00641 // <abstime> using the <cond_timedwait> function.
00642 
00643 int
00644 ACE_Condition_Thread_Mutex::wait (void)
00645 {
00646 // ACE_TRACE ("ACE_Condition_Thread_Mutex::wait");
00647   return ACE_OS::cond_wait (&this->cond_, &this->mutex_.lock_);
00648 }
00649 
00650 int
00651 ACE_Condition_Thread_Mutex::wait (ACE_Thread_Mutex &mutex,
00652                                   const ACE_Time_Value *abstime)
00653 {
00654 // ACE_TRACE ("ACE_Condition_Thread_Mutex::wait");
00655   return ACE_OS::cond_timedwait (&this->cond_,
00656                                  &mutex.lock_,
00657                                  (ACE_Time_Value *) abstime);
00658 }
00659 
00660 int
00661 ACE_Condition_Thread_Mutex::wait (const ACE_Time_Value *abstime)
00662 {
00663 // ACE_TRACE ("ACE_Condition_Thread_Mutex::wait");
00664   return this->wait (this->mutex_, abstime);
00665 }
00666 
00667 int
00668 ACE_Condition_Thread_Mutex::signal (void)
00669 {
00670 // ACE_TRACE ("ACE_Condition_Thread_Mutex::signal");
00671   return ACE_OS::cond_signal (&this->cond_);
00672 }
00673 
00674 int
00675 ACE_Condition_Thread_Mutex::broadcast (void)
00676 {
00677 // ACE_TRACE ("ACE_Condition_Thread_Mutex::broadcast");
00678   return ACE_OS::cond_broadcast (&this->cond_);
00679 }
00680 
00681 ACE_ALLOC_HOOK_DEFINE(ACE_Sub_Barrier)
00682 
00683 void
00684 ACE_Sub_Barrier::dump (void) const
00685 {
00686 // ACE_TRACE ("ACE_Sub_Barrier::dump");
00687 
00688   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00689   this->barrier_finished_.dump ();
00690   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("running_threads_ = %d"), this->running_threads_));
00691   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00692   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00693 }
00694 
00695 ACE_Sub_Barrier::ACE_Sub_Barrier (u_int count,
00696                                   ACE_Thread_Mutex &lock,
00697                                   const ACE_TCHAR *name,
00698                                   void *arg)
00699   : barrier_finished_ (lock, name, arg),
00700     running_threads_ (count)
00701 {
00702 // ACE_TRACE ("ACE_Sub_Barrier::ACE_Sub_Barrier");
00703 }
00704 
00705 ACE_ALLOC_HOOK_DEFINE(ACE_Barrier)
00706 ACE_ALLOC_HOOK_DEFINE(ACE_Thread_Barrier)
00707 ACE_ALLOC_HOOK_DEFINE(ACE_Process_Barrier)
00708 
00709 void
00710 ACE_Barrier::dump (void) const
00711 {
00712 // ACE_TRACE ("ACE_Barrier::dump");
00713 
00714   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00715   this->lock_.dump ();
00716   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("current_generation_ = %d"), this->current_generation_));
00717   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ncount_ = %d"), this->count_));
00718   this->sub_barrier_1_.dump ();
00719   this->sub_barrier_2_.dump ();
00720   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00721 }
00722 
00723 ACE_Barrier::ACE_Barrier (u_int count,
00724                           const ACE_TCHAR *name,
00725                           void *arg)
00726   : lock_ (name, (ACE_mutexattr_t *) arg),
00727     current_generation_ (0),
00728     count_ (count),
00729     sub_barrier_1_ (count, lock_, name, arg),
00730     sub_barrier_2_ (count, lock_, name, arg)
00731 {
00732 // ACE_TRACE ("ACE_Barrier::ACE_Barrier");
00733   this->sub_barrier_[0] = &this->sub_barrier_1_;
00734   this->sub_barrier_[1] = &this->sub_barrier_2_;
00735 }
00736 
00737 int
00738 ACE_Barrier::wait (void)
00739 {
00740 // ACE_TRACE ("ACE_Barrier::wait");
00741   ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1);
00742 
00743   ACE_Sub_Barrier *sbp =
00744     this->sub_barrier_[this->current_generation_];
00745 
00746   // Check for shutdown...
00747   if (sbp == 0)
00748     return -1;
00749 
00750   if (sbp->running_threads_ == 1)
00751     {
00752       // We're the last running thread, so swap generations and tell
00753       // all the threads waiting on the barrier to continue on their
00754       // way.
00755 
00756       sbp->running_threads_ = this->count_;
00757       // Swap generations.
00758       this->current_generation_ = 1 - this->current_generation_;
00759       sbp->barrier_finished_.broadcast ();
00760     }
00761   else
00762     {
00763       --sbp->running_threads_;
00764 
00765       // Block until all the other threads wait().
00766       while (sbp->running_threads_ != this->count_)
00767         sbp->barrier_finished_.wait ();
00768     }
00769 
00770   return 0;
00771 }
00772 
00773 ACE_Thread_Barrier::ACE_Thread_Barrier (u_int count, const ACE_TCHAR *name)
00774   : ACE_Barrier (count, name)
00775 {
00776 // ACE_TRACE ("ACE_Thread_Barrier::ACE_Thread_Barrier");
00777 }
00778 
00779 void
00780 ACE_Thread_Barrier::dump (void) const
00781 {
00782 // ACE_TRACE ("ACE_Thread_Barrier::dump");
00783   ACE_Barrier::dump ();
00784 }
00785 
00786 #if 0
00787 ACE_Process_Barrier::ACE_Process_Barrier (u_int count, const ACE_TCHAR *name)
00788   : ACE_Barrier (count, USYNC_PROCESS, name)
00789 {
00790 // ACE_TRACE ("ACE_Process_Barrier::ACE_Process_Barrier");
00791 }
00792 
00793 void
00794 ACE_Process_Barrier::dump (void) const
00795 {
00796 // ACE_TRACE ("ACE_Process_Barrier::dump");
00797   ACE_Barrier::dump ();
00798 }
00799 
00800 template <class MUTEX> void
00801 ACE_Process_Condition<MUTEX>::dump (void) const
00802 {
00803 // ACE_TRACE ("ACE_Process_Condition<MUTEX>::dump");
00804 
00805   ACE_Condition<MUTEX>::dump ();
00806 }
00807 
00808 template <class MUTEX>
00809 ACE_Process_Condition<MUTEX>::ACE_Process_Condition (MUTEX &m,
00810                                                      const ACE_TCHAR *name,
00811                                                      void *arg)
00812   : ACE_Condition<MUTEX> (m, USYNC_PROCESS, name, arg)
00813 {
00814 // ACE_TRACE ("ACE_Process_Condition<MUTEX>::ACE_Process_Condition");
00815 }
00816 #endif /* 0 */
00817 
00818 ACE_ALLOC_HOOK_DEFINE(ACE_Thread_Mutex)
00819 
00820 void
00821 ACE_Thread_Mutex::dump (void) const
00822 {
00823 // ACE_TRACE ("ACE_Thread_Mutex::dump");
00824 
00825   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00826   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00827   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00828 }
00829 
00830 ACE_Thread_Mutex::~ACE_Thread_Mutex (void)
00831 {
00832 // ACE_TRACE ("ACE_Thread_Mutex::~ACE_Thread_Mutex");
00833   this->remove ();
00834 }
00835 
00836 ACE_Thread_Mutex::ACE_Thread_Mutex (const ACE_TCHAR *name, ACE_mutexattr_t *arg)
00837   : removed_ (0)
00838 {
00839 //  ACE_TRACE ("ACE_Thread_Mutex::ACE_Thread_Mutex");
00840 
00841   if (ACE_OS::thread_mutex_init (&this->lock_,
00842                                  USYNC_THREAD,
00843                                  name,
00844                                  arg) != 0)
00845     ACE_ERROR ((LM_ERROR,
00846                 ACE_LIB_TEXT ("%p\n"),
00847                 ACE_LIB_TEXT ("ACE_Thread_Mutex::ACE_Thread_Mutex")));
00848 }
00849 
00850 void
00851 ACE_RW_Mutex::dump (void) const
00852 {
00853 // ACE_TRACE ("ACE_RW_Mutex::dump");
00854 
00855   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00856   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00857   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00858 }
00859 
00860 ACE_RW_Mutex::ACE_RW_Mutex (int type, const ACE_TCHAR *name, void *arg)
00861   : removed_ (0)
00862 {
00863 // ACE_TRACE ("ACE_RW_Mutex::ACE_RW_Mutex");
00864   if (ACE_OS::rwlock_init (&this->lock_, type, name, arg) != 0)
00865     ACE_ERROR ((LM_ERROR,
00866                 ACE_LIB_TEXT ("%p\n"),
00867                 ACE_LIB_TEXT ("ACE_RW_Mutex::ACE_RW_Mutex")));
00868 }
00869 
00870 ACE_RW_Mutex::~ACE_RW_Mutex (void)
00871 {
00872 // ACE_TRACE ("ACE_RW_Mutex::~ACE_RW_Mutex");
00873   this->remove ();
00874 }
00875 
00876 ACE_ALLOC_HOOK_DEFINE(ACE_RW_Thread_Mutex)
00877 
00878 ACE_RW_Thread_Mutex::ACE_RW_Thread_Mutex (const ACE_TCHAR *name,
00879                                           void *arg)
00880   : ACE_RW_Mutex (USYNC_THREAD, name, arg)
00881 {
00882 // ACE_TRACE ("ACE_RW_Thread_Mutex::ACE_RW_Thread_Mutex");
00883 }
00884 
00885 void
00886 ACE_RW_Thread_Mutex::dump (void) const
00887 {
00888 // ACE_TRACE ("ACE_RW_Thread_Mutex::dump");
00889   ACE_RW_Mutex::dump ();
00890 }
00891 
00892 
00893 //ACE_TEMPLATE_METHOD_SPECIALIZATION
00894 int
00895 ACE_Condition<ACE_Recursive_Thread_Mutex>::remove (void)
00896 {
00897   return ACE_OS::cond_destroy (&this->cond_);
00898 }
00899 
00900 void
00901 ACE_Condition<ACE_Recursive_Thread_Mutex>::dump (void) const
00902 {
00903 // ACE_TRACE ("ACE_Condition<MUTEX>::dump");
00904 
00905   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00906   // No dump method for ACE_cond_t even in emulated mode. 
00907   // cond_.dump ();
00908   this->mutex_.dump ();
00909   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00910   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00911 }
00912 
00913 //ACE_TEMPLATE_METHOD_SPECIALIZATION
00914 ACE_Condition<ACE_Recursive_Thread_Mutex>::~ACE_Condition (void)
00915 {
00916   this->remove ();
00917 }
00918 
00919 //ACE_TEMPLATE_METHOD_SPECIALIZATION
00920 ACE_Condition<ACE_Recursive_Thread_Mutex>::ACE_Condition (ACE_Recursive_Thread_Mutex &m)
00921   : mutex_ (m)
00922 {
00923   ACE_OS::cond_init (&this->cond_);
00924 }
00925 
00926 //ACE_TEMPLATE_METHOD_SPECIALIZATION
00927 int
00928 ACE_Condition<ACE_Recursive_Thread_Mutex>::wait (const ACE_Time_Value *abstime)
00929 {
00930   return this->wait (this->mutex_, abstime);
00931 }
00932 
00933 //ACE_TEMPLATE_METHOD_SPECIALIZATION
00934 int
00935 ACE_Condition<ACE_Recursive_Thread_Mutex>::wait (ACE_Recursive_Thread_Mutex &mutex,
00936                                                  const ACE_Time_Value *abstime)
00937 {
00938   ACE_recursive_mutex_state mutex_state_holder;
00939   ACE_recursive_thread_mutex_t &recursive_mutex = mutex.mutex ();
00940 
00941   if (ACE_OS::recursive_mutex_cond_unlock (&recursive_mutex,
00942                                            mutex_state_holder) == -1)
00943     return -1;
00944 
00945   // We wait on the condition, specifying the nesting mutex. For platforms
00946   // with ACE_HAS_RECURSIVE_MUTEXES, this is the recursive mutex itself,
00947   // and is the same as recursive_mutex, above. The caller should have been
00948   // holding the lock on entry to this method, and it is still held.
00949   // For other platforms, this is the nesting mutex that guards the
00950   // ACE_recursive_mutex_t internals, and recursive_mutex_cond_unlock()
00951   // returned with the lock held, but waiters primed and waiting to be
00952   // released. At cond_wait below, the mutex will be released.
00953   // On return, it will be reacquired.
00954   const int result = abstime == 0
00955     ? ACE_OS::cond_wait (&this->cond_,
00956                          &mutex.get_nesting_mutex ())
00957     : ACE_OS::cond_timedwait (&this->cond_,
00958                               &mutex.get_nesting_mutex (),
00959                               (ACE_Time_Value *) abstime);
00960   // We are holding the mutex, whether the wait succeeded or failed.
00961   // Stash errno (in case it failed) and then we need to reset the
00962   // recursive mutex state to what it was on entry to this method.
00963   // Resetting it may require a wait for another thread to release
00964   // the ACE_recursive_thread_mutex_t if this is a platform without
00965   // ACE_HAS_RECURSIVE_MUTEXES, and recursive_mutex_cond_relock() takes
00966   // care of that.
00967   {
00968     ACE_Errno_Guard error (errno);
00969     ACE_OS::recursive_mutex_cond_relock (&recursive_mutex,
00970                                          mutex_state_holder);
00971   }
00972 
00973   return result;
00974 }
00975 
00976 //ACE_TEMPLATE_METHOD_SPECIALIZATION
00977 int
00978 ACE_Condition<ACE_Recursive_Thread_Mutex>::signal (void)
00979 {
00980   return ACE_OS::cond_signal (&this->cond_);
00981 }
00982 
00983 //ACE_TEMPLATE_METHOD_SPECIALIZATION
00984 int
00985 ACE_Condition<ACE_Recursive_Thread_Mutex>::broadcast (void)
00986 {
00987   return ACE_OS::cond_broadcast (&this->cond_);
00988 }
00989 
00990 //ACE_TEMPLATE_METHOD_SPECIALIZATION
00991 ACE_Recursive_Thread_Mutex &
00992 ACE_Condition<ACE_Recursive_Thread_Mutex>::mutex (void)
00993 {
00994   return this->mutex_;
00995 }
00996 
00997 #if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
00998 // These are only instantiated with ACE_HAS_THREADS.
00999 template class ACE_Guard<ACE_Thread_Mutex>;
01000 template class ACE_Guard<ACE_RW_Thread_Mutex>;
01001 template class ACE_Read_Guard<ACE_RW_Thread_Mutex>;
01002 template class ACE_Read_Guard<ACE_Thread_Mutex>;
01003 template class ACE_Write_Guard<ACE_RW_Thread_Mutex>;
01004 template class ACE_Write_Guard<ACE_Thread_Mutex>;
01005 #elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
01006 // These are only instantiated with ACE_HAS_THREADS.
01007 #pragma instantiate ACE_Guard<ACE_Thread_Mutex>
01008 #pragma instantiate ACE_Guard<ACE_RW_Thread_Mutex>
01009 #pragma instantiate ACE_Read_Guard<ACE_RW_Thread_Mutex>
01010 #pragma instantiate ACE_Read_Guard<ACE_Thread_Mutex>
01011 #pragma instantiate ACE_Write_Guard<ACE_RW_Thread_Mutex>
01012 #pragma instantiate ACE_Write_Guard<ACE_Thread_Mutex>
01013 #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
01014 
01015 #endif /* ACE_HAS_THREADS */
01016 #endif /* ACE_SYNCH_C */

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