summaryrefslogtreecommitdiff
path: root/ace/Object_Manager.cpp
blob: f2d24b96b9946a906ad8bc3275fa56499c0f44e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
// $Id$

#include "ace/Object_Manager.h"
#if !defined (ACE_LACKS_ACE_TOKEN)
# include "ace/Token_Manager.h"
#endif /* ! ACE_LACKS_ACE_TOKEN */
#if defined (ACE_LACKS_ACE_SVCCONF)
#  include "ace/Thread_Manager.h"
#  include "ace/Framework_Component.h"
#else  /* ! ACE_LACKS_ACE_SVCCONF */
#  include "ace/Service_Manager.h"
#  include "ace/Service_Config.h"
#endif /* ! ACE_LACKS_ACE_SVCCONF */
#include "ace/Signal.h"
#include "ace/Log_Msg.h"
#include "ace/Containers.h"
#include "ace/Synch.h"
#include "ace/Malloc.h"
#include "ace/Signal.h"

#if !defined (__ACE_INLINE__)
# include "ace/Object_Manager.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(ace, Object_Manager, "$Id$")

#if ! defined (ACE_APPLICATION_PREALLOCATED_OBJECT_DEFINITIONS)
# define ACE_APPLICATION_PREALLOCATED_OBJECT_DEFINITIONS
#endif /* ACE_APPLICATION_PREALLOCATED_OBJECT_DEFINITIONS */

#if ! defined (ACE_APPLICATION_PREALLOCATED_ARRAY_DEFINITIONS)
# define ACE_APPLICATION_PREALLOCATED_ARRAY_DEFINITIONS
#endif /* ACE_APPLICATION_PREALLOCATED_ARRAY_DEFINITIONS */

#if ! defined (ACE_APPLICATION_PREALLOCATED_OBJECT_DELETIONS)
# define ACE_APPLICATION_PREALLOCATED_OBJECT_DELETIONS
#endif /* ACE_APPLICATION_PREALLOCATED_OBJECT_DELETIONS */

#if ! defined (ACE_APPLICATION_PREALLOCATED_ARRAY_DELETIONS)
# define ACE_APPLICATION_PREALLOCATED_ARRAY_DELETIONS
#endif /* ACE_APPLICATION_PREALLOCATED_ARRAY_DELETIONS */

// Singleton pointer.
ACE_Object_Manager *ACE_Object_Manager::instance_ = 0;

void *ACE_Object_Manager::preallocated_object[
  ACE_Object_Manager::ACE_PREALLOCATED_OBJECTS] = { 0 };

void *ACE_Object_Manager::preallocated_array[
  ACE_Object_Manager::ACE_PREALLOCATED_ARRAYS] = { 0 };

// Handy macros for use by ACE_Object_Manager constructor to
// preallocate or delete an object or array, either statically (in
// global data) or dynamically (on the heap).
#if defined (ACE_HAS_STATIC_PREALLOCATION)
# define ACE_PREALLOCATE_OBJECT(TYPE, ID)\
    {\
      static ACE_Cleanup_Adapter<TYPE> obj;\
      preallocated_object[ID] = &obj;\
    }
# define ACE_PREALLOCATE_ARRAY(TYPE, ID, COUNT)\
    {\
      static ACE_Cleanup_Adapter<TYPE> obj[COUNT];\
      preallocated_array[ID] = &obj;\
    }
#else
# define ACE_PREALLOCATE_OBJECT(TYPE, ID)\
    {\
      ACE_Cleanup_Adapter<TYPE> *obj_p;\
      ACE_NEW_RETURN (obj_p, ACE_Cleanup_Adapter<TYPE>, -1);\
      preallocated_object[ID] = obj_p;\
    }
# define ACE_PREALLOCATE_ARRAY(TYPE, ID, COUNT)\
    {\
      ACE_Cleanup_Adapter<TYPE[COUNT]> *array_p;\
      ACE_NEW_RETURN (array_p, ACE_Cleanup_Adapter<TYPE[COUNT]>, -1);\
      preallocated_array[ID] = array_p;\
    }
# define ACE_DELETE_PREALLOCATED_OBJECT(TYPE, ID)\
    ace_cleanup_destroyer (\
      (ACE_Cleanup_Adapter<TYPE> *) preallocated_object[ID], 0);\
    preallocated_object[ID] = 0;
# define ACE_DELETE_PREALLOCATED_ARRAY(TYPE, ID, COUNT)\
    delete (ACE_Cleanup_Adapter<TYPE[COUNT]> *) preallocated_array[ID];\
    preallocated_array[ID] = 0;
