1 #include "ace/Object_Manager_Base.h"
2 #include "ace/OS_Memory.h"
3 #include "ace/OS_NS_Thread.h"
4 #include "ace/OS_NS_sys_socket.h"
5 #include "ace/OS_NS_signal.h"
6 #include "ace/OS_NS_stdio.h"
8 #if defined (ACE_HAS_ALLOC_HOOKS)
9 # include "ace/Malloc_Base.h"
10 #endif /* ACE_HAS_ALLOC_HOOKS */
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
14 #if defined (ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS)
15 int ACE_SEH_Default_Exception_Selector (void *)
17 // this is only windows and only used here,
18 // defined in ace/config-win32-common.h.
19 return (DWORD
) ACE_SEH_DEFAULT_EXCEPTION_HANDLING_ACTION
;
22 int ACE_SEH_Default_Exception_Handler (void *)
26 #endif /* ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS */
28 #if defined (ACE_HAS_ALLOC_HOOKS)
29 # define ACE_OS_PREALLOCATE_OBJECT(TYPE, ID)\
32 ACE_ALLOCATOR_RETURN (obj_p, static_cast<TYPE *>(ACE_Allocator::instance()->malloc(sizeof(TYPE))), -1); \
33 preallocated_object[ID] = (void *) obj_p;\
35 # define ACE_OS_DELETE_PREALLOCATED_OBJECT(TYPE, ID)\
36 ACE_Allocator::instance()->free (preallocated_object[ID]); \
37 preallocated_object[ID] = 0;
39 # define ACE_OS_PREALLOCATE_OBJECT(TYPE, ID)\
42 ACE_NEW_RETURN (obj_p, TYPE, -1);\
43 preallocated_object[ID] = (void *) obj_p;\
45 # define ACE_OS_DELETE_PREALLOCATED_OBJECT(TYPE, ID)\
46 delete (TYPE *) preallocated_object[ID];\
47 preallocated_object[ID] = 0;
48 #endif /* ACE_HAS_ALLOC_HOOKS */
50 ACE_Object_Manager_Base::ACE_Object_Manager_Base (void)
51 : object_manager_state_ (OBJ_MAN_UNINITIALIZED
)
52 , dynamically_allocated_ (false)
57 ACE_Object_Manager_Base::~ACE_Object_Manager_Base (void)
59 #if defined (ACE_HAS_NONSTATIC_OBJECT_MANAGER)
60 // Clear the flag so that fini () doesn't delete again.
61 dynamically_allocated_
= false;
62 #endif /* ACE_HAS_NONSTATIC_OBJECT_MANAGER */
66 ACE_Object_Manager_Base::starting_up_i ()
68 return object_manager_state_
< OBJ_MAN_INITIALIZED
;
72 ACE_Object_Manager_Base::shutting_down_i ()
74 return object_manager_state_
> OBJ_MAN_INITIALIZED
;
77 /*****************************************************************************/
81 ACE_OS_Object_Manager_Internal_Exit_Hook (void)
83 if (ACE_OS_Object_Manager::instance_
)
84 ACE_OS_Object_Manager::instance ()->fini ();
87 ACE_OS_Object_Manager
*ACE_OS_Object_Manager::instance_
= 0;
89 void *ACE_OS_Object_Manager::preallocated_object
[
90 ACE_OS_Object_Manager::ACE_OS_PREALLOCATED_OBJECTS
] = { 0 };
92 ACE_OS_Object_Manager::ACE_OS_Object_Manager (void)
96 #if defined (ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS)
97 , seh_except_selector_ (ACE_SEH_Default_Exception_Selector
)
98 , seh_except_handler_ (ACE_SEH_Default_Exception_Handler
)
99 #endif /* ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS */
101 // If instance_ was not 0, then another ACE_OS_Object_Manager has
102 // already been instantiated (it is likely to be one initialized by
103 // way of library/DLL loading). Let this one go through
104 // construction in case there really is a good reason for it (like,
105 // ACE is a static/archive library, and this one is the non-static
106 // instance (with ACE_HAS_NONSTATIC_OBJECT_MANAGER, or the user has
107 // a good reason for creating a separate one) but the original one
108 // will be the one retrieved from calls to
109 // ACE_Object_Manager::instance().
111 // Be sure that no further instances are created via instance ().
118 ACE_OS_Object_Manager::~ACE_OS_Object_Manager (void)
120 dynamically_allocated_
= false; // Don't delete this again in fini()
124 ACE_ALLOC_HOOK_DEFINE(ACE_OS_Object_Manager
)
127 ACE_OS_Object_Manager::default_mask (void)
129 return ACE_OS_Object_Manager::instance ()->default_mask_
;
133 ACE_OS_Object_Manager::thread_hook (void)
135 return ACE_OS_Object_Manager::instance ()->thread_hook_
;
138 #if defined (ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS)
139 ACE_SEH_EXCEPT_HANDLER
140 ACE_OS_Object_Manager::seh_except_selector (void)
142 return ACE_OS_Object_Manager::instance ()->seh_except_selector_
;
145 ACE_SEH_EXCEPT_HANDLER
146 ACE_OS_Object_Manager::seh_except_selector (ACE_SEH_EXCEPT_HANDLER n
)
148 ACE_OS_Object_Manager
*instance
=
149 ACE_OS_Object_Manager::instance ();
151 ACE_SEH_EXCEPT_HANDLER retv
= instance
->seh_except_selector_
;
152 instance
->seh_except_selector_
= n
;
156 ACE_SEH_EXCEPT_HANDLER
157 ACE_OS_Object_Manager::seh_except_handler (void)
159 return ACE_OS_Object_Manager::instance ()->seh_except_handler_
;
162 ACE_SEH_EXCEPT_HANDLER
163 ACE_OS_Object_Manager::seh_except_handler (ACE_SEH_EXCEPT_HANDLER n
)
165 ACE_OS_Object_Manager
*instance
=
166 ACE_OS_Object_Manager::instance ();
168 ACE_SEH_EXCEPT_HANDLER retv
= instance
->seh_except_handler_
;
169 instance
->seh_except_handler_
= n
;
172 #endif /* ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS */
175 ACE_OS_Object_Manager::thread_hook (ACE_Thread_Hook
*new_thread_hook
)
177 ACE_OS_Object_Manager
*os_om
= ACE_OS_Object_Manager::instance ();
178 ACE_Thread_Hook
*old_hook
= os_om
->thread_hook_
;
179 os_om
->thread_hook_
= new_thread_hook
;
183 ACE_OS_Object_Manager
*
184 ACE_OS_Object_Manager::instance (void)
186 // This function should be called during construction of static
187 // instances, or before any other threads have been created in the
188 // process. So, it's not thread safe.
192 ACE_OS_Object_Manager
*instance_pointer
= 0;
194 ACE_NEW_RETURN (instance_pointer
,
195 ACE_OS_Object_Manager
,
197 // I (coryan) removed it, using asserts in the OS layer
198 // brings down the Log msg stuff
199 // ACE_ASSERT (instance_pointer == instance_);
201 instance_pointer
->dynamically_allocated_
= true;
209 ACE_OS_Object_Manager::init (void)
211 if (starting_up_i ())
213 // First, indicate that this ACE_OS_Object_Manager instance is being
215 object_manager_state_
= OBJ_MAN_INITIALIZING
;
217 if (this == instance_
)
219 # if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
220 # if defined (ACE_HAS_WINCE_BROKEN_ERRNO)
221 ACE_CE_Errno::init ();
222 # endif /* ACE_HAS_WINCE_BROKEN_ERRNO */
223 ACE_OS_PREALLOCATE_OBJECT (ACE_thread_mutex_t
, ACE_OS_MONITOR_LOCK
)
224 if (ACE_OS::thread_mutex_init
225 // This line must not be broken to avoid tickling a bug with SunC++'s preprocessor.
226 (reinterpret_cast <ACE_thread_mutex_t
*> (ACE_OS_Object_Manager::preallocated_object
[ACE_OS_MONITOR_LOCK
])) != 0)
227 ACE_OS_Object_Manager::print_error_message (
228 __LINE__
, ACE_TEXT ("ACE_OS_MONITOR_LOCK"));
229 ACE_OS_PREALLOCATE_OBJECT (ACE_recursive_thread_mutex_t
,
230 ACE_TSS_CLEANUP_LOCK
)
231 if (ACE_OS::recursive_mutex_init
232 // This line must not be broken to avoid tickling a bug with SunC++'s preprocessor.
233 (reinterpret_cast <ACE_recursive_thread_mutex_t
*> (ACE_OS_Object_Manager::preallocated_object
[ACE_TSS_CLEANUP_LOCK
])) != 0)
234 ACE_OS_Object_Manager::print_error_message (
235 __LINE__
, ACE_TEXT ("ACE_TSS_CLEANUP_LOCK"));
236 ACE_OS_PREALLOCATE_OBJECT (ACE_thread_mutex_t
,
237 ACE_LOG_MSG_INSTANCE_LOCK
)
238 if (ACE_OS::thread_mutex_init
239 // This line must not be broken to avoid tickling a bug with SunC++'s preprocessor.
240 (reinterpret_cast <ACE_thread_mutex_t
*> (ACE_OS_Object_Manager::preallocated_object
[ACE_LOG_MSG_INSTANCE_LOCK
])) != 0)
241 ACE_OS_Object_Manager::print_error_message (
242 __LINE__
, ACE_TEXT ("ACE_LOG_MSG_INSTANCE_LOCK"));
243 # if defined (ACE_HAS_TSS_EMULATION)
244 ACE_OS_PREALLOCATE_OBJECT (ACE_recursive_thread_mutex_t
,
246 if (ACE_OS::recursive_mutex_init
247 // This line must not be broken to avoid tickling a bug with SunC++'s preprocessor.
248 (reinterpret_cast <ACE_recursive_thread_mutex_t
*> (ACE_OS_Object_Manager::preallocated_object
[ACE_TSS_KEY_LOCK
])) != 0)
249 ACE_OS_Object_Manager::print_error_message (
250 __LINE__
, ACE_TEXT ("ACE_TSS_KEY_LOCK"));
251 # if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
252 ACE_OS_PREALLOCATE_OBJECT (ACE_recursive_thread_mutex_t
,
254 if (ACE_OS::recursive_mutex_init
255 // This line must not be broken to avoid tickling a bug with SunC++'s preprocessor.
256 (reinterpret_cast <ACE_recursive_thread_mutex_t
*> (ACE_OS_Object_Manager::preallocated_object
[ACE_TSS_BASE_LOCK
])) != 0)
257 ACE_OS_Object_Manager::print_error_message (
258 __LINE__
, ACE_TEXT ("ACE_TSS_BASE_LOCK"));
259 # endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE */
260 # endif /* ACE_HAS_TSS_EMULATION */
261 # endif /* ACE_MT_SAFE */
263 // Open Winsock (no-op on other platforms).
264 ACE_OS::socket_init (ACE_WSOCK_VERSION
);
266 // Register the exit hook, for use by ACE_OS::exit ().
267 ACE_OS::set_exit_hook (&ACE_OS_Object_Manager_Internal_Exit_Hook
);
270 #if defined (ACE_HAS_ALLOC_HOOKS)
271 ACE_ALLOCATOR_RETURN (default_mask_
, static_cast<sigset_t
*>(ACE_Allocator::instance()->malloc(sizeof(sigset_t
))), -1);
273 ACE_NEW_RETURN (default_mask_
, sigset_t
, -1);
274 #endif /* ACE_HAS_ALLOC_HOOKS */
275 ACE_OS::sigfillset (default_mask_
);
277 // Finally, indicate that the ACE_OS_Object_Manager instance has
279 object_manager_state_
= OBJ_MAN_INITIALIZED
;
281 # if defined (ACE_WIN32) && defined (ACE_HAS_WIN32_GETVERSION)
282 /* Since MS found it necessary to deprecate these. */
283 # pragma warning(push)
284 # pragma warning(disable:4996)
285 # if defined(__clang__)
286 # pragma clang diagnostic push
287 # pragma clang diagnostic ignored "-Wdeprecated-declarations"
288 # endif /* __clang__ */
289 ACE_OS::win32_versioninfo_
.dwOSVersionInfoSize
=
290 sizeof (ACE_TEXT_OSVERSIONINFO
);
291 ACE_TEXT_GetVersionEx (&ACE_OS::win32_versioninfo_
);
292 # if defined(__clang__)
293 # pragma clang diagnostic pop
294 # endif /* __clang__ */
295 # pragma warning(pop)
296 # endif /* ACE_WIN32 */
299 // Had already initialized.
304 // Clean up an ACE_OS_Object_Manager. There can be instances of this object
305 // other than The Instance. This can happen if a user creates one for some
306 // reason. All objects clean up their per-object information and managed
307 // objects, but only The Instance cleans up the static preallocated objects.
309 ACE_OS_Object_Manager::fini (void)
311 if (instance_
== 0 || shutting_down_i ())
312 // Too late. Or, maybe too early. Either fini () has already
313 // been called, or init () was never called.
314 return object_manager_state_
== OBJ_MAN_SHUT_DOWN
? 1 : -1;
316 // No mutex here. Only the main thread should destroy the singleton
317 // ACE_OS_Object_Manager instance.
319 // Indicate that the ACE_OS_Object_Manager instance is being shut
320 // down. This object manager should be the last one to be shut
322 object_manager_state_
= OBJ_MAN_SHUTTING_DOWN
;
324 // If another Object_Manager has registered for termination, do it.
328 next_
= 0; // Protect against recursive calls.
331 // Call all registered cleanup hooks, in reverse order of
333 exit_info_
.call_hooks ();
335 // Only clean up preallocated objects when the singleton Instance is being
337 if (this == instance_
)
339 // Close down Winsock (no-op on other platforms).
340 ACE_OS::socket_fini ();
342 #if ! defined (ACE_HAS_STATIC_PREALLOCATION)
343 // Cleanup the dynamically preallocated objects.
344 # if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
345 # if !defined(ACE_HAS_BROKEN_PREALLOCATED_OBJECTS_AFTER_FORK)
346 if (ACE_OS::thread_mutex_destroy
347 // This line must not be broken to avoid tickling a bug with SunC++'s preprocessor.
348 (reinterpret_cast <ACE_thread_mutex_t
*> (ACE_OS_Object_Manager::preallocated_object
[ACE_OS_MONITOR_LOCK
])) != 0)
349 # ifdef ACE_LACKS_PTHREAD_MUTEX_DESTROY
350 if (errno
!= ENOTSUP
)
352 ACE_OS_Object_Manager::print_error_message (
353 __LINE__
, ACE_TEXT ("ACE_OS_MONITOR_LOCK"));
354 # endif /* ! ACE_HAS_BROKEN_PREALLOCATED_OBJECTS_AFTER_FORK */
355 ACE_OS_DELETE_PREALLOCATED_OBJECT (ACE_thread_mutex_t
,
357 # if !defined(ACE_HAS_BROKEN_PREALLOCATED_OBJECTS_AFTER_FORK)
358 if (ACE_OS::recursive_mutex_destroy
359 // This line must not be broken to avoid tickling a bug with SunC++'s preprocessor.
360 (reinterpret_cast <ACE_recursive_thread_mutex_t
*> (ACE_OS_Object_Manager::preallocated_object
[ACE_TSS_CLEANUP_LOCK
])) != 0)
361 # ifdef ACE_LACKS_PTHREAD_MUTEX_DESTROY
362 if (errno
!= ENOTSUP
)
364 ACE_OS_Object_Manager::print_error_message (
365 __LINE__
, ACE_TEXT ("ACE_TSS_CLEANUP_LOCK"));
366 # endif /* ! ACE_HAS_BROKEN_PREALLOCATED_OBJECTS_AFTER_FORK */
367 ACE_OS_DELETE_PREALLOCATED_OBJECT (ACE_recursive_thread_mutex_t
,
368 ACE_TSS_CLEANUP_LOCK
)
369 # if !defined(ACE_HAS_BROKEN_PREALLOCATED_OBJECTS_AFTER_FORK)
370 if (ACE_OS::thread_mutex_destroy
371 // This line must not be broken to avoid tickling a bug with SunC++'s preprocessor.
372 (reinterpret_cast <ACE_thread_mutex_t
*> (ACE_OS_Object_Manager::preallocated_object
[ACE_LOG_MSG_INSTANCE_LOCK
])) != 0)
373 # ifdef ACE_LACKS_PTHREAD_MUTEX_DESTROY
374 if (errno
!= ENOTSUP
)
376 ACE_OS_Object_Manager::print_error_message (
377 __LINE__
, ACE_TEXT ("ACE_LOG_MSG_INSTANCE_LOCK "));
378 # endif /* ! ACE_HAS_BROKEN_PREALLOCATED_OBJECTS_AFTER_FORK */
379 ACE_OS_DELETE_PREALLOCATED_OBJECT (ACE_thread_mutex_t
,
380 ACE_LOG_MSG_INSTANCE_LOCK
)
381 # if defined (ACE_HAS_TSS_EMULATION)
382 # if !defined(ACE_HAS_BROKEN_PREALLOCATED_OBJECTS_AFTER_FORK)
383 if (ACE_OS::recursive_mutex_destroy
384 // This line must not be broken to avoid tickling a bug with SunC++'s preprocessor.
385 (reinterpret_cast <ACE_recursive_thread_mutex_t
*> (ACE_OS_Object_Manager::preallocated_object
[ACE_TSS_KEY_LOCK
])) != 0)
386 ACE_OS_Object_Manager::print_error_message (
387 __LINE__
, ACE_TEXT ("ACE_TSS_KEY_LOCK"));
388 # endif /* ! ACE_HAS_BROKEN_PREALLOCATED_OBJECTS_AFTER_FORK */
389 ACE_OS_DELETE_PREALLOCATED_OBJECT (ACE_recursive_thread_mutex_t
,
391 # if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
392 # if !defined(ACE_HAS_BROKEN_PREALLOCATED_OBJECTS_AFTER_FORK)
393 if (ACE_OS::recursive_mutex_destroy
394 // This line must not be broken to avoid tickling a bug with SunC++'s preprocessor.
395 (reinterpret_cast <ACE_recursive_thread_mutex_t
*> (ACE_OS_Object_Manager::preallocated_object
[ACE_TSS_BASE_LOCK
])) != 0)
396 ACE_OS_Object_Manager::print_error_message (
397 __LINE__
, ACE_TEXT ("ACE_TSS_BASE_LOCK"));
398 # endif /* ! ACE_HAS_BROKEN_PREALLOCATED_OBJECTS_AFTER_FORK */
399 ACE_OS_DELETE_PREALLOCATED_OBJECT (ACE_recursive_thread_mutex_t
,
401 # endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE */
402 # endif /* ACE_HAS_TSS_EMULATION */
403 # if defined (ACE_HAS_WINCE_BROKEN_ERRNO)
404 ACE_CE_Errno::fini ();
405 # endif /* ACE_HAS_WINCE_BROKEN_ERRNO */
406 # endif /* ACE_MT_SAFE */
407 #endif /* ! ACE_HAS_STATIC_PREALLOCATION */
410 #if defined (ACE_HAS_ALLOC_HOOKS)
411 ACE_Allocator::instance()->free(default_mask_
);
413 delete default_mask_
;
414 #endif /* ACE_HAS_ALLOC_HOOKS */
417 // Indicate that this ACE_OS_Object_Manager instance has been shut down.
418 object_manager_state_
= OBJ_MAN_SHUT_DOWN
;
420 if (dynamically_allocated_
)
425 if (this == instance_
)
431 int ace_exit_hook_marker
= 0;
434 ACE_OS_Object_Manager::at_exit (ACE_EXIT_HOOK func
, const char* name
)
436 return exit_info_
.at_exit_i (&ace_exit_hook_marker
,
437 reinterpret_cast <ACE_CLEANUP_FUNC
> (func
),
443 ACE_OS_Object_Manager::print_error_message (unsigned int line_number
,
444 const ACE_TCHAR
*message
)
446 // To avoid duplication of these const strings in OS.o.
447 #if !defined (ACE_HAS_WINCE)
448 # ifndef ACE_LACKS_STDERR
449 fprintf (stderr
, "ace/Object_Manager_Base.cpp, line %u: %s ",
451 ACE_TEXT_ALWAYS_CHAR (message
));
453 ACE_UNUSED_ARG (line_number
);
454 ACE_UNUSED_ARG (message
);
456 # if !defined (ACE_LACKS_PERROR)
458 # endif /* ACE_LACKS_PERROR */
460 // @@ Need to use the following information.
461 ACE_UNUSED_ARG (line_number
);
462 ACE_UNUSED_ARG (message
);
464 ACE_TCHAR
*lpMsgBuf
= 0;
465 ::FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
|
466 FORMAT_MESSAGE_FROM_SYSTEM
,
469 MAKELANGID (LANG_NEUTRAL
,
472 (ACE_TCHAR
*) &lpMsgBuf
,
477 ACE_TEXT ("ACE_OS error"),
483 ACE_OS_Object_Manager::starting_up (void)
485 return ACE_OS_Object_Manager::instance_
486 ? instance_
->starting_up_i ()
491 ACE_OS_Object_Manager::shutting_down (void)
493 return ACE_OS_Object_Manager::instance_
494 ? instance_
->shutting_down_i ()
498 /*****************************************************************************/
500 #if !defined (ACE_HAS_NONSTATIC_OBJECT_MANAGER)
502 * @class ACE_OS_Object_Manager_Manager
504 * @brief Ensure that the ACE_OS_Object_Manager gets initialized at
505 * program startup, and destroyed at program termination.
507 * Without ACE_HAS_NONSTATIC_OBJECT_MANAGER, a static instance of this
508 * class is created. Therefore, it gets created before main ()
509 * is called. And it gets destroyed after main () returns.
511 class ACE_OS_Object_Manager_Manager
515 ACE_OS_Object_Manager_Manager (void);
518 ~ACE_OS_Object_Manager_Manager (void);
521 /// Save the main thread ID, so that destruction can be suppressed.
522 ACE_thread_t saved_main_thread_id_
;
525 ACE_OS_Object_Manager_Manager::ACE_OS_Object_Manager_Manager (void)
526 : saved_main_thread_id_ (ACE_OS::thr_self ())
528 // Ensure that the Object_Manager gets initialized before any
529 // application threads have been spawned. Because this will be called
530 // during construction of static objects, that should always be the
532 (void) ACE_OS_Object_Manager::instance ();
535 ACE_OS_Object_Manager_Manager::~ACE_OS_Object_Manager_Manager (void)
537 if (ACE_OS::thr_equal (ACE_OS::thr_self (),
538 saved_main_thread_id_
))
540 delete ACE_OS_Object_Manager::instance_
;
541 ACE_OS_Object_Manager::instance_
= 0;
543 // else if this destructor is not called by the main thread, then do
544 // not delete the ACE_OS_Object_Manager. That causes problems, on
548 static ACE_OS_Object_Manager_Manager ACE_OS_Object_Manager_Manager_instance
;
549 #endif /* ! ACE_HAS_NONSTATIC_OBJECT_MANAGER */
551 ACE_END_VERSIONED_NAMESPACE_DECL