1 
  2 #include <util/xmlBlasterDef.h>
  3 #include <util/objman.h>
  4 #include <util/Global.h>
  5 #include <cstdlib> //<stdlib.h>
  6 
  7 using namespace std;
  8 using namespace org::xmlBlaster::client::protocol;
  9 using namespace org::xmlBlaster::util::dispatch;
 10 using namespace org::xmlBlaster::util;
 11 
 12 //static int gObjectManagerState = 0;
 13 // Cleanup routine for atexit
 14 extern "C" void object_manager_cleanup()
 15    { 
 16         /*
 17      if(gObjectManagerState != Object_Lifetime_Manager_Base::OBJ_MAN_SHUT_DOWN &&
 18         gObjectManagerState != Object_Lifetime_Manager_Base::OBJ_MAN_SHUTTING_DOWN)
 19        ;//Object_Lifetime_Manager::instance()->fini();
 20         */
 21    }
 22 
 23 namespace org { namespace xmlBlaster { namespace util {
 24 
 25 #define PREALLOCATE_OBJECT(TYPE, ID)\
 26 {\
 27   TYPE* t = new TYPE;\
 28   ManagedObject<TYPE> *obj_p;\
 29   obj_p = new  ManagedObject<TYPE>(t);\
 30   preallocated_object[ID] = obj_p;\
 31 }
 32 
 33 //namespace org { namespace xmlBlaster { namespace util {
 34 
 35 // Static declarations for Object_Lifetime_Manager.
 36 Object_Lifetime_Manager * Object_Lifetime_Manager::instance_ = 0;
 37 ManagedMap              Object_Lifetime_Manager::managedObjectMap_;
 38 ManagedList             Object_Lifetime_Manager::managedObjectList_;
 39 void *                  Object_Lifetime_Manager::preallocated_object[PREALLOCATED_OBJECTS];
 40 
 41 
 42 void Object_Lifetime_Manager::init (void)
 43 {
 44    Object_Lifetime_Manager::instance()->startup();
 45 }
 46 
 47 void Object_Lifetime_Manager::fini (void)
 48 {
 49    Object_Lifetime_Manager::instance()->shutdown();
 50 }
 51 
 52 /**
 53  * This function should be called during
 54  * construction of static instances, or
 55  * before any other threads have been created
 56  * in the process. So, it's not thread safe.
 57  */
 58 Object_Lifetime_Manager * Object_Lifetime_Manager::instance (void)
 59 {
 60   if (instance_ == 0) 
 61   {
 62     Object_Lifetime_Manager *instance_pointer =
 63     new Object_Lifetime_Manager;
 64     
 65     instance_pointer->dynamically_allocated_ = 1;
 66   }
 67   return instance_;
 68 }
 69 
 70 int Object_Lifetime_Manager::startup ()
 71 {
 72   if (starting_up_i ()) 
 73   {
 74     // First, indicate that this
 75     // Object_Lifetime_Manager instance
 76     // is being initialized.
 77     object_manager_state_ = OBJ_MAN_INITIALIZING;
 78     if (this == instance_) 
 79     {      
 80       // Create Global as part of ObjectManager startup.
 81       PREALLOCATE_OBJECT(Global, XMLBLASTER_GLOBAL);
 82       object_manager_state_ = OBJ_MAN_INITIALIZED;    
 83     }
 84     return 0;
 85   } 
 86   else 
 87   {
 88     // Hads already initialized.
 89     return 1;
 90   }
 91 }
 92 
 93 int Object_Lifetime_Manager::shutdown ()
 94 {
 95    if (shutting_down_i())
 96       // Too late. Or, maybe too early. Either
 97       // <fini> has already been called, or
 98       // <init> was never called.
 99       return object_manager_state_ == OBJ_MAN_SHUT_DOWN ? 1 : -1;
100 
101    // Indicate that the Object_Lifetime_Manager
102    // instance is being shut down.
103    // This object manager should be the last one
104    // to be shut down.
105    /*gObjectManagerState = */object_manager_state_ = OBJ_MAN_SHUTTING_DOWN;
106 
107    // Only clean up Pre-allocated Objects when
108    // the singleton Instance is being destroyed.
109    // The sequence is reversed, the last created object is destroyed first.
110    if (this == instance_) {
111       ManagedList::reverse_iterator i;
112       for(i = managedObjectList_.rbegin(); i != managedObjectList_.rend(); ++i) {
113          Cleanup_Adaptor* adap = *i;
114          adap->cleanup();
115          delete adap;
116       }
117       managedObjectList_.clear();
118 
119       managedObjectMap_.clear();
120 
121       // Delete the Global post of list method as a dependency may exist
122       // between objects existing in both constructs.
123       // Array is last as all objects placed here are intentional and
124       // it is assumed user is aware of such semantics.
125       delete (Cleanup_Adaptor*)preallocated_object[XMLBLASTER_GLOBAL];
126    }
127 
128    // Indicate that this Object_Lifetime_Manager
129    // instance has been shut down.
130    /*gObjectManagerState = */object_manager_state_ = OBJ_MAN_SHUT_DOWN;
131    if (dynamically_allocated_)
132       delete this;
133    if (this == instance_)
134       instance_ = 0;
135    return 0;
136 }
137 
138 }}} // namespace


syntax highlighted by Code2HTML, v. 0.9.1