#endif /* ACE_HAS_STATIC_PREALLOCATION */

#if !defined (ACE_LACKS_ACE_SVCCONF)

class ACE_Object_Manager_Preallocations
{
  // = TITLE
  //   Performs preallocations of certain statically allocated
  //   services needed by ACE.
public:
  ACE_Object_Manager_Preallocations (void);
  ~ACE_Object_Manager_Preallocations (void);

private:
  ACE_Static_Svc_Descriptor ace_svc_desc_ACE_Service_Manager;
};

// We can't use the ACE_SVC_FACTORY_DECLARE macro here because this
// needs to be in the ACE_Export context rather than the
// ACE_Svc_Export context.
//extern "C" ACE_Export
//ACE_Service_Object *
//_make_ACE_Service_Manager (ACE_Service_Object_Exterminator *);

ACE_Object_Manager_Preallocations::ACE_Object_Manager_Preallocations (void)
{
  ACE_STATIC_SVC_DEFINE (ACE_Service_Manager_initializer,
                         ACE_LIB_TEXT ("ACE_Service_Manager"),
                         ACE_SVC_OBJ_T,
                         &ACE_SVC_NAME (ACE_Service_Manager),
                         ACE_Service_Type::DELETE_THIS |
                           ACE_Service_Type::DELETE_OBJ,
                         0)

  // Initialize the static service objects using the descriptors created
  // above.
  ace_svc_desc_ACE_Service_Manager =
    ace_svc_desc_ACE_Service_Manager_initializer;

  // Add to the list of static configured services.
  ACE_Service_Config::static_svcs ()->
    insert (&ace_svc_desc_ACE_Service_Manager);
}

ACE_Object_Manager_Preallocations::~ACE_Object_Manager_Preallocations (void)
{
}

#endif /* ! ACE_LACKS_ACE_SVCCONF */

int
ACE_Object_Manager::starting_up (void)
{
  return ACE_Object_Manager::instance_  ?  instance_->starting_up_i ()  :  1;
}

int
ACE_Object_Manager::shutting_down (void)
{
  return ACE_Object_Manager::instance_  ?  instance_->shutting_down_i ()  :  1;
}

#if defined (ACE_DISABLE_WIN32_ERROR_WINDOWS)
// Instead of popping up a window for exceptions, just print something out
LONG _stdcall ACE_UnhandledExceptionFilter (PEXCEPTION_POINTERS pExceptionInfo)
{
  DWORD dwExceptionCode = pExceptionInfo->ExceptionRecord->ExceptionCode;

  if (dwExceptionCode == EXCEPTION_ACCESS_VIOLATION)
    ACE_ERROR ((LM_ERROR, ACE_LIB_TEXT ("\nERROR: ACCESS VIOLATION\n")));
  else
    ACE_ERROR ((LM_ERROR, ACE_LIB_TEXT ("\nERROR: UNHANDLED EXCEPTION\n")));

  return EXCEPTION_EXECUTE_HANDLER;
}
#endif /* ACE_DISABLE_WIN32_ERROR_WINDOWS */

