Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Object_Manager.h
blob27ac68717d7f6a6a23521bba5cabe444266eb02d
1 /* -*- C++ -*- */
3 //=============================================================================
4 /**
5 * @file Object_Manager.h
7 * @author David L. Levine <levine@cs.wustl.edu>
8 * @author Matthias Kerkhoff
9 * @author Per Andersson
11 //=============================================================================
13 #ifndef ACE_OBJECT_MANAGER_H
14 #define ACE_OBJECT_MANAGER_H
15 #include /**/ "ace/pre.h"
17 #include /**/ "ace/ACE_export.h"
18 #include "ace/Object_Manager_Base.h"
19 #include "ace/Global_Macros.h"
21 #if !defined (ACE_LACKS_PRAGMA_ONCE)
22 # pragma once
23 #endif /* ACE_LACKS_PRAGMA_ONCE */
25 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
27 // Forward declarations.
28 class ACE_Object_Manager_Preallocations;
29 class ACE_Sig_Adapter;
30 class ACE_Sig_Set;
32 ACE_END_VERSIONED_NAMESPACE_DECL
34 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
36 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
38 class ACE_Mutex;
39 class ACE_Null_Mutex;
40 class ACE_Thread_Mutex;
41 class ACE_Recursive_Thread_Mutex;
42 class ACE_RW_Thread_Mutex;
44 ACE_END_VERSIONED_NAMESPACE_DECL
46 # include "ace/Recursive_Thread_Mutex.h"
47 #endif /* ACE_MT_SAFE */
49 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
51 // only used by ACE_OS_Object_Manager::ctor
52 # if defined (ACE_WIN32)
53 // Default WIN32 structured exception handler.
54 int ACE_SEH_Default_Exception_Selector (void *);
55 int ACE_SEH_Default_Exception_Handler (void *);
56 # endif /* ACE_WIN32 */
58 class ACE_Cleanup_Info_Node;
59 template <class T> class ACE_Cleanup_Adapter;
61 // Configuration parameters.
62 #if !defined (ACE_MAX_MANAGED_OBJECTS)
63 # define ACE_MAX_MANAGED_OBJECTS 128
64 #endif /* ! ACE_MAX_MANAGED_OBJECTS */
66 #if !defined (ACE_APPLICATION_PREALLOCATED_OBJECT_DECLARATIONS)
67 # define ACE_APPLICATION_PREALLOCATED_OBJECT_DECLARATIONS
68 #endif /* ! ACE_APPLICATION_PREALLOCATED_OBJECT_DECLARATIONS */
70 #if !defined (ACE_APPLICATION_PREALLOCATED_ARRAY_DECLARATIONS)
71 # define ACE_APPLICATION_PREALLOCATED_ARRAY_DECLARATIONS
72 #endif /* ! ACE_APPLICATION_PREALLOCATED_ARRAY_DECLARATIONS */
74 /**
75 * @class ACE_Object_Manager
77 * @brief Manager for ACE library services and singleton cleanup.
79 * The ACE_Object_Manager manages cleanup of objects, typically
80 * singletons, at program termination. In addition to managing
81 * the cleanup of the ACE library, it provides an interface for
82 * application to register objects to be cleaned up.
83 * This class also shuts down ACE library services, so that they
84 * can reclaim their storage, at program termination. It works
85 * by creating a static instance whose destructor gets called
86 * along with those of all other static objects. Hooks are
87 * provided for application code to register objects and arrays
88 * for cleanup, e.g., destruction. The order of such cleanup
89 * calls is in the reverse order of registration, i.e., that
90 * last object/array to register gets cleaned up first.
91 * The ACE_Object_Manager API includes ACE_Managed_Object. That
92 * class is contained in a separate file because it is a
93 * template class, and some compilers require that template and
94 * non-template class definitions appear in separate files.
95 * Please see ace/Managed_Object.h for a description of that
96 * part of the API. In summary, ACE_Managed_Object provides two
97 * adapters, the ACE_Cleanup_Adapter and ACE_Managed_Object
98 * template classes for adapting objects of any type to be
99 * easily managed by the ACE_Object_Manager. There are several
100 * mechanisms for adapting objects and arrays for cleanup at
101 * program termination, in roughly increasing order of ease-of-use:
102 * 1) Derive the object's class from ACE_Cleanup.
103 * 2) Allow the ACE_Object_Manager to both dynamically allocate
104 * and deallocate the object.
105 * 3) Provide an <ACE_CLEANUP_FUNC> cleanup hook for the object or
106 * array.
107 * 4) Allow the ACE_Object_Manager to both preallocate the object
108 * or array, either statically in global data or dynamically on
109 * the heap, when its singleton instance is construction.
111 * There are also several mechanisms for registering objects and
112 * arrays for cleanup. In decreasing order of flexibility and
113 * complexity (with the exception of the last mechanism):
115 * 1) ACE_Object_Manager::at_exit (void *object,
116 * ACE_CLEANUP_FUNC cleanup_hook,
117 * void *param);
118 * can be used to register any object or array for any
119 * cleanup activity at program termination.
120 * 2) ACE_Object_Manager::at_exit (ACE_Cleanup *object,
121 * void *param = 0);
122 * can be used to register an ACE_Cleanup object
123 * for any cleanup activity at program termination.
124 * The final mechanism is not general purpose, but can only
125 * be used to allocate objects and arrays at program startup:
126 * 3) ACE_Managed_Object::get_preallocated_object
127 * (ACE_Object_Manager::Preallocated_Object id);
128 * and
129 * ACE_Managed_Object::get_preallocated_array
130 * (ACE_Object_Manager::Preallocated_Array id);
131 * can only be used to allocate objects at program startup,
132 * either in global data or on the heap (selected at compile
133 * time). These are intended to replace static locks, etc.
134 * Instead of creating a static ACE_Object_Manager instance, one
135 * can alternatively be created on the stack of the main program
136 * thread. It is created just after entry to ::main (int, char
137 * *[]), and before any existing code in that function is
138 * executed. To enable this alternative, add #define
139 * ACE_HAS_NONSTATIC_OBJECT_MANAGER before including the platform
140 * specific config-* file in ace/config.h prior to
141 * building the ACE library and your applications. This #define
142 * is enabled in some config files that are supplied with ACE.
144 * To ensure a static object manager is used, #undef
145 * ACE_HAS_NONSTATIC_OBJECT_MANAGER *after* including the platform
146 * specific config-* file.
147 * Note that the ACE_Object_Manager _must_ be created before
148 * any threads are spawned by the program.
149 * If ACE_HAS_NONSTATIC_OBJECT_MANAGER is not #defined, the ACE
150 * library creates a static, singleton ACE_Object_Manager instance.
151 * The instance is placed in global program data, and constructed
152 * via a static object constructor. If ACE_HAS_NONSTATIC_OBJECT_MANAGER
153 * is #defined, the ACE_Object_Manager instance is created on the stack
154 * of the main program thread, as noted above.
156 * With ACE_HAS_NONSTATIC_OBJECT_MANAGER enabled, the ACE
157 * library has no static objects that require destruction.
158 * However, there are two drawbacks to using it:
159 * 1) main (int, char *[]) must be declared with arguments, even
160 * if they're not used. All of ACE is converted to this, so
161 * just applications have to be concerned with it.
162 * 2) If there any static objects that depend on those that are
163 * cleaned up by the Object_Manager, they'll get cleaned up too
164 * late. The ACE tests do not violate this requirement.
165 * However, applications may have trouble with it.
166 * NOTE on the use of <::exit> -- <::exit> does not destroy
167 * automatic objects. Therefore, if
168 * ACE_HAS_NONSTATIC_OBJECT_MANAGER is enabled, the
169 * ACE_Object_Manager instance will *not* be destroyed if
170 * <::exit> is called! However, <ACE_OS::exit> will properly
171 * destroy the ACE_Object_Manager. It is highly recommended
172 * that <ACE_OS::exit> be used instead of <::exit>.
174 * However, <::exit> and <ACE_OS::exit> are tricky to use
175 * properly, especially in multithread programs. It is much
176 * safer to throw an exception (or simulate that effect) that
177 * will be caught by <main> instead of calling exit. Then,
178 * <main> can perform any necessary application-specific cleanup
179 * and return the status value. In addition, it's usually best
180 * to avoid calling <::exit> and <ACE_OS::exit> from threads
181 * other than the main thread. Thanks to Jeff Greif
182 * <jmg@trivida.com> for pointing out that <::exit> doesn't
183 * destroy automatic objects, and for developing the
184 * recommendations in this paragraph.
186 * Instead of creating a static ACE_Object_Manager, or letting
187 * ACE create it on the stack of <main> for you, another
188 * alternative is to #define
189 * ACE_DOESNT_INSTANTIATE_NONSTATIC_OBJECT_MANAGER. With that
190 * #define, the application must create the ACE_Object_Manager.
191 * The recommended way is to call <ACE::init> at the start of
192 * the program, and call <ACE::fini> at the end. Alternatively,
193 * the application could explicity construct an
194 * ACE_Object_Manager.
196 class ACE_Export ACE_Object_Manager : public ACE_Object_Manager_Base
198 public:
199 ACE_ALLOC_HOOK_DECLARE;
202 * Explicitly initialize (construct the singleton instance of) the
203 * ACE_Object_Manager. Returns 0 on success, -1 on failure, and 1
204 * if it had already been called.
206 virtual int init ();
209 * Explicitly destroy the singleton instance of the
210 * ACE_Object_Manager. Returns 0 on success, -1 on failure, and 1
211 * if it had already been called.
213 virtual int fini ();
216 * Returns 1 before the ACE_Object_Manager has been constructed.
217 * This flag can be used to determine if the program is constructing
218 * static objects. If no static object spawns any threads, the
219 * program will be single-threaded when this flag returns 1. (Note
220 * that the program still might construct some static objects when
221 * this flag returns 0, if ACE_HAS_NONSTATIC_OBJECT_MANAGER is not
222 * defined.)
224 static int starting_up ();
227 * Returns 1 after the ACE_Object_Manager has been destroyed. This
228 * flag can be used to determine if the program is in the midst of
229 * destroying static objects. (Note that the program might destroy
230 * some static objects before this flag can return 1, if
231 * ACE_HAS_NONSTATIC_OBJECT_MANAGER is not defined.)
233 static int shutting_down ();
236 * Register an ACE_Cleanup object for cleanup at process
237 * termination. The object is deleted via the
238 * <ace_cleanup_destroyer>. If you need more flexibility, see the
239 * @c other at_exit method below. For OS's that do not have
240 * processes, cleanup takes place at the end of <main>. Returns 0
241 * on success. On failure, returns -1 and sets errno to: EAGAIN if
242 * shutting down, ENOMEM if insufficient virtual memory, or EEXIST
243 * if the object (or array) had already been registered.
245 static int at_exit (ACE_Cleanup *object, void *param = 0, const char* name = 0);
247 #if defined (ACE_HAS_TSS_EMULATION)
248 static int init_tss ();
249 int init_tss_i ();
250 #endif
253 * Register an object (or array) for cleanup at process termination.
254 * "cleanup_hook" points to a (global, or static member) function
255 * that is called for the object or array when it to be destroyed.
256 * It may perform any necessary cleanup specific for that object or
257 * its class. "param" is passed as the second parameter to the
258 * @a cleanup_hook function; the first parameter is the object (or
259 * array) to be destroyed. @a cleanup_hook, for example, may delete
260 * the object (or array). For OS's that do not have processes, this
261 * function is the same as <at_thread_exit>. Returns 0 on success.
262 * On failure, returns -1 and sets errno to: EAGAIN if shutting
263 * down, ENOMEM if insufficient virtual memory, or EEXIST if the
264 * object (or array) had already been registered.
266 static int at_exit (void *object,
267 ACE_CLEANUP_FUNC cleanup_hook,
268 void *param,
269 const char* name = 0);
271 static int remove_at_exit (void *object);
273 #if 0 /* not implemented yet */
274 /// Similar to at_exit(), except that the cleanup_hook is called
275 /// when the current thread exits instead of when the program terminates.
276 static int at_thread_exit (void *object,
277 ACE_CLEANUP_FUNC cleanup_hook,
278 void *param,
279 const char* name);
280 #endif /* 0 */
282 /// Unique identifiers for preallocated objects. Please see
283 /// ace/Managed_Object.h for information on accessing preallocated
284 /// objects.
285 enum Preallocated_Object
287 ACE_FILECACHE_LOCK,
288 #if defined (ACE_HAS_THREADS)
289 ACE_STATIC_OBJECT_LOCK,
290 #endif /* ACE_HAS_THREADS */
291 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
292 ACE_MT_CORBA_HANDLER_LOCK,
293 ACE_DUMP_LOCK,
294 ACE_SIG_HANDLER_LOCK,
295 ACE_SINGLETON_NULL_LOCK,
296 ACE_SINGLETON_RECURSIVE_THREAD_LOCK,
297 ACE_THREAD_EXIT_LOCK,
298 #if !defined (ACE_LACKS_ACE_TOKEN)
299 ACE_TOKEN_MANAGER_CREATION_LOCK,
300 ACE_TOKEN_INVARIANTS_CREATION_LOCK,
301 #endif /* ! ACE_LACKS_ACE_TOKEN */
302 ACE_PROACTOR_EVENT_LOOP_LOCK,
303 #endif /* ACE_MT_SAFE */
305 // Hook for preallocated objects provided by application.
306 ACE_APPLICATION_PREALLOCATED_OBJECT_DECLARATIONS
308 ACE_PREALLOCATED_OBJECTS // This enum value must be last!
311 /// Unique identifiers for preallocated arrays. Please see
312 /// ace/Managed_Object.h for information on accessing preallocated
313 /// arrays.
314 enum Preallocated_Array
316 /// There currently are no preallocated arrays in the ACE
317 /// library. If the application doesn't have any, make sure
318 /// the the preallocated_array size is at least one by declaring
319 /// this dummy . . .
320 ACE_EMPTY_PREALLOCATED_ARRAY,
322 /// Hook for preallocated arrays provided by application.
323 ACE_APPLICATION_PREALLOCATED_ARRAY_DECLARATIONS
325 ACE_PREALLOCATED_ARRAYS // This enum value must be last!
329 * @deprecated
330 * Accesses a default signal set used, for example,
331 * in ACE_Sig_Guard methods.
332 * Deprecated: use ACE_Object_Manager::default_mask () instead.
334 static ACE_Sig_Set &default_mask ();
336 private:
337 /// For at_exit support.
338 ACE_OS_Exit_Info exit_info_;
340 #if !defined (ACE_LACKS_ACE_SVCCONF)
341 /// Preallocated objects collection.
342 ACE_Object_Manager_Preallocations *preallocations_;
344 /// ACE_Service_Config signal handler.
345 ACE_Sig_Adapter *ace_service_config_sig_handler_;
346 #endif /* ! ACE_LACKS_ACE_SVCCONF */
348 /// Register an object or array for deletion at program termination.
349 /// See description of static version above for return values.
350 int at_exit_i (void *object, ACE_CLEANUP_FUNC cleanup_hook, void *param, const char* name);
352 /// Remove an object for deletion at program termination.
353 /// See description of static version above for return values.
354 int remove_at_exit_i (void *object);
356 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
357 public:
358 // = The <get_singleton_lock> accessors are for internal
359 // use by ACE_Singleton _only_.
362 * Accesses an ACE_Null_Mutex to be used for construction of
363 * ACE_Singletons. Returns 0, and the lock in the argument, on
364 * success; returns -1 on failure.
366 static int get_singleton_lock (ACE_Null_Mutex *&);
369 * Accesses a non-recursive ACE_Thread_Mutex to be used for
370 * construction of ACE_Singletons. Returns 0, and the lock in the
371 * argument, on success; returns -1 on failure.
373 static int get_singleton_lock (ACE_Thread_Mutex *&);
376 * Accesses a non-recursive ACE_Mutex to be used for construction
377 * of ACE_Singletons. Returns 0, and the lock in the argument, on
378 * success; returns -1 on failure.
380 static int get_singleton_lock (ACE_Mutex *&);
383 * Accesses a recursive ACE_Recursive_Thread_Mutex to be used for
384 * construction of ACE_Singletons. Returns 0, and the lock in the
385 * argument, on success; returns -1 on failure.
387 static int get_singleton_lock (ACE_Recursive_Thread_Mutex *&);
390 * Accesses a readers/writer ACE_RW_Thread_Mutex to be used for
391 * construction of ACE_Singletons. Returns 0, and the lock in the
392 * argument, on success; returns -1 on failure.
394 static int get_singleton_lock (ACE_RW_Thread_Mutex *&);
395 #endif /* ACE_MT_SAFE */
397 public:
398 // For internal use only by ACE_Managed_Objects.
401 * Accessor to singleton instance. Because static member functions
402 * are provided in the interface, this should not be public. However,
403 * it is public so that ACE_Managed_Object<TYPE> can access it.
405 static ACE_Object_Manager *instance ();
407 /// Table of preallocated objects.
408 static void *preallocated_object[ACE_PREALLOCATED_OBJECTS];
410 /// Table of preallocated arrays.
411 static void *preallocated_array[ACE_PREALLOCATED_ARRAYS];
413 public:
414 /// Application code should not use these explicitly, so they're
415 /// hidden here. They're public so that the ACE_Object_Manager can
416 /// be constructed/destructed in <main> with
417 /// ACE_HAS_NONSTATIC_OBJECT_MANAGER.
418 ACE_Object_Manager ();
419 ~ACE_Object_Manager ();
421 private:
422 /// Singleton pointer.
423 static ACE_Object_Manager *instance_;
425 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
426 /// Lock that is used to guard internal structures.
427 ACE_Recursive_Thread_Mutex *internal_lock_;
429 /// Null lock for guarding singleton creation.
430 ACE_Cleanup_Adapter<ACE_Null_Mutex> *singleton_null_lock_;
432 /// Lock for guarding singleton creation, when Object_Manager
433 /// hasn't been started up, or has already been shut down.
434 ACE_Cleanup_Adapter<ACE_Recursive_Thread_Mutex> *singleton_recursive_lock_;
435 #endif /* ACE_MT_SAFE */
437 #if defined (ACE_HAS_TSS_EMULATION)
438 /// Main thread's thread-specific storage array.
439 void *ts_storage_[ACE_TSS_Emulation::ACE_TSS_THREAD_KEYS_MAX];
440 bool ts_storage_initialized_;
441 #endif /* ACE_HAS_TSS_EMULATION */
443 #if !defined (ACE_HAS_NONSTATIC_OBJECT_MANAGER)
444 friend class ACE_Object_Manager_Manager;
445 #endif /* ACE_HAS_NONSTATIC_OBJECT_MANAGER */
447 ACE_Object_Manager (const ACE_Object_Manager &) = delete;
448 ACE_Object_Manager &operator= (const ACE_Object_Manager &) = delete;
449 ACE_Object_Manager (ACE_Object_Manager &&) = delete;
450 ACE_Object_Manager &operator= (ACE_Object_Manager &&) = delete;
453 ACE_END_VERSIONED_NAMESPACE_DECL
455 #include "ace/Static_Object_Lock.h"
457 #if defined (__ACE_INLINE__)
458 #include "ace/Object_Manager.inl"
459 #endif /* __ACE_INLINE__ */
461 #include "ace/Managed_Object.h"
463 #if !defined (ACE_LACKS_ACE_SVCCONF)
464 // We can't use the ACE_SVC_FACTORY_DECLARE macro here because this
465 // needs to be in the ACE_Export context rather than the
466 // ACE_Svc_Export context.
467 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
468 class ACE_Service_Object;
469 ACE_END_VERSIONED_NAMESPACE_DECL
470 ACE_FACTORY_DECLARE (ACE, ACE_Service_Manager)
471 #endif /* ! ACE_LACKS_ACE_SVCCONF */
474 #include /**/ "ace/post.h"
475 #endif /* ACE_OBJECT_MANAGER_H */