Object_Manager.cpp

Go to the documentation of this file.
00001 // $Id: Object_Manager.cpp 79134 2007-07-31 18:23:50Z johnnyw $
00002 
00003 #include "ace/Object_Manager.h"
00004 #if !defined (ACE_LACKS_ACE_TOKEN)
00005 # include "ace/Token_Manager.h"
00006 #endif /* ! ACE_LACKS_ACE_TOKEN */
00007 #include "ace/Thread_Manager.h"
00008 #if !defined (ACE_LACKS_ACE_SVCCONF)
00009 #  include "ace/Service_Manager.h"
00010 #  include "ace/Service_Config.h"
00011 #endif /* ! ACE_LACKS_ACE_SVCCONF */
00012 #include "ace/Signal.h"
00013 #include "ace/Log_Msg.h"
00014 #include "ace/Malloc.h"
00015 #include "ace/Sig_Adapter.h"
00016 #include "ace/Framework_Component.h"
00017 #include "ace/DLL_Manager.h"
00018 #include "ace/Atomic_Op.h"
00019 #include "ace/OS_NS_sys_time.h"
00020 
00021 #if defined (ACE_HAS_TRACE)
00022 #include "ace/Trace.h"
00023 #endif /* ACE_HAS_TRACE */
00024 
00025 #if !defined (__ACE_INLINE__)
00026 # include "ace/Object_Manager.inl"
00027 #endif /* __ACE_INLINE__ */
00028 
00029 #include "ace/Guard_T.h"
00030 #include "ace/Null_Mutex.h"
00031 #include "ace/Mutex.h"
00032 #include "ace/RW_Thread_Mutex.h"
00033 
00034 ACE_RCSID(ace, Object_Manager, "$Id: Object_Manager.cpp 79134 2007-07-31 18:23:50Z johnnyw $")
00035 
00036 #if ! defined (ACE_APPLICATION_PREALLOCATED_OBJECT_DEFINITIONS)
00037 # define ACE_APPLICATION_PREALLOCATED_OBJECT_DEFINITIONS
00038 #endif /* ACE_APPLICATION_PREALLOCATED_OBJECT_DEFINITIONS */
00039 
00040 #if ! defined (ACE_APPLICATION_PREALLOCATED_ARRAY_DEFINITIONS)
00041 # define ACE_APPLICATION_PREALLOCATED_ARRAY_DEFINITIONS
00042 #endif /* ACE_APPLICATION_PREALLOCATED_ARRAY_DEFINITIONS */
00043 
00044 #if ! defined (ACE_APPLICATION_PREALLOCATED_OBJECT_DELETIONS)
00045 # define ACE_APPLICATION_PREALLOCATED_OBJECT_DELETIONS
00046 #endif /* ACE_APPLICATION_PREALLOCATED_OBJECT_DELETIONS */
00047 
00048 #if ! defined (ACE_APPLICATION_PREALLOCATED_ARRAY_DELETIONS)
00049 # define ACE_APPLICATION_PREALLOCATED_ARRAY_DELETIONS
00050 #endif /* ACE_APPLICATION_PREALLOCATED_ARRAY_DELETIONS */
00051 
00052 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00053 
00054 // Singleton pointer.
00055 ACE_Object_Manager *ACE_Object_Manager::instance_ = 0;
00056 
00057 void *ACE_Object_Manager::preallocated_object[
00058   ACE_Object_Manager::ACE_PREALLOCATED_OBJECTS] = { 0 };
00059 
00060 void *ACE_Object_Manager::preallocated_array[
00061   ACE_Object_Manager::ACE_PREALLOCATED_ARRAYS] = { 0 };
00062 
00063 // Handy macros for use by ACE_Object_Manager constructor to
00064 // preallocate or delete an object or array, either statically (in
00065 // global data) or dynamically (on the heap).
00066 #if defined (ACE_HAS_STATIC_PREALLOCATION)
00067 # define ACE_PREALLOCATE_OBJECT(TYPE, ID)\
00068     {\
00069       static ACE_Cleanup_Adapter<TYPE> obj;\
00070       preallocated_object[ID] = &obj;\
00071     }
00072 # define ACE_PREALLOCATE_ARRAY(TYPE, ID, COUNT)\
00073     {\
00074       static ACE_Cleanup_Adapter<TYPE> obj[COUNT];\
00075       preallocated_array[ID] = &obj;\
00076     }
00077 #else
00078 # define ACE_PREALLOCATE_OBJECT(TYPE, ID)\
00079     {\
00080       ACE_Cleanup_Adapter<TYPE> *obj_p;\
00081       ACE_NEW_RETURN (obj_p, ACE_Cleanup_Adapter<TYPE>, -1);\
00082       preallocated_object[ID] = obj_p;\
00083     }
00084 # define ACE_PREALLOCATE_ARRAY(TYPE, ID, COUNT)\
00085     {\
00086       ACE_Cleanup_Adapter<TYPE[COUNT]> *array_p;\
00087       ACE_NEW_RETURN (array_p, ACE_Cleanup_Adapter<TYPE[COUNT]>, -1);\
00088       preallocated_array[ID] = array_p;\
00089     }
00090 # define ACE_DELETE_PREALLOCATED_OBJECT(TYPE, ID)\
00091     ACE_CLEANUP_DESTROYER_NAME (\
00092       (ACE_Cleanup_Adapter<TYPE> *) preallocated_object[ID], 0);\
00093     preallocated_object[ID] = 0;
00094 # define ACE_DELETE_PREALLOCATED_ARRAY(TYPE, ID, COUNT)\
00095     delete (ACE_Cleanup_Adapter<TYPE[COUNT]> *) preallocated_array[ID];\
00096     preallocated_array[ID] = 0;
00097 #endif /* ACE_HAS_STATIC_PREALLOCATION */
00098 
00099 #if !defined (ACE_LACKS_ACE_SVCCONF)
00100 
00101 /**
00102  * @class ACE_Object_Manager_Preallocations
00103  *
00104  * @brief Performs preallocations of certain statically allocated services
00105  * needed by ACE.
00106  */
00107 class ACE_Object_Manager_Preallocations
00108 {
00109 public:
00110   ACE_Object_Manager_Preallocations (void);
00111   ~ACE_Object_Manager_Preallocations (void);
00112 
00113 private:
00114   ACE_Static_Svc_Descriptor ace_svc_desc_ACE_Service_Manager;
00115 };
00116 
00117 ACE_Object_Manager_Preallocations::ACE_Object_Manager_Preallocations (void)
00118 {
00119   ACE_STATIC_SVC_DEFINE (ACE_Service_Manager_initializer,
00120                          ACE_TEXT ("ACE_Service_Manager"),
00121                          ACE_SVC_OBJ_T,
00122                          &ACE_SVC_NAME (ACE_Service_Manager),
00123                          ACE_Service_Type::DELETE_THIS |
00124                            ACE_Service_Type::DELETE_OBJ,
00125                          0)
00126 
00127   // Initialize the static service objects using the descriptors created
00128   // above.
00129   ace_svc_desc_ACE_Service_Manager =
00130     ace_svc_desc_ACE_Service_Manager_initializer;
00131 
00132   // Add to the list of static configured services.
00133   ACE_Service_Config::static_svcs ()->
00134     insert (&ace_svc_desc_ACE_Service_Manager);
00135 }
00136 
00137 ACE_Object_Manager_Preallocations::~ACE_Object_Manager_Preallocations (void)
00138 {
00139 }
00140 
00141 #endif /* ! ACE_LACKS_ACE_SVCCONF */
00142 
00143 int
00144 ACE_Object_Manager::starting_up (void)
00145 {
00146   return ACE_Object_Manager::instance_  ?  instance_->starting_up_i ()  :  1;
00147 }
00148 
00149 int
00150 ACE_Object_Manager::shutting_down (void)
00151 {
00152   return ACE_Object_Manager::instance_  ?  instance_->shutting_down_i ()  :  1;
00153 }
00154 
00155 #if defined (ACE_DISABLE_WIN32_ERROR_WINDOWS)
00156 // Instead of popping up a window for exceptions, just print something out
00157 LONG _stdcall ACE_UnhandledExceptionFilter (PEXCEPTION_POINTERS pExceptionInfo)
00158 {
00159   DWORD dwExceptionCode = pExceptionInfo->ExceptionRecord->ExceptionCode;
00160 
00161   if (dwExceptionCode == EXCEPTION_ACCESS_VIOLATION)
00162     ACE_ERROR ((LM_ERROR, ACE_TEXT ("\nERROR: ACCESS VIOLATION\n")));
00163   else
00164     ACE_ERROR ((LM_ERROR, ACE_TEXT ("\nERROR: UNHANDLED EXCEPTION\n")));
00165 
00166   return EXCEPTION_EXECUTE_HANDLER;
00167 }
00168 #endif /* ACE_DISABLE_WIN32_ERROR_WINDOWS */
00169 
00170 // Initialize an ACE_Object_Manager.  There can be instances of this object
00171 // other than The Instance.  This can happen if a user creates one for some
00172 // reason.  All objects set up their per-object information and managed
00173 // objects, but only The Instance sets up the static preallocated objects and
00174 // the (static) ACE_Service_Config signal handler.
00175 int
00176 ACE_Object_Manager::init (void)
00177 {
00178   if (starting_up_i ())
00179     {
00180       // First, indicate that the ACE_Object_Manager instance is being
00181       // initialized.
00182       object_manager_state_ = OBJ_MAN_INITIALIZING;
00183 
00184       // Only The Instance sets up with ACE_OS_Object_Manager and initializes
00185       // the preallocated objects.
00186       if (this == instance_)
00187         {
00188           // Make sure that the ACE_OS_Object_Manager has been created,
00189           // and register with it for chained fini ().
00190           ACE_OS_Object_Manager::instance ()->next_ = this;
00191 
00192 #     if defined (ACE_HAS_BUILTIN_ATOMIC_OP)
00193           ACE_Atomic_Op<ACE_Thread_Mutex, long>::init_functions ();
00194           ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long>::init_functions ();
00195 #     endif /* ACE_HAS_BUILTIN_ATOMIC_OP */
00196 
00197 #     if !defined (ACE_LACKS_ACE_SVCCONF)
00198           // Construct the ACE_Service_Config's signal handler.
00199           ACE_NEW_RETURN (ace_service_config_sig_handler_,
00200                      ACE_Sig_Adapter (&ACE_Service_Config::handle_signal), -1);
00201           ACE_Service_Config::signal_handler (ace_service_config_sig_handler_);
00202 #     endif /* ! ACE_LACKS_ACE_SVCCONF */
00203 
00204           // Allocate the preallocated (hard-coded) object instances.
00205           ACE_PREALLOCATE_OBJECT (ACE_SYNCH_RW_MUTEX, ACE_FILECACHE_LOCK)
00206 #     if defined (ACE_HAS_THREADS)
00207           ACE_PREALLOCATE_OBJECT (ACE_Recursive_Thread_Mutex,
00208                                   ACE_STATIC_OBJECT_LOCK)
00209 #     endif /* ACE_HAS_THREADS */
00210 #     if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00211           ACE_PREALLOCATE_OBJECT (ACE_Thread_Mutex,
00212                                   ACE_MT_CORBA_HANDLER_LOCK)
00213           ACE_PREALLOCATE_OBJECT (ACE_Thread_Mutex, ACE_DUMP_LOCK)
00214           ACE_PREALLOCATE_OBJECT (ACE_Recursive_Thread_Mutex,
00215                                   ACE_SIG_HANDLER_LOCK)
00216           ACE_PREALLOCATE_OBJECT (ACE_Null_Mutex, ACE_SINGLETON_NULL_LOCK)
00217           ACE_PREALLOCATE_OBJECT (ACE_Recursive_Thread_Mutex,
00218                                   ACE_SINGLETON_RECURSIVE_THREAD_LOCK)
00219           ACE_PREALLOCATE_OBJECT (ACE_Thread_Mutex, ACE_THREAD_EXIT_LOCK)
00220 #if !defined (ACE_LACKS_ACE_TOKEN) && defined (ACE_HAS_TOKENS_LIBRARY)
00221           ACE_PREALLOCATE_OBJECT (ACE_TOKEN_CONST::MUTEX,
00222                                   ACE_TOKEN_MANAGER_CREATION_LOCK)
00223           ACE_PREALLOCATE_OBJECT (ACE_TOKEN_CONST::MUTEX,
00224                                   ACE_TOKEN_INVARIANTS_CREATION_LOCK)
00225 #endif /* ! ACE_LACKS_ACE_TOKEN && ACE_HAS_TOKENS_LIBRARY */
00226           ACE_PREALLOCATE_OBJECT (ACE_Thread_Mutex,
00227                                   ACE_PROACTOR_EVENT_LOOP_LOCK)
00228 #     endif /* ACE_MT_SAFE */
00229         }
00230 
00231       if (this == instance_)
00232         {
00233           // Hooks for preallocated objects and arrays provided by application.
00234           ACE_APPLICATION_PREALLOCATED_OBJECT_DEFINITIONS
00235           ACE_APPLICATION_PREALLOCATED_ARRAY_DEFINITIONS
00236 
00237 #     if defined (ACE_HAS_TSS_EMULATION)
00238           // Initialize the main thread's TS storage.
00239           ACE_TSS_Emulation::tss_open (ts_storage_);
00240 #     endif /* ACE_HAS_TSS_EMULATION */
00241 
00242 #if defined (ACE_DISABLE_WIN32_ERROR_WINDOWS) && \
00243     defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)
00244 #if defined (_DEBUG) && (defined (_MSC_VER) || defined (__INTEL_COMPILER))
00245           // This will keep the ACE_Assert window
00246           _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
00247           _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDERR );
00248 #endif /* _DEBUG && _MSC_VER || __INTEL_COMPILER */
00249 
00250           // And this will catch all unhandled exceptions.
00251           SetUnhandledExceptionFilter (&ACE_UnhandledExceptionFilter);
00252 #endif /* ACE_DISABLE_WIN32_ERROR_WINDOWS && ACE_WIN32 && !ACE_HAS_WINCE */
00253 
00254 
00255 #     if !defined (ACE_LACKS_ACE_SVCCONF)
00256           ACE_NEW_RETURN (preallocations_,
00257                           ACE_Object_Manager_Preallocations,
00258                           -1);
00259 #     endif /* ! ACE_LACKS_ACE_SVCCONF */
00260 
00261           // Open the main thread's ACE_Log_Msg.
00262           if (0 == ACE_LOG_MSG)
00263             return -1;
00264         }
00265 
00266       // Finally, indicate that the ACE_Object_Manager instance has
00267       // been initialized.
00268       object_manager_state_ = OBJ_MAN_INITIALIZED;
00269 
00270 #if defined (ACE_HAS_TRACE)
00271       // Allow tracing again (useful if user does init/fini/init)
00272       ACE_Trace::start_tracing ();
00273 #endif /* ACE_HAS_TRACE */
00274 
00275       return 0;
00276     } else {
00277       // Had already initialized.
00278       return 1;
00279     }
00280 }
00281 
00282 ACE_Object_Manager::ACE_Object_Manager (void)
00283   // With ACE_HAS_TSS_EMULATION, ts_storage_ is initialized by the call to
00284   // ACE_OS::tss_open () in the function body.
00285   : exit_info_ ()
00286 #if !defined (ACE_LACKS_ACE_SVCCONF)
00287   , preallocations_ (0)
00288   , ace_service_config_sig_handler_ (0)
00289 #endif /* ! ACE_LACKS_ACE_SVCCONF */
00290 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00291   , singleton_null_lock_ (0)
00292   , singleton_recursive_lock_ (0)
00293 # endif /* ACE_MT_SAFE */
00294 {
00295 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00296   ACE_NEW (internal_lock_, ACE_Recursive_Thread_Mutex);
00297 # endif /* ACE_MT_SAFE */
00298 
00299   // If instance_ was not 0, then another ACE_Object_Manager has
00300   // already been instantiated (it is likely to be one initialized by way
00301   // of library/DLL loading).  Let this one go through construction in
00302   // case there really is a good reason for it (like, ACE is a static/archive
00303   // library, and this one is the non-static instance (with
00304   // ACE_HAS_NONSTATIC_OBJECT_MANAGER, or the user has a good reason for
00305   // creating a separate one) but the original one will be the one retrieved
00306   // from calls to ACE_Object_Manager::instance().
00307 
00308   // Be sure that no further instances are created via instance ().
00309   if (instance_ == 0)
00310     instance_ = this;
00311 
00312   init ();
00313 }
00314 
00315 ACE_Object_Manager::~ACE_Object_Manager (void)
00316 {
00317   dynamically_allocated_ = false;   // Don't delete this again in fini()
00318   fini ();
00319 }
00320 
00321 ACE_Object_Manager *
00322 ACE_Object_Manager::instance (void)
00323 {
00324   // This function should be called during construction of static
00325   // instances, or before any other threads have been created in
00326   // the process.  So, it's not thread safe.
00327 
00328   if (instance_ == 0)
00329     {
00330       ACE_Object_Manager *instance_pointer = 0;
00331 
00332       ACE_NEW_RETURN (instance_pointer,
00333                       ACE_Object_Manager,
00334                       0);
00335       ACE_ASSERT (instance_pointer == instance_);
00336 
00337       instance_pointer->dynamically_allocated_ = true;
00338 
00339       return instance_pointer;
00340     }
00341   else
00342     return instance_;
00343 }
00344 
00345 int
00346 ACE_Object_Manager::at_exit_i (void *object,
00347                                ACE_CLEANUP_FUNC cleanup_hook,
00348                                void *param)
00349 {
00350   ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00351     *instance_->internal_lock_, -1));
00352 
00353   if (shutting_down_i ())
00354     {
00355       errno = EAGAIN;
00356       return -1;
00357     }
00358 
00359   if (exit_info_.find (object))
00360     {
00361       // The object has already been registered.
00362       errno = EEXIST;
00363       return -1;
00364     }
00365 
00366   return exit_info_.at_exit_i (object, cleanup_hook, param);
00367 }
00368 
00369 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00370 
00371 int
00372 ACE_Object_Manager::get_singleton_lock (ACE_Null_Mutex *&lock)
00373 {
00374   if (starting_up ()  ||  shutting_down ())
00375     {
00376       // The preallocated lock has not been constructed yet.
00377       // Therefore, the program is single-threaded at this point.  Or,
00378       // the ACE_Object_Manager instance has been destroyed, so the
00379       // preallocated lock is not available.  Allocate a lock to use,
00380       // for interface compatibility, though there should be no
00381       // contention on it.
00382       if (ACE_Object_Manager::instance ()->singleton_null_lock_ == 0)
00383         {
00384           ACE_NEW_RETURN (ACE_Object_Manager::instance ()->
00385                             singleton_null_lock_,
00386                           ACE_Cleanup_Adapter<ACE_Null_Mutex>,
00387                           -1);
00388 
00389           // Can't register with the ACE_Object_Manager here!  The
00390           // lock's declaration is visible to the ACE_Object_Manager
00391           // destructor, so it will clean it up as a special case.
00392         }
00393 
00394       if (ACE_Object_Manager::instance ()->singleton_null_lock_ != 0)
00395         lock = &ACE_Object_Manager::instance ()->singleton_null_lock_->
00396           object ();
00397     }
00398   else
00399     // Use the Object_Manager's preallocated lock.
00400     lock = ACE_Managed_Object<ACE_Null_Mutex>::get_preallocated_object
00401       (ACE_Object_Manager::ACE_SINGLETON_NULL_LOCK);
00402 
00403   return 0;
00404 }
00405 
00406 int
00407 ACE_Object_Manager::get_singleton_lock (ACE_Thread_Mutex *&lock)
00408 {
00409   if (lock == 0)
00410     {
00411       if (starting_up () || shutting_down ())
00412         {
00413           // The Object_Manager and its internal lock have not been
00414           // constructed yet.  Therefore, the program is single-
00415           // threaded at this point.  Or, the ACE_Object_Manager
00416           // instance has been destroyed, so the internal lock is not
00417           // available.  Either way, we can not use double-checked
00418           // locking.  So, we'll leak the lock.
00419           ACE_NEW_RETURN (lock,
00420                           ACE_Thread_Mutex,
00421                           -1);
00422         }
00423       else
00424         {
00425           // Allocate a new lock, but use double-checked locking to
00426           // ensure that only one thread allocates it.
00427           ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex,
00428                                     ace_mon,
00429                                     *ACE_Object_Manager::instance ()->
00430                                     internal_lock_,
00431                                     -1));
00432 
00433           if (lock == 0)
00434             {
00435               ACE_Cleanup_Adapter<ACE_Thread_Mutex> *lock_adapter;
00436               ACE_NEW_RETURN (lock_adapter,
00437                               ACE_Cleanup_Adapter<ACE_Thread_Mutex>,
00438                               -1);
00439               lock = &lock_adapter->object ();
00440 
00441               // Register the lock for destruction at program
00442               // termination.  This call will cause us to grab the
00443               // ACE_Object_Manager::instance ()->internal_lock_
00444               // again; that's why it is a recursive lock.
00445               ACE_Object_Manager::at_exit (lock_adapter);
00446             }
00447         }
00448     }
00449 
00450   return 0;
00451 }
00452 
00453 int
00454 ACE_Object_Manager::get_singleton_lock (ACE_Mutex *&lock)
00455 {
00456   if (lock == 0)
00457     {
00458       if (starting_up ()  ||  shutting_down ())
00459         {
00460           // The Object_Manager and its internal lock have not been
00461           // constructed yet.  Therefore, the program is single-
00462           // threaded at this point.  Or, the ACE_Object_Manager
00463           // instance has been destroyed, so the internal lock is not
00464           // available.  Either way, we can not use double-checked
00465           // locking.  So, we'll leak the lock.
00466 
00467           ACE_NEW_RETURN (lock,
00468                           ACE_Mutex,
00469                           -1);
00470         }
00471       else
00472         {
00473           // Allocate a new lock, but use double-checked locking to
00474           // ensure that only one thread allocates it.
00475           ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex,
00476                                     ace_mon,
00477                                     *ACE_Object_Manager::instance ()->
00478                                       internal_lock_,
00479                                     -1));
00480 
00481           if (lock == 0)
00482             {
00483               ACE_Cleanup_Adapter<ACE_Mutex> *lock_adapter;
00484               ACE_NEW_RETURN (lock_adapter,
00485                               ACE_Cleanup_Adapter<ACE_Mutex>,
00486                               -1);
00487               lock = &lock_adapter->object ();
00488 
00489               // Register the lock for destruction at program
00490               // termination.  This call will cause us to grab the
00491               // ACE_Object_Manager::instance ()->internal_lock_
00492               // again; that's why it is a recursive lock.
00493               ACE_Object_Manager::at_exit (lock_adapter);
00494             }
00495         }
00496     }
00497 
00498   return 0;
00499 }
00500 
00501 int
00502 ACE_Object_Manager::get_singleton_lock (ACE_Recursive_Thread_Mutex *&lock)
00503 {
00504   if (starting_up ()  ||  shutting_down ())
00505     {
00506       // The preallocated lock has not been constructed yet.
00507       // Therefore, the program is single-threaded at this point.  Or,
00508       // the ACE_Object_Manager instance has been destroyed, so the
00509       // preallocated lock is not available.  Allocate a lock to use,
00510       // for interface compatibility, though there should be no
00511       // contention on it.
00512       if (ACE_Object_Manager::instance ()->singleton_recursive_lock_ == 0)
00513         ACE_NEW_RETURN (ACE_Object_Manager::instance ()->
00514                           singleton_recursive_lock_,
00515                         ACE_Cleanup_Adapter<ACE_Recursive_Thread_Mutex>,
00516                         -1);
00517 
00518       // Can't register with the ACE_Object_Manager here!  The lock's
00519       // declaration is visible to the ACE_Object_Manager destructor,
00520       // so it will clean it up as a special case.
00521 
00522       if (ACE_Object_Manager::instance ()->singleton_recursive_lock_ != 0)
00523         lock = &ACE_Object_Manager::instance ()->singleton_recursive_lock_->
00524           object ();
00525     }
00526   else
00527     {
00528       // Use the Object_Manager's preallocated lock.
00529       lock = ACE_Managed_Object<ACE_Recursive_Thread_Mutex>::
00530         get_preallocated_object (ACE_Object_Manager::
00531                                  ACE_SINGLETON_RECURSIVE_THREAD_LOCK);
00532     }
00533 
00534   return 0;
00535 }
00536 
00537 int
00538 ACE_Object_Manager::get_singleton_lock (ACE_RW_Thread_Mutex *&lock)
00539 {
00540   if (lock == 0)
00541     {
00542       if (starting_up () || shutting_down ())
00543         {
00544           // The Object_Manager and its internal lock have not been
00545           // constructed yet.  Therefore, the program is single-
00546           // threaded at this point.  Or, the ACE_Object_Manager
00547           // instance has been destroyed, so the internal lock is not
00548           // available.  Either way, we can not use double-checked
00549           // locking.  So, we'll leak the lock.
00550 
00551           ACE_NEW_RETURN (lock,
00552                           ACE_RW_Thread_Mutex,
00553                           -1);
00554         }
00555       else
00556         {
00557           // Allocate a new lock, but use double-checked locking to
00558           // ensure that only one thread allocates it.
00559           ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex,
00560                                     ace_mon,
00561                                     *ACE_Object_Manager::instance ()->
00562                                     internal_lock_,
00563                                     -1));
00564 
00565           if (lock == 0)
00566             {
00567               ACE_Cleanup_Adapter<ACE_RW_Thread_Mutex> *lock_adapter;
00568               ACE_NEW_RETURN (lock_adapter,
00569                               ACE_Cleanup_Adapter<ACE_RW_Thread_Mutex>,
00570                               -1);
00571               lock = &lock_adapter->object ();
00572 
00573 
00574               // Register the lock for destruction at program
00575               // termination.  This call will cause us to grab the
00576               // ACE_Object_Manager::instance ()->internal_lock_
00577               // again; that's why it is a recursive lock.
00578               ACE_Object_Manager::at_exit (lock_adapter);
00579             }
00580         }
00581     }
00582 
00583   return 0;
00584 }
00585 #endif /* ACE_MT_SAFE */
00586 
00587 // Clean up an ACE_Object_Manager.  There can be instances of this object
00588 // other than The Instance.  This can happen if (on Win32) the ACE DLL
00589 // causes one to be created, or if a user creates one for some reason.
00590 // Only The Instance cleans up the static preallocated objects.  All objects
00591 // clean up their per-object information and managed objects.
00592 int
00593 ACE_Object_Manager::fini (void)
00594 {
00595   if (shutting_down_i ())
00596     // Too late.  Or, maybe too early.  Either fini () has already
00597     // been called, or init () was never called.
00598     return object_manager_state_ == OBJ_MAN_SHUT_DOWN  ?  1  :  -1;
00599 
00600   // No mutex here.  Only the main thread should destroy the singleton
00601   // ACE_Object_Manager instance.
00602 
00603   // Indicate that this ACE_Object_Manager instance is being
00604   // shut down.
00605   object_manager_state_ = OBJ_MAN_SHUTTING_DOWN;
00606 
00607   // Call all registered cleanup hooks, in reverse order of
00608   // registration.
00609   exit_info_.call_hooks ();
00610 
00611   if (this == instance_)
00612     {
00613 #if !defined (ACE_LACKS_ACE_SVCCONF)
00614       delete preallocations_;
00615       preallocations_ = 0;
00616 #endif /* ! ACE_LACKS_ACE_SVCCONF */
00617 
00618 #if defined (ACE_HAS_TRACE)
00619       ACE_Trace::stop_tracing ();
00620 #endif /* ACE_HAS_TRACE */
00621 
00622 #if !defined (ACE_LACKS_ACE_SVCCONF)
00623       // Close and possibly delete all service instances in the Service
00624       // Repository.
00625       ACE_Service_Config::fini_svcs ();
00626 
00627       // Unlink all services in the Service Repository and close/delete
00628       // all ACE library services and singletons.
00629       ACE_Service_Config::close ();
00630 #endif /* ! ACE_LACKS_ACE_SVCCONF */
00631 
00632       // This must come after closing ACE_Service_Config, since it will
00633       // close down it's dlls--it manages ACE_DLL_Manager.
00634       ACE_Framework_Repository::close_singleton ();
00635       ACE_DLL_Manager::close_singleton ();
00636 
00637 #  if ! defined (ACE_THREAD_MANAGER_LACKS_STATICS)
00638       ACE_Thread_Manager::close_singleton ();
00639 #  endif /* ! ACE_THREAD_MANAGER_LACKS_STATICS */
00640 
00641       // Close the main thread's TSS, including its Log_Msg instance.
00642       ACE_OS::cleanup_tss (1 /* main thread */);
00643 
00644       //
00645       // Note:  Do not access Log Msg after this since it is gone
00646       //
00647 
00648       // Close the ACE_Allocator.
00649       ACE_Allocator::close_singleton ();
00650 
00651 #if ! defined (ACE_HAS_STATIC_PREALLOCATION)
00652       // Hooks for deletion of preallocated objects and arrays provided by
00653       // application.
00654       ACE_APPLICATION_PREALLOCATED_ARRAY_DELETIONS
00655       ACE_APPLICATION_PREALLOCATED_OBJECT_DELETIONS
00656 
00657       // Cleanup the dynamically preallocated arrays.
00658       // (none)
00659 
00660       // Cleanup the dynamically preallocated objects.
00661       ACE_DELETE_PREALLOCATED_OBJECT (ACE_SYNCH_RW_MUTEX, ACE_FILECACHE_LOCK)
00662 #if defined (ACE_HAS_THREADS)
00663       ACE_DELETE_PREALLOCATED_OBJECT (ACE_Recursive_Thread_Mutex,
00664                                       ACE_STATIC_OBJECT_LOCK)
00665 #endif /* ACE_HAS_THREADS */
00666 # if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00667       ACE_DELETE_PREALLOCATED_OBJECT (ACE_Thread_Mutex,
00668                                       ACE_MT_CORBA_HANDLER_LOCK)
00669       ACE_DELETE_PREALLOCATED_OBJECT (ACE_Thread_Mutex, ACE_DUMP_LOCK)
00670       ACE_DELETE_PREALLOCATED_OBJECT (ACE_Recursive_Thread_Mutex,
00671                                       ACE_SIG_HANDLER_LOCK)
00672       ACE_DELETE_PREALLOCATED_OBJECT (ACE_Null_Mutex,
00673                                       ACE_SINGLETON_NULL_LOCK)
00674       ACE_DELETE_PREALLOCATED_OBJECT (ACE_Recursive_Thread_Mutex,
00675                                       ACE_SINGLETON_RECURSIVE_THREAD_LOCK)
00676       ACE_DELETE_PREALLOCATED_OBJECT (ACE_Thread_Mutex, ACE_THREAD_EXIT_LOCK)
00677 #if !defined (ACE_LACKS_ACE_TOKEN) && defined (ACE_HAS_TOKENS_LIBRARY)
00678       ACE_DELETE_PREALLOCATED_OBJECT (ACE_TOKEN_CONST::MUTEX,
00679                                       ACE_TOKEN_MANAGER_CREATION_LOCK)
00680       ACE_DELETE_PREALLOCATED_OBJECT (ACE_TOKEN_CONST::MUTEX,
00681                                       ACE_TOKEN_INVARIANTS_CREATION_LOCK)
00682 #endif /* ! ACE_LACKS_ACE_TOKEN && ACE_HAS_TOKENS_LIBRARY */
00683       ACE_DELETE_PREALLOCATED_OBJECT (ACE_Thread_Mutex,
00684                                       ACE_PROACTOR_EVENT_LOOP_LOCK)
00685 # endif /* ACE_MT_SAFE */
00686 #endif /* ! ACE_HAS_STATIC_PREALLOCATION */
00687 
00688 #if defined (ACE_HAS_THREADS)
00689       ACE_Static_Object_Lock::cleanup_lock ();
00690 #endif /* ACE_HAS_THREADS */
00691     }
00692 
00693 #if !defined (ACE_LACKS_ACE_SVCCONF)
00694   delete ace_service_config_sig_handler_;
00695   ace_service_config_sig_handler_ = 0;
00696 #endif /* ! ACE_LACKS_ACE_SVCCONF */
00697 
00698 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00699   delete internal_lock_;
00700   internal_lock_ = 0;
00701 
00702   delete singleton_null_lock_;
00703   singleton_null_lock_ = 0;
00704 
00705   delete singleton_recursive_lock_;
00706   singleton_recursive_lock_ = 0;
00707 #endif /* ACE_MT_SAFE */
00708 
00709   // Indicate that this ACE_Object_Manager instance has been shut down.
00710   object_manager_state_ = OBJ_MAN_SHUT_DOWN;
00711 
00712   // Then, ensure that the ACE_OS_Object_Manager gets shut down.
00713   if (this == instance_ && ACE_OS_Object_Manager::instance_)
00714     ACE_OS_Object_Manager::instance_->fini ();
00715 
00716   if (dynamically_allocated_)
00717     {
00718       delete this;
00719     }
00720 
00721   if (this == instance_)
00722     instance_ = 0;
00723 
00724   return 0;
00725 }
00726 
00727 
00728 #if !defined (ACE_HAS_NONSTATIC_OBJECT_MANAGER)
00729 /**
00730  * @class ACE_Object_Manager_Manager
00731  *
00732  * @brief Ensure that the ACE_Object_Manager gets initialized at program
00733  * startup, and destroyed at program termination.
00734  *
00735  * Without ACE_HAS_NONSTATIC_OBJECT_MANAGER, a static instance of this
00736  * class is created.  Therefore, it gets created before main ()
00737  * is called.  And it gets destroyed after main () returns.
00738  */
00739 class ACE_Export ACE_Object_Manager_Manager
00740 {
00741 public:
00742   ACE_Object_Manager_Manager (void);
00743   ~ACE_Object_Manager_Manager (void);
00744 
00745 private:
00746   /// Save the main thread ID, so that destruction can be suppressed.
00747   ACE_thread_t saved_main_thread_id_;
00748 };
00749 
00750 ACE_Object_Manager_Manager::ACE_Object_Manager_Manager (void)
00751   : saved_main_thread_id_ (ACE_OS::thr_self ())
00752 {
00753   // Ensure that the Object_Manager gets initialized before any
00754   // application threads have been spawned.  Because this will be called
00755   // during construction of static objects, that should always be the
00756   // case.
00757   (void) ACE_Object_Manager::instance ();
00758 }
00759 
00760 ACE_Object_Manager_Manager::~ACE_Object_Manager_Manager (void)
00761 {
00762   if (ACE_OS::thr_equal (ACE_OS::thr_self (),
00763                          saved_main_thread_id_))
00764     {
00765       delete ACE_Object_Manager::instance_;
00766       ACE_Object_Manager::instance_ = 0;
00767     }
00768   // else if this destructor is not called by the main thread, then do
00769   // not delete the ACE_Object_Manager.  That causes problems, on
00770   // WIN32 at least.
00771 }
00772 
00773 static ACE_Object_Manager_Manager ACE_Object_Manager_Manager_instance;
00774 #endif /* ! ACE_HAS_NONSTATIC_OBJECT_MANAGER */
00775 
00776 #if defined (ACE_HAS_THREADS)
00777 
00778 // hack to get around errors while compiling using split-cpp
00779 #if !defined (ACE_IS_SPLITTING)
00780 // This is global so that it doesn't have to be declared in the header
00781 // file.  That would cause nasty circular include problems.
00782 typedef ACE_Cleanup_Adapter<ACE_Recursive_Thread_Mutex> ACE_Static_Object_Lock_Type;
00783 static ACE_Static_Object_Lock_Type *ACE_Static_Object_Lock_lock = 0;
00784 #endif /* ! ACE_IS_SPLITTING */
00785 
00786 // ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK isn't (currently) used by ACE.
00787 // But, applications may find it useful for avoiding recursive calls
00788 // if they have overridden operator new.  Thanks to Jody Hagins
00789 // <jody@atdesk.com> for contributing it.
00790 
00791 ACE_Recursive_Thread_Mutex *
00792 ACE_Static_Object_Lock::instance (void)
00793 {
00794   if (ACE_Object_Manager::starting_up ()  ||
00795       ACE_Object_Manager::shutting_down ())
00796     {
00797       // The preallocated ACE_STATIC_OBJECT_LOCK has not been
00798       // constructed yet.  Therefore, the program is single-threaded
00799       // at this point.  Or, the ACE_Object_Manager instance has been
00800       // destroyed, so the preallocated lock is not available.
00801       // Allocate a lock to use, for interface compatibility, though
00802       // there should be no contention on it.
00803       if (ACE_Static_Object_Lock_lock == 0)
00804         {
00805 #     if defined (ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK)
00806         // Allocate a buffer with malloc, and then use placement
00807         // new for the object, on the malloc'd buffer.
00808         void *buffer =
00809           ACE_OS::malloc (sizeof (*ACE_Static_Object_Lock_lock));
00810         if (buffer == 0)
00811           {
00812             return 0;
00813           }
00814         // do not use ACE_NEW macros for placement new
00815         ACE_Static_Object_Lock_lock = new (buffer)
00816                         ACE_Static_Object_Lock_Type ();
00817 
00818 #       else   /* ! ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK */
00819         ACE_NEW_RETURN (ACE_Static_Object_Lock_lock,
00820                         ACE_Cleanup_Adapter<ACE_Recursive_Thread_Mutex>,
00821                         0);
00822 #       endif /* ! ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK */
00823         }
00824 
00825       // Can't register with the ACE_Object_Manager here!  The lock's
00826       // declaration is visible to the ACE_Object_Manager destructor,
00827       // so it will clean it up as a special case.
00828 
00829       return &ACE_Static_Object_Lock_lock->object ();
00830     }
00831   else
00832     // Return the preallocated ACE_STATIC_OBJECT_LOCK.
00833     return
00834       ACE_Managed_Object<ACE_Recursive_Thread_Mutex>::get_preallocated_object
00835         (ACE_Object_Manager::ACE_STATIC_OBJECT_LOCK);
00836 }
00837 
00838 void
00839 ACE_Static_Object_Lock::cleanup_lock (void)
00840 {
00841 # if defined(ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK)
00842     // It was malloc'd, so we need to explicitly call the dtor
00843     // and then free the memory.
00844     ACE_DES_FREE (ACE_Static_Object_Lock_lock,
00845                   ACE_OS::free,
00846                   ACE_Static_Object_Lock_Type);
00847 # else  /* ! ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK */
00848     delete ACE_Static_Object_Lock_lock;
00849 # endif /* ! ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK */
00850     ACE_Static_Object_Lock_lock = 0;
00851 }
00852 #endif /* ACE_HAS_THREADS */
00853 
00854 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Sun Jan 27 12:05:32 2008 for ACE by doxygen 1.3.6