// Initialize an ACE_Object_Manager.  There can be instances of this object
// other than The Instance.  This can happen if a user creates one for some
// reason.  All objects set up their per-object information and managed
// objects, but only The Instance sets up the static preallocated objects and
// the (static) ACE_Service_Config signal handler.
int
ACE_Object_Manager::init (void)
{
  if (starting_up_i ())
    {
      // First, indicate that the ACE_Object_Manager instance is being
      // initialized.
      object_manager_state_ = OBJ_MAN_INITIALIZING;

      // Only The Instance sets up with ACE_OS_Object_Manager and initializes
      // the preallocated objects.
      if (this == instance_)
        {
          // Make sure that the ACE_OS_Object_Manager has been created,
          // and register with it for chained fini ().
          ACE_OS_Object_Manager::instance ()->next_ = this;

#     if !defined (ACE_LACKS_ACE_SVCCONF)
          // Construct the ACE_Service_Config's signal handler.
          ACE_NEW_RETURN (ace_service_config_sig_handler_,
                     ACE_Sig_Adapter (&ACE_Service_Config::handle_signal), -1);
          ACE_Service_Config::signal_handler (ace_service_config_sig_handler_);
#     endif /* ! ACE_LACKS_ACE_SVCCONF */

          // Allocate the preallocated (hard-coded) object instances.
          ACE_PREALLOCATE_OBJECT (ACE_SYNCH_RW_MUTEX, ACE_FILECACHE_LOCK)
#     if defined (ACE_HAS_THREADS)
          ACE_PREALLOCATE_OBJECT (ACE_Recursive_Thread_Mutex,
                                  ACE_STATIC_OBJECT_LOCK)
#     endif /* ACE_HAS_THREADS */
#     if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
          ACE_PREALLOCATE_OBJECT (ACE_Thread_Mutex,
                                  ACE_MT_CORBA_HANDLER_LOCK)
          ACE_PREALLOCATE_OBJECT (ACE_Thread_Mutex, ACE_DUMP_LOCK)
          ACE_PREALLOCATE_OBJECT (ACE_Recursive_Thread_Mutex,
                                  ACE_SIG_HANDLER_LOCK)
          ACE_PREALLOCATE_OBJECT (ACE_Null_Mutex, ACE_SINGLETON_NULL_LOCK)
          ACE_PREALLOCATE_OBJECT (ACE_Recursive_Thread_Mutex,
                                  ACE_SINGLETON_RECURSIVE_THREAD_LOCK)
          ACE_PREALLOCATE_OBJECT (ACE_Thread_Mutex, ACE_THREAD_EXIT_LOCK)
#if !defined (ACE_LACKS_ACE_TOKEN) && defined (ACE_HAS_TOKENS_LIBRARY)
          ACE_PREALLOCATE_OBJECT (ACE_TOKEN_CONST::MUTEX,
                                  ACE_TOKEN_MANAGER_CREATION_LOCK)
          ACE_PREALLOCATE_OBJECT (ACE_TOKEN_CONST::MUTEX,
                                  ACE_TOKEN_INVARIANTS_CREATION_LOCK)
#endif /* ! ACE_LACKS_ACE_TOKEN && ACE_HAS_TOKENS_LIBRARY */
          ACE_PREALLOCATE_OBJECT (ACE_Thread_Mutex,
                                  ACE_PROACTOR_EVENT_LOOP_LOCK)
#     endif /* ACE_MT_SAFE */
        }

      if (this == instance_)
        {
          // Hooks for preallocated objects and arrays provided by application.
          ACE_APPLICATION_PREALLOCATED_OBJECT_DEFINITIONS
          ACE_APPLICATION_PREALLOCATED_ARRAY_DEFINITIONS

#     if defined (ACE_HAS_TSS_EMULATION)
          // Initialize the main thread's TS storage.
          ACE_TSS_Emulation::tss_open (ts_storage_);
#     endif /* ACE_HAS_TSS_EMULATION */

#if defined (ACE_DISABLE_WIN32_ERROR_WINDOWS)
#if defined (_DEBUG) && defined (_MSC_VER)
          // This will keep the ACE_Assert window
           _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
          _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDERR );
#endif /* _DEBUG && _MSC_VER */

          // And this will catch all unhandled exceptions.
          SetUnhandledExceptionFilter (&ACE_UnhandledExceptionFilter);
#endif /* ACE_DISABLE_WIN32_ERROR_WINDOWS */



#     if !defined (ACE_LACKS_ACE_SVCCONF)
          ACE_NEW_RETURN (preallocations_,
                          ACE_Object_Manager_Preallocations,
                          -1);
#     endif /* ! ACE_LACKS_ACE_SVCCONF */

          // Open the main thread's ACE_Log_Msg.
          (void) ACE_LOG_MSG;
        }

      // Finally, indicate that the ACE_Object_Manager instance has
      // been initialized.
      object_manager_state_ = OBJ_MAN_INITIALIZED;

      return 0;
    } else {
      // Had already initialized.
      return 1;
    }
}

ACE_Object_Manager::ACE_Object_Manager (void)
  // With ACE_HAS_TSS_EMULATION, ts_storage_ is initialized by the call to
  // ACE_OS::tss_open () in the function body.
  : exit_info_ ()
#if !defined (ACE_LACKS_ACE_SVCCONF)
  , preallocations_ (0)
  , ace_service_config_sig_handler_ (0)
#endif /* ! ACE_LACKS_ACE_SVCCONF */
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  , singleton_null_lock_ (0)
  , singleton_recursive_lock_ (0)
