Go to the documentation of this file.00001
00002
00003 #include "tao/TAO_Singleton_Manager.h"
00004
00005 #include "ace/Guard_T.h"
00006 #include "ace/Recursive_Thread_Mutex.h"
00007 #include "ace/Log_Msg.h"
00008 #include "ace/Object_Manager.h"
00009 #include "ace/os_include/os_typeinfo.h"
00010
00011 #if !defined (__ACE_INLINE__)
00012 # include "tao/TAO_Singleton_Manager.inl"
00013 #endif
00014
00015 ACE_RCSID (tao,
00016 TAO_Singleton_Manager,
00017 "$Id: TAO_Singleton_Manager.cpp 84164 2009-01-15 08:03:08Z johnnyw $")
00018
00019 namespace
00020 {
00021
00022 TAO_Singleton_Manager * the_instance = 0;
00023 }
00024
00025 #if (defined (ACE_HAS_VERSIONED_NAMESPACE) && ACE_HAS_VERSIONED_NAMESPACE == 1)
00026 # define TAO_SINGLETON_MANAGER_CLEANUP_DESTROYER_NAME ACE_PREPROC_CONCATENATE(TAO_VERSIONED_NAMESPACE_NAME, _TAO_Singleton_Manager_cleanup_destroyer)
00027 # define TAO_SINGLETON_MANAGER_FINI_NAME ACE_PREPROC_CONCATENATE(TAO_VERSIONED_NAMESPACE_NAME, _TAO_Singleton_Manager_fini)
00028 #endif
00029
00030
00031
00032 extern "C" void
00033 TAO_SINGLETON_MANAGER_CLEANUP_DESTROYER_NAME (void *, void *)
00034 {
00035 #if defined (TAO_HAS_VERSIONED_NAMESPACE) \
00036 && TAO_HAS_VERSIONED_NAMESPACE == 1
00037 using namespace TAO_VERSIONED_NAMESPACE_NAME;
00038 #endif
00039
00040 if (the_instance)
00041 {
00042 (void) TAO_Singleton_Manager::instance ()->fini ();
00043 }
00044 }
00045
00046 #if (ACE_HAS_GCC_DESTRUCTOR_ATTRIBUTE == 1)
00047 static void TAO_SINGLETON_MANAGER_FINI_NAME (void) ACE_GCC_DESTRUCTOR_ATTRIBUTE;
00048
00049 void TAO_SINGLETON_MANAGER_FINI_NAME (void)
00050 {
00051 #if defined (TAO_HAS_VERSIONED_NAMESPACE) \
00052 && TAO_HAS_VERSIONED_NAMESPACE == 1
00053 using namespace TAO_VERSIONED_NAMESPACE_NAME;
00054 #endif
00055 if (the_instance)
00056 {
00057 (void) TAO_Singleton_Manager::instance ()->fini ();
00058 }
00059 }
00060 #endif
00061
00062 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00063
00064 TAO_Singleton_Manager::TAO_Singleton_Manager (void)
00065 : default_mask_ (0),
00066 thread_hook_ (0),
00067 exit_info_ (),
00068 registered_with_object_manager_ (-1)
00069 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00070 , internal_lock_ (0)
00071 # endif
00072 {
00073 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00074 ACE_NEW (this->internal_lock_,
00075 TAO_SYNCH_RECURSIVE_MUTEX);
00076 # endif
00077
00078 if (the_instance == 0)
00079 {
00080 the_instance = this;
00081 }
00082
00083
00084
00085
00086
00087 int register_with_object_manager = -1;
00088 (void) this->init (register_with_object_manager);
00089 }
00090
00091 TAO_Singleton_Manager::~TAO_Singleton_Manager (void)
00092 {
00093 this->dynamically_allocated_ = false;
00094 (void) this->fini ();
00095
00096 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00097 delete this->internal_lock_;
00098 this->internal_lock_ = 0;
00099 #endif
00100 }
00101
00102 sigset_t *
00103 TAO_Singleton_Manager::default_mask (void)
00104 {
00105 return TAO_Singleton_Manager::instance ()->default_mask_;
00106 }
00107
00108 ACE_Thread_Hook *
00109 TAO_Singleton_Manager::thread_hook (void)
00110 {
00111 return TAO_Singleton_Manager::instance ()->thread_hook_;
00112 }
00113
00114 ACE_Thread_Hook *
00115 TAO_Singleton_Manager::thread_hook (ACE_Thread_Hook *new_thread_hook)
00116 {
00117 TAO_Singleton_Manager *tao_om = TAO_Singleton_Manager::instance ();
00118 ACE_Thread_Hook *old_hook = tao_om->thread_hook_;
00119 tao_om->thread_hook_ = new_thread_hook;
00120 return old_hook;
00121 }
00122
00123 TAO_Singleton_Manager *
00124 TAO_Singleton_Manager::instance (void)
00125 {
00126
00127
00128
00129 if (the_instance == 0)
00130 {
00131 TAO_Singleton_Manager *instance_pointer = 0;
00132
00133 ACE_NEW_RETURN (instance_pointer,
00134 TAO_Singleton_Manager,
00135 0);
00136 ACE_ASSERT (instance_pointer == the_instance);
00137
00138 instance_pointer->dynamically_allocated_ = true;
00139
00140 return instance_pointer;
00141 }
00142 else
00143 {
00144 return the_instance;
00145 }
00146 }
00147
00148 int
00149 TAO_Singleton_Manager::init (void)
00150 {
00151 if (this->registered_with_object_manager_ == -1)
00152 {
00153
00154
00155 int const register_with_object_manager = 1;
00156
00157 return this->init (register_with_object_manager);
00158 }
00159
00160 return 1;
00161 }
00162
00163 int
00164 TAO_Singleton_Manager::init (int register_with_object_manager)
00165 {
00166 if (this->starting_up_i ())
00167 {
00168
00169
00170 this->object_manager_state_ = OBJ_MAN_INITIALIZING;
00171
00172 if (this == the_instance)
00173 {
00174 # if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00175
00176 # endif
00177 }
00178
00179 ACE_NEW_RETURN (this->default_mask_, sigset_t, -1);
00180 ACE_OS::sigfillset (this->default_mask_);
00181
00182
00183
00184 this->object_manager_state_ = OBJ_MAN_INITIALIZED;
00185
00186 return 0;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195 if (this->registered_with_object_manager_ != -1
00196 && register_with_object_manager != this->registered_with_object_manager_)
00197 {
00198
00199
00200
00201
00202 errno = EINVAL;
00203 return -1;
00204 }
00205
00206 if (this->registered_with_object_manager_ == -1)
00207 {
00208 if (register_with_object_manager == 1
00209 && ACE_Object_Manager::at_exit (
00210 this,
00211 (ACE_CLEANUP_FUNC) TAO_SINGLETON_MANAGER_CLEANUP_DESTROYER_NAME,
00212 0,
00213 typeid (*this).name ()) != 0)
00214 return -1;
00215
00216 this->registered_with_object_manager_ = register_with_object_manager;
00217 }
00218
00219
00220 return 1;
00221 }
00222
00223
00224
00225
00226
00227 int
00228 TAO_Singleton_Manager::fini (void)
00229 {
00230 if (the_instance == 0 || this->shutting_down_i ())
00231 {
00232
00233
00234 return this->object_manager_state_ == OBJ_MAN_SHUT_DOWN ? 1 : -1;
00235 }
00236
00237
00238
00239
00240
00241
00242
00243 this->object_manager_state_ = OBJ_MAN_SHUTTING_DOWN;
00244
00245
00246 if (this->next_)
00247 {
00248 this->next_->fini ();
00249 this->next_ = 0;
00250 }
00251
00252
00253
00254 this->exit_info_.call_hooks ();
00255
00256
00257 if (this->registered_with_object_manager_ == 1)
00258 {
00259 ACE_Object_Manager::remove_at_exit (this);
00260 }
00261
00262 delete this->default_mask_;
00263 this->default_mask_ = 0;
00264
00265
00266 this->object_manager_state_ = OBJ_MAN_SHUT_DOWN;
00267
00268 if (this == the_instance)
00269 the_instance = 0;
00270
00271 if (this->dynamically_allocated_)
00272 {
00273 delete this;
00274 }
00275
00276 return 0;
00277 }
00278
00279 int
00280 TAO_Singleton_Manager::starting_up (void)
00281 {
00282 return
00283 the_instance
00284 ? the_instance->starting_up_i ()
00285 : 1;
00286 }
00287
00288 int
00289 TAO_Singleton_Manager::shutting_down (void)
00290 {
00291 return
00292 the_instance
00293 ? the_instance->shutting_down_i ()
00294 : 1;
00295 }
00296
00297 int
00298 TAO_Singleton_Manager::at_exit_i (void *object,
00299 ACE_CLEANUP_FUNC cleanup_hook,
00300 void *param,
00301 const char *name)
00302 {
00303 ACE_MT (ACE_GUARD_RETURN (TAO_SYNCH_RECURSIVE_MUTEX,
00304 ace_mon,
00305 *the_instance->internal_lock_,
00306 -1));
00307
00308 if (this->shutting_down_i ())
00309 {
00310 errno = EAGAIN;
00311 return -1;
00312 }
00313
00314 if (this->exit_info_.find (object))
00315 {
00316
00317 errno = EEXIST;
00318 return -1;
00319 }
00320
00321 return this->exit_info_.at_exit_i (object, cleanup_hook, param, name);
00322 }
00323
00324 TAO_END_VERSIONED_NAMESPACE_DECL