# endif /* ACE_MT_SAFE */
{
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  ACE_NEW (internal_lock_, ACE_Recursive_Thread_Mutex);
# endif /* ACE_MT_SAFE */

  // If instance_ was not 0, then another ACE_Object_Manager has
  // already been instantiated (it is likely to be one initialized by way
  // of library/DLL loading).  Let this one go through construction in
  // case there really is a good reason for it (like, ACE is a static/archive
  // library, and this one is the non-static instance (with
  // ACE_HAS_NONSTATIC_OBJECT_MANAGER, or the user has a good reason for
  // creating a separate one) but the original one will be the one retrieved
  // from calls to ACE_Object_Manager::instance().

  // Be sure that no further instances are created via instance ().
  if (instance_ == 0)
    instance_ = this;

  init ();
}

ACE_Object_Manager::~ACE_Object_Manager (void)
{
  dynamically_allocated_ = 0;   // Don't delete this again in fini()
  fini ();
}

ACE_Object_Manager *
ACE_Object_Manager::instance (void)
{
  // This function should be called during construction of static
  // instances, or before any other threads have been created in
  // the process.  So, it's not thread safe.

  if (instance_ == 0)
    {
      ACE_Object_Manager *instance_pointer;

      ACE_NEW_RETURN (instance_pointer,
                      ACE_Object_Manager,
                      0);
      ACE_ASSERT (instance_pointer == instance_);

      instance_pointer->dynamically_allocated_ = 1;

      return instance_pointer;
    }
  else
    return instance_;
}

int
ACE_Object_Manager::at_exit_i (void *object,
                               ACE_CLEANUP_FUNC cleanup_hook,
                               void *param)
{
  ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
    *instance_->internal_lock_, -1));

  if (shutting_down_i ())
    {
      errno = EAGAIN;
      return -1;
    }

  if (exit_info_.find (object))
    {
      // The object has already been registered.
      errno = EEXIST;
      return -1;
    }

  return exit_info_.at_exit_i (object, cleanup_hook, param);
}

#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)

int
ACE_Object_Manager::get_singleton_lock (ACE_Null_Mutex *&lock)
{
  if (starting_up ()  ||  shutting_down ())
    {
      // The preallocated lock has not been constructed yet.
      // Therefore, the program is single-threaded at this point.  Or,
      // the ACE_Object_Manager instance has been destroyed, so the
      // preallocated lock is not available.  Allocate a lock to use,
      // for interface compatibility, though there should be no
      // contention on it.
      if (ACE_Object_Manager::instance ()->singleton_null_lock_ == 0)
        {
          ACE_NEW_RETURN (ACE_Object_Manager::instance ()->
                            singleton_null_lock_,
                          ACE_Cleanup_Adapter<ACE_Null_Mutex>,
                          -1);

          // Can't register with the ACE_Object_Manager here!  The
          // lock's declaration is visible to the ACE_Object_Manager
          // destructor, so it will clean it up as a special case.
        }

      if (ACE_Object_Manager::instance ()->singleton_null_lock_ != 0)
        lock = &ACE_Object_Manager::instance ()->singleton_null_lock_->
          object ();
    }
  else
    // Use the Object_Manager's preallocated lock.
    lock = ACE_Managed_Object<ACE_Null_Mutex>::get_preallocated_object
      (ACE_Object_Manager::ACE_SINGLETON_NULL_LOCK);

  return 0;
}

int
ACE_Object_Manager::get_singleton_lock (ACE_Thread_Mutex *&lock)
{
  if (lock == 0)
    {
      if (starting_up () || shutting_down ())
        {
          // The Object_Manager and its internal lock have not been
          // constructed yet.  Therefore, the program is single-
          // threaded at this point.  Or, the ACE_Object_Manager
          // instance has been destroyed, so the internal lock is not
          // available.  Either way, we can not use double-checked
          // locking.  So, we'll leak the lock.
          ACE_NEW_RETURN (lock,
                          ACE_Thread_Mutex,
                          -1);
        }
      else
        {
          // Allocate a new lock, but use double-checked locking to
          // ensure that only one thread allocates it.
          ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex,
                                    ace_mon,
                                    *ACE_Object_Manager::instance ()->
                                    internal_lock_,
                                    -1));

          if (lock == 0)
            {
              ACE_Cleanup_Adapter<ACE_Thread_Mutex> *lock_adapter;
              ACE_NEW_RETURN (lock_adapter,
                              ACE_Cleanup_Adapter<ACE_Thread_Mutex>,
                              -1);
              lock = &lock_adapter->object ();

              // Register the lock for destruction at program
              // termination.  This call will cause us to grab the
              // ACE_Object_Manager::instance ()->internal_lock_
              // again; that's why it is a recursive lock.
              ACE_Object_Manager::at_exit (lock_adapter);
            }
        }
    }

  return 0;
}

int
ACE_Object_Manager::get_singleton_lock (ACE_Mutex *&lock)
{
  if (lock == 0)
    {
      if (starting_up ()  ||  shutting_down ())
        {
          // The Object_Manager and its internal lock have not been
          // constructed yet.  Therefore, the program is single-
          // threaded at this point.  Or, the ACE_Object_Manager
          // instance has been destroyed, so the internal lock is not
          // available.  Either way, we can not use double-checked
          // locking.  So, we'll leak the lock.

          ACE_NEW_RETURN (lock,
                          ACE_Mutex,
                          -1);
        }
      else
        {
          // Allocate a new lock, but use double-checked locking to
          // ensure that only one thread allocates it.
          ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex,
                                    ace_mon,
                                    *ACE_Object_Manager::instance ()->
                                      internal_lock_,
                                    -1));

          if (lock == 0)
            {
              ACE_Cleanup_Adapter<ACE_Mutex> *lock_adapter;
              ACE_NEW_RETURN (lock_adapter,
                              ACE_Cleanup_Adapter<ACE_Mutex>,
                              -1);
              lock = &lock_adapter->object ();

              // Register the lock for destruction at program
              // termination.  This call will cause us to grab the
              // ACE_Object_Manager::instance ()->internal_lock_
              // again; that's why it is a recursive lock.
              ACE_Object_Manager::at_exit (lock_adapter);
            }
        }
    }

  return 0;
}

int
ACE_Object_Manager::get_singleton_lock (ACE_Recursive_Thread_Mutex *&lock)
{
  if (starting_up ()  ||  shutting_down ())
    {
      // The preallocated lock has not been constructed yet.
      // Therefore, the program is single-threaded at this point.  Or,
      // the ACE_Object_Manager instance has been destroyed, so the
      // preallocated lock is not available.  Allocate a lock to use,
      // for interface compatibility, though there should be no
      // contention on it.
      if (ACE_Object_Manager::instance ()->singleton_recursive_lock_ == 0)
        ACE_NEW_RETURN (ACE_Object_Manager::instance ()->
                          singleton_recursive_lock_,
                        ACE_Cleanup_Adapter<ACE_Recursive_Thread_Mutex>,
                        -1);

      // Can't register with the ACE_Object_Manager here!  The lock's
      // declaration is visible to the ACE_Object_Manager destructor,
      // so it will clean it up as a special case.

      if (ACE_Object_Manager::instance ()->singleton_recursive_lock_ != 0)
        lock = &ACE_Object_Manager::instance ()->singleton_recursive_lock_->
          object ();
    }
  else
    {
      // Use the Object_Manager's preallocated lock.
      lock = ACE_Managed_Object<ACE_Recursive_Thread_Mutex>::
        get_preallocated_object (ACE_Object_Manager::
                                 ACE_SINGLETON_RECURSIVE_THREAD_LOCK);
    }

  return 0;
}

int
ACE_Object_Manager::get_singleton_lock (ACE_RW_Thread_Mutex *&lock)
{
  if (lock == 0)
    {
      if (starting_up () || shutting_down ())
        {
          // The Object_Manager and its internal lock have not been
          // constructed yet.  Therefore, the program is single-
          // threaded at this point.  Or, the ACE_Object_Manager
          // instance has been destroyed, so the internal lock is not
          // available.  Either way, we can not use double-checked
          // locking.  So, we'll leak the lock.

          ACE_NEW_RETURN (lock,
                          ACE_RW_Thread_Mutex,
                          -1);
        }
      else
        {
          // Allocate a new lock, but use double-checked locking to
          // ensure that only one thread allocates it.
          ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex,
                                    ace_mon,
                                    *ACE_Object_Manager::instance ()->
                                    internal_lock_,
                                    -1));

          if (lock == 0)
            {
              ACE_Cleanup_Adapter<ACE_RW_Thread_Mutex> *lock_adapter;
              ACE_NEW_RETURN (lock_adapter,
                              ACE_Cleanup_Adapter<ACE_RW_Thread_Mutex>,
                              -1);
              lock = &lock_adapter->object ();


              // Register the lock for destruction at program
              // termination.  This call will cause us to grab the
              // ACE_Object_Manager::instance ()->internal_lock_
              // again; that's why it is a recursive lock.
              ACE_Object_Manager::at_exit (lock_adapter);
            }
        }
    }

  return 0;
}
#endif /* ACE_MT_SAFE */

// NOTE:  this function needs to appear _after_ the
// get_singleton_lock () functions in order to compile with
// g++ 2.7.2.3.
//
// Clean up an ACE_Object_Manager.  There can be instances of this object
// other than The Instance.  This can happen if (on Win32) the ACE DLL
// causes one to be created, or if a user creates one for some reason.
// Only The Instance cleans up the static preallocated objects.  All objects
// clean up their per-object information and managed objects.
int
ACE_Object_Manager::fini (void)
{
  if (shutting_down_i ())
    // Too late.  Or, maybe too early.  Either fini () has already
    // been called, or init () was never called.
    return object_manager_state_ == OBJ_MAN_SHUT_DOWN  ?  1  :  -1;

  // No mutex here.  Only the main thread should destroy the singleton
  // ACE_Object_Manager instance.

  // First, indicate that this ACE_Object_Manager instance is being
  // shut down.
  object_manager_state_ = OBJ_MAN_SHUTTING_DOWN;

  // Call all registered cleanup hooks, in reverse order of
  // registration.
  exit_info_.call_hooks ();

  if (this == instance_)
    {
#if !defined (ACE_LACKS_ACE_SVCCONF)
      delete preallocations_;
      preallocations_ = 0;
#endif /* ! ACE_LACKS_ACE_SVCCONF */

      ACE_Trace::stop_tracing ();

#if defined (ACE_LACKS_ACE_SVCCONF)

      ACE_Framework_Repository::close_singleton ();

#  if ! defined (ACE_THREAD_MANAGER_LACKS_STATICS)
      ACE_Thread_Manager::close_singleton ();
#  endif /* ! ACE_THREAD_MANAGER_LACKS_STATICS */

#else /* ! ACE_LACKS_ACE_SVCCONF */

      // Close and possibly delete all service instances in the Service
      // Repository.
      ACE_Service_Config::fini_svcs ();

#endif /* ! ACE_LACKS_ACE_SVCCONF */



      // Close the main thread's TSS, including its Log_Msg instance.
      ACE_OS::cleanup_tss (1 /* main thread */);

      //
      // Note:  Do not access Log Msg after this since it is gone
      //

#if !defined (ACE_LACKS_ACE_SVCCONF)
      // Unlink all services in the Service Repository and close/delete
      // all ACE library services and singletons.
      ACE_Service_Config::close ();
#endif /* ! ACE_LACKS_ACE_SVCCONF */

      // Close the ACE_Allocator.
      ACE_Allocator::close_singleton ();

#if ! defined (ACE_HAS_STATIC_PREALLOCATION)
      // Hooks for deletion of preallocated objects and arrays provided by
      // application.
      ACE_APPLICATION_PREALLOCATED_ARRAY_DELETIONS
      ACE_APPLICATION_PREALLOCATED_OBJECT_DELETIONS

      // Cleanup the dynamically preallocated arrays.
      // (none)

      // Cleanup the dynamically preallocated objects.
      ACE_DELETE_PREALLOCATED_OBJECT (ACE_SYNCH_RW_MUTEX, ACE_FILECACHE_LOCK)
#if defined (ACE_HAS_THREADS)
      ACE_DELETE_PREALLOCATED_OBJECT (ACE_Recursive_Thread_Mutex,
                                      ACE_STATIC_OBJECT_LOCK)
#endif /* ACE_HAS_THREADS */
# if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
      ACE_DELETE_PREALLOCATED_OBJECT (ACE_Thread_Mutex,
                                      ACE_MT_CORBA_HANDLER_LOCK)
      ACE_DELETE_PREALLOCATED_OBJECT (ACE_Thread_Mutex, ACE_DUMP_LOCK)
      ACE_DELETE_PREALLOCATED_OBJECT (ACE_Recursive_Thread_Mutex,
                                      ACE_SIG_HANDLER_LOCK)
      ACE_DELETE_PREALLOCATED_OBJECT (ACE_Null_Mutex,
                                      ACE_SINGLETON_NULL_LOCK)
      ACE_DELETE_PREALLOCATED_OBJECT (ACE_Recursive_Thread_Mutex,
                                      ACE_SINGLETON_RECURSIVE_THREAD_LOCK)
      ACE_DELETE_PREALLOCATED_OBJECT (ACE_Thread_Mutex, ACE_THREAD_EXIT_LOCK)
#if !defined (ACE_LACKS_ACE_TOKEN) && defined (ACE_HAS_TOKENS_LIBRARY)
      ACE_DELETE_PREALLOCATED_OBJECT (ACE_TOKEN_CONST::MUTEX,
                                      ACE_TOKEN_MANAGER_CREATION_LOCK)
      ACE_DELETE_PREALLOCATED_OBJECT (ACE_TOKEN_CONST::MUTEX,
                                      ACE_TOKEN_INVARIANTS_CREATION_LOCK)
#endif /* ! ACE_LACKS_ACE_TOKEN && ACE_HAS_TOKENS_LIBRARY */
      ACE_DELETE_PREALLOCATED_OBJECT (ACE_Thread_Mutex,
                                      ACE_PROACTOR_EVENT_LOOP_LOCK)
# endif /* ACE_MT_SAFE */
#endif /* ! ACE_HAS_STATIC_PREALLOCATION */

#if defined (ACE_HAS_THREADS)
      ACE_Static_Object_Lock::cleanup_lock ();
#endif /* ACE_HAS_THREADS */
    }

#if !defined (ACE_LACKS_ACE_SVCCONF)
  delete ace_service_config_sig_handler_;
  ace_service_config_sig_handler_ = 0;
#endif /* ! ACE_LACKS_ACE_SVCCONF */

#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  delete internal_lock_;
  internal_lock_ = 0;

  delete singleton_null_lock_;
  singleton_null_lock_ = 0;

  delete singleton_recursive_lock_;
  singleton_recursive_lock_ = 0;
#endif /* ACE_MT_SAFE */

  // Indicate that this ACE_Object_Manager instance has been shut down.
  object_manager_state_ = OBJ_MAN_SHUT_DOWN;

  // Then, ensure that the ACE_OS_Object_Manager gets shut down.
  if (this == instance_ && ACE_OS_Object_Manager::instance_)
    ACE_OS_Object_Manager::instance_->fini ();

  if (dynamically_allocated_)
    {
      delete this;
    }

  if (this == instance_)
    instance_ = 0;

  return 0;
}


#if !defined (ACE_HAS_NONSTATIC_OBJECT_MANAGER)
class ACE_Export ACE_Object_Manager_Manager
  // = TITLE
  //    Ensure that the <ACE_Object_Manager> gets initialized at
  //    program startup, and destroyed at program termination.
  //
  // = DESCRIPTION
  //    Without ACE_HAS_NONSTATIC_OBJECT_MANAGER, a static instance of this
  //    class is created.  Therefore, it gets created before main ()
  //    is called.  And it gets destroyed after main () returns.
{
public:
  ACE_Object_Manager_Manager (void);
  ~ACE_Object_Manager_Manager (void);

private:
  ACE_thread_t saved_main_thread_id_;
  // Save the main thread ID, so that destruction can be suppressed.
};

ACE_Object_Manager_Manager::ACE_Object_Manager_Manager (void)
  : saved_main_thread_id_ (ACE_OS::thr_self ())
{
  // Ensure that the Object_Manager gets initialized before any
  // application threads have been spawned.  Because this will be called
  // during construction of static objects, that should always be the
  // case.
  (void) ACE_Object_Manager::instance ();
}

ACE_Object_Manager_Manager::~ACE_Object_Manager_Manager (void)
{
  if (ACE_OS::thr_equal (ACE_OS::thr_self (),
                         saved_main_thread_id_))
    {
      delete ACE_Object_Manager::instance_;
      ACE_Object_Manager::instance_ = 0;
    }
  // else if this destructor is not called by the main thread, then do
  // not delete the ACE_Object_Manager.  That causes problems, on
  // WIN32 at least.
}

static ACE_Object_Manager_Manager ACE_Object_Manager_Manager_instance;
#endif /* ! ACE_HAS_NONSTATIC_OBJECT_MANAGER */

#if defined (ACE_HAS_THREADS)

// hack to get around errors while compiling using split-cpp
#if !defined (ACE_IS_SPLITTING)
// This is global so that it doesn't have to be declared in the header
// file.  That would cause nasty circular include problems.
typedef ACE_Cleanup_Adapter<ACE_Recursive_Thread_Mutex> ACE_Static_Object_Lock_Type;
static ACE_Static_Object_Lock_Type *ACE_Static_Object_Lock_lock = 0;
#endif /* ! ACE_IS_SPLITTING */

// ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK isn't (currently) used by ACE.
// But, applications may find it useful for avoiding recursive calls
// if they have overridden operator new.  Thanks to Jody Hagins
// <jody@atdesk.com> for contributing it.

ACE_Recursive_Thread_Mutex *
ACE_Static_Object_Lock::instance (void)
{
  if (ACE_Object_Manager::starting_up ()  ||
      ACE_Object_Manager::shutting_down ())
    {
      // The preallocated ACE_STATIC_OBJECT_LOCK has not been
      // constructed yet.  Therefore, the program is single-threaded
      // at this point.  Or, the ACE_Object_Manager instance has been
      // destroyed, so the preallocated lock is not available.
      // Allocate a lock to use, for interface compatibility, though
      // there should be no contention on it.
      if (ACE_Static_Object_Lock_lock == 0)
        {
#     if defined (ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK)
        // Allocate a buffer with malloc, and then use placement
        // new for the object, on the malloc'd buffer.
        void *buffer =
          ACE_OS::malloc (sizeof (*ACE_Static_Object_Lock_lock));
        if (buffer == 0)
          {
            return 0;
          }
        ACE_NEW_RETURN (ACE_Static_Object_Lock_lock,
                        (buffer) ACE_Static_Object_Lock_Type (),
                        0);
#       else   /* ! ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK */
        ACE_NEW_RETURN (ACE_Static_Object_Lock_lock,
                        ACE_Cleanup_Adapter<ACE_Recursive_Thread_Mutex>,
                        0);
#       endif /* ! ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK */
        }

      // Can't register with the ACE_Object_Manager here!  The lock's
      // declaration is visible to the ACE_Object_Manager destructor,
      // so it will clean it up as a special case.

      return &ACE_Static_Object_Lock_lock->object ();
    }
  else
    // Return the preallocated ACE_STATIC_OBJECT_LOCK.
    return
      ACE_Managed_Object<ACE_Recursive_Thread_Mutex>::get_preallocated_object
        (ACE_Object_Manager::ACE_STATIC_OBJECT_LOCK);
}

void
ACE_Static_Object_Lock::cleanup_lock (void)
{
# if defined(ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK)
    // It was malloc'd, so we need to explicitly call the dtor
    // and then free the memory.
    ACE_DES_FREE (ACE_Static_Object_Lock_lock,
                  ACE_OS::free,
                  ACE_Static_Object_Lock_Type);
# else  /* ! ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK */
    delete ACE_Static_Object_Lock_lock;
# endif /* ! ACE_SHOULD_MALLOC_STATIC_OBJECT_LOCK */
    ACE_Static_Object_Lock_lock = 0;
}
#endif /* ACE_HAS_THREADS */

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
# if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
    template class ACE_Cleanup_Adapter<ACE_Null_Mutex>;
    template class ACE_Cleanup_Adapter<ACE_Mutex>;
    template class ACE_Cleanup_Adapter<ACE_Recursive_Thread_Mutex>;
    template class ACE_Cleanup_Adapter<ACE_Thread_Mutex>;
    template class ACE_Managed_Object<ACE_Null_Mutex>;
    template class ACE_Managed_Object<ACE_Mutex>;
    template class ACE_Managed_Object<ACE_Recursive_Thread_Mutex>;
    template class ACE_Managed_Object<ACE_Thread_Mutex>;
# endif /* ACE_MT_SAFE */
  template class ACE_Cleanup_Adapter<ACE_SYNCH_RW_MUTEX>;
  template class ACE_Managed_Object<ACE_SYNCH_RW_MUTEX>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
# if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
#   pragma instantiate ACE_Cleanup_Adapter<ACE_Null_Mutex>
#   pragma instantiate ACE_Cleanup_Adapter<ACE_Mutex>
#   pragma instantiate ACE_Cleanup_Adapter<ACE_Recursive_Thread_Mutex>
#   pragma instantiate ACE_Cleanup_Adapter<ACE_Thread_Mutex>
#   pragma instantiate ACE_Managed_Object<ACE_Null_Mutex>
#   pragma instantiate ACE_Managed_Object<ACE_Mutex>
#   pragma instantiate ACE_Managed_Object<ACE_Recursive_Thread_Mutex>
#   pragma instantiate ACE_Managed_Object<ACE_Thread_Mutex>
# endif /* ACE_MT_SAFE */
# pragma instantiate ACE_Cleanup_Adapter<ACE_SYNCH_RW_MUTEX>
# pragma instantiate ACE_Managed_Object<ACE_SYNCH_RW_MUTEX>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */