summaryrefslogtreecommitdiff
path: root/ace/Thread_Manager.cpp
blob: 4e48a0aeef7ba5f80ea288554bf120738f020e39 (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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
// Thread_Manager.cpp
// $Id$

#define ACE_BUILD_DLL
#include "ace/Synch_T.h"
#include "ace/Thread_Manager.h"

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

ACE_ALLOC_HOOK_DEFINE(ACE_Thread_Control)
ACE_ALLOC_HOOK_DEFINE(ACE_Thread_Manager)

// Process-wide Thread Manager.
ACE_Thread_Manager *ACE_Thread_Manager::thr_mgr_ = 0;

// Controls whether the Thread_Manager is deleted when we shut down
// (we can only delete it safely if we created it!)
int ACE_Thread_Manager::delete_thr_mgr_ = 0;

void
ACE_Thread_Manager::dump (void) const
{
  ACE_TRACE ("ACE_Thread_Manager::dump");
  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));

  ACE_DEBUG ((LM_DEBUG, "\ngrp_id_ = %d", this->grp_id_));
  ACE_DEBUG ((LM_DEBUG, "\nmax_table_size_ = %d", this->max_table_size_));
  ACE_DEBUG ((LM_DEBUG, "\ncurrent_count_ = %d", this->current_count_));

  for (size_t i = 0; i < this->current_count_; i++)
    this->thr_table_[i].dump ();

  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}

void
ACE_Thread_Descriptor::dump (void) const
{
  ACE_TRACE ("ACE_Thread_Descriptor::dump");
  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));

  ACE_DEBUG ((LM_DEBUG, "\nthr_id_ = %d", this->thr_id_));
  ACE_DEBUG ((LM_DEBUG, "\nthr_handle_ = %d", this->thr_handle_));
  ACE_DEBUG ((LM_DEBUG, "\ngrp_id_ = %d", this->grp_id_));
  ACE_DEBUG ((LM_DEBUG, "\nthr_state_ = %d", this->thr_state_));

  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}

ACE_Thread_Descriptor::ACE_Thread_Descriptor (void)
  : thr_id_ (ACE_OS::NULL_thread),
    grp_id_ (0),
    thr_state_ (ACE_THR_IDLE)
{
  ACE_TRACE ("ACE_Thread_Descriptor::ACE_Thread_Descriptor");
}

// The following macro simplifies subsequence code.
#define ACE_FIND(OP,INDEX) \
  int INDEX = OP; \
  if (INDEX == -1) return -1

int 
ACE_Thread_Manager::thread_descriptor (ACE_thread_t thr_id,
				       ACE_Thread_Descriptor &descriptor)
{
  ACE_TRACE ("ACE_Thread_Descriptor::thread_descriptor");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  ACE_FIND (this->find_thread (thr_id), index);
  descriptor = this->thr_table_[index];
  return 0;
}


int 
ACE_Thread_Manager::hthread_descriptor (ACE_hthread_t thr_handle, 
					ACE_Thread_Descriptor &descriptor)
{
  ACE_TRACE ("ACE_Thread_Descriptor::hthread_descriptor");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  ACE_FIND (this->find_hthread (thr_handle), index);
  descriptor = this->thr_table_[index];
  return 0;
}

// Return the thread descriptor (indexed by ACE_hthread_t).

int 
ACE_Thread_Manager::thr_self (ACE_hthread_t &self)
{
  ACE_TRACE ("ACE_Thread_Descriptor::thr_self");  
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  // Try to get the cached HANDLE out of TSS to avoid lookup.
  ACE_hthread_t *handle = ACE_LOG_MSG->thr_handle ();

  // Wasn't in the cache, so we'll have to look it up and cache it.
  if (handle == 0) 
    { 
      ACE_thread_t id = ACE_OS::thr_self ();
      
      ACE_FIND (this->find_thread (id), index);
      handle = &this->thr_table_[index].thr_handle_;

      // Update the TSS cache. 
      ACE_LOG_MSG->thr_handle (handle);
    } 
  self = *handle;
  return 0;
}

int
ACE_Thread_Manager::resize (size_t size)
{
  ACE_TRACE ("ACE_Thread_Manager::resize");
  ACE_Thread_Descriptor *temp;
  
  ACE_NEW_RETURN (temp, ACE_Thread_Descriptor[size], -1);

  for (size_t i = 0; i < this->max_table_size_; i++)
    temp[i] = this->thr_table_[i]; // Structure assignment.

  this->max_table_size_ = size;

  delete [] this->thr_table_;

  this->thr_table_ = temp;
  return 0;
}

// Create and initialize the table to keep track of the thread pool.

int
ACE_Thread_Manager::open (size_t size)
{
  ACE_TRACE ("ACE_Thread_Manager::open");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  if (this->max_table_size_ < size)
    this->resize (size);
  return 0;
}

// Initialize the synchronization variables.

ACE_Thread_Manager::ACE_Thread_Manager (size_t size)
  : thr_table_ (0),
    max_table_size_ (0), 
    current_count_ (0),
    grp_id_ (1)
#if defined (ACE_HAS_THREADS)
    , zero_cond_ (lock_)
#endif /* ACE_HAS_THREADS */
{
  ACE_TRACE ("ACE_Thread_Manager::ACE_Thread_Manager");
  if (this->open (size) == -1)
    ACE_ERROR ((LM_ERROR, "%p\n", "ACE_Thread_Manager"));
}

ACE_Thread_Manager *
ACE_Thread_Manager::instance (void)
{
  ACE_TRACE ("ACE_Thread_Manager::instance");

  if (ACE_Thread_Manager::thr_mgr_ == 0)
    {
      // Perform Double-Checked Locking Optimization.
      ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
				*ACE_Static_Object_Lock::instance (), 0));

      if (ACE_Thread_Manager::thr_mgr_ == 0)
	{
	  ACE_NEW_RETURN (ACE_Thread_Manager::thr_mgr_, ACE_Thread_Manager, 0);
	  ACE_Thread_Manager::delete_thr_mgr_ = 1;
	}
    }

  return ACE_Thread_Manager::thr_mgr_;
}

ACE_Thread_Manager *
ACE_Thread_Manager::instance (ACE_Thread_Manager *tm)
{
  ACE_TRACE ("ACE_Thread_Manager::instance");

  ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
			    *ACE_Static_Object_Lock::instance (), 0));

  ACE_Thread_Manager *t = ACE_Thread_Manager::thr_mgr_;
  // We can't safely delete it since we don't know who created it!
  ACE_Thread_Manager::delete_thr_mgr_ = 0;

  ACE_Thread_Manager::thr_mgr_ = tm;
  return t;
}

void
ACE_Thread_Manager::close_singleton (void)
{
  ACE_TRACE ("ACE_Thread_Manager::close_singleton");

  ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
		     *ACE_Static_Object_Lock::instance ()));

  if (ACE_Thread_Manager::delete_thr_mgr_)
    {
      delete ACE_Thread_Manager::thr_mgr_;
      ACE_Thread_Manager::thr_mgr_ = 0;
      ACE_Thread_Manager::delete_thr_mgr_ = 0;
    }
}

// Close up and release all resources.

int
ACE_Thread_Manager::close (void)
{
  ACE_TRACE ("ACE_Thread_Manager::close");

  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  if (this->thr_table_ != 0)
    {
      delete [] this->thr_table_;
      this->thr_table_ = 0;
      this->max_table_size_ = 0;
      this->current_count_ = 0;
    }
  return 0;
}

ACE_Thread_Manager::~ACE_Thread_Manager (void)
{
  ACE_TRACE ("ACE_Thread_Manager::~ACE_Thread_Manager");
  this->close ();
}

// Run the thread entry point for the <ACE_Thread_Adapter>.  This must
// be an extern "C" to make certain compilers happy...

extern "C" void *
ace_thread_manager_adapter (void *args)
{
  ACE_Thread_Adapter *thread_args = (ACE_Thread_Adapter *) args;

  // This really needs to go into TSS!
  ACE_Thread_Control tc (thread_args->thr_mgr ());

  // Invoke the user-supplied function with the args.
  return thread_args->invoke ();
}

// Call the appropriate OS routine to spawn a thread.  Should *not* be
// called with the lock_ held...

int 
ACE_Thread_Manager::spawn_i (ACE_THR_FUNC func, 
			     void *args, 
			     long flags, 
			     ACE_thread_t *t_id, 
			     ACE_hthread_t *t_handle,
			     long priority,
			     int grp_id,
			     void *stack, 
			     size_t stack_size,
			     ACE_Task_Base *task)
{
  ACE_Thread_Adapter *thread_args = 0;
#if !defined (ACE_NO_THREAD_ADAPTER)
  ACE_NEW_RETURN (thread_args,
		  ACE_Thread_Adapter (func,
				      args,
				      (ACE_THR_C_FUNC) ace_thread_manager_adapter,
				      this),
		  -1);
#endif /* ACE_NO_THREAD_ADAPTER */

  ACE_TRACE ("ACE_Thread_Manager::spawn_i");
  ACE_thread_t thr_id;
  ACE_hthread_t thr_handle;

  if (t_id == 0)
    t_id = &thr_id;

  if (t_handle == 0)
    t_handle = &thr_handle;
  
  int result = ACE_Thread::spawn (func,
				  args,
				  flags,
				  t_id,
				  t_handle,
				  priority,
				  stack,
				  stack_size,
				  thread_args);
  if (result != 0)
    {
      // _Don't_ clobber errno here!  result is either 0 or -1, and
      // ACE_OS::thr_create () already set errno!  D. Levine 28 Mar
      // 1997 errno = result;
      return -1;
    }
  else
    return this->append_thr (*t_id, *t_handle, 
			     ACE_THR_SPAWNED, 
			     grp_id, task);
}

// Create a new thread running <func>.  *Must* be called with the
// <lock_> held...

int 
ACE_Thread_Manager::spawn (ACE_THR_FUNC func, 
			   void *args, 
			   long flags, 
			   ACE_thread_t *t_id, 
			   ACE_hthread_t *t_handle,
			   long priority,
			   int grp_id,
			   void *stack, 
			   size_t stack_size)
{
  ACE_TRACE ("ACE_Thread_Manager::spawn");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  if (grp_id == -1)
    grp_id = this->grp_id_++; // Increment the group id.

  if (this->spawn_i (func, args, flags, t_id, t_handle, 
		     priority, grp_id, stack, stack_size) == -1)
    return -1;
  else
    return grp_id;
}

// Create N new threads running FUNC.

int 
ACE_Thread_Manager::spawn_n (size_t n, 
			     ACE_THR_FUNC func, 
			     void *args, 
			     long flags,
			     long priority,
			     int grp_id,
			     ACE_Task_Base *task,
			     ACE_hthread_t thread_handles[])
{
  ACE_TRACE ("ACE_Thread_Manager::spawn_n");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  if (grp_id == -1)
    grp_id = this->grp_id_++; // Increment the group id.

  for (size_t i = 0; i < n; i++)
    {
      // @@ What should happen if this fails?! e.g., should we try to
      // cancel the other threads that we've already spawned or what?
      if (this->spawn_i (func,
			 args,
			 flags,
			 0,
			 thread_handles == 0 ? 0 : &thread_handles[i], 
			 priority,
			 grp_id, 
			 0,
			 0,
			 task) == -1)
	return -1;
    }

  return grp_id;
}

// Create N new threads running FUNC.

int 
ACE_Thread_Manager::spawn_n (ACE_thread_t thread_ids[],
			     size_t n, 
			     ACE_THR_FUNC func, 
			     void *args,
			     long flags, 
			     long priority,
			     int grp_id,
			     void *stack[],
			     size_t stack_size[],
			     ACE_hthread_t thread_handles[])
{
  ACE_TRACE ("ACE_Thread_Manager::spawn_n");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  if (grp_id == -1)
    grp_id = this->grp_id_++; // Increment the group id.

  for (size_t i = 0; i < n; i++)
    {
      // @@ What should happen if this fails?! e.g., should we try to
      // cancel the other threads that we've already spawned or what?
      if (this->spawn_i (func, 
			 args, 
			 flags, 
			 thread_ids == 0 ? 0 : &thread_ids[i], 
			 thread_handles == 0 ? 0 : &thread_handles[i], 
			 priority, 
			 grp_id, 
			 stack == 0 ? 0 : stack[i], 
			 stack_size == 0 ? 0 : stack_size[i]) == -1)
	return -1;
    }

  return grp_id;
}

// Append a thread into the pool (does not check for duplicates).
// Must be called with locks held.

int
ACE_Thread_Manager::append_thr (ACE_thread_t t_id, 
				ACE_hthread_t t_handle,
				ACE_Thread_State thr_state,
				int grp_id,
				ACE_Task_Base *task)
{
  ACE_TRACE ("ACE_Thread_Manager::append_thr");
  // Try to resize the array to twice its existing size if we run out
  // of space...
  if (this->current_count_ >= this->max_table_size_ 
      && this->resize (this->max_table_size_ * 2) == -1)
    return -1;
  else
    {
      ACE_Thread_Descriptor &thr_desc = 
	this->thr_table_[this->current_count_];

      thr_desc.thr_id_ = t_id;
      thr_desc.thr_handle_ = t_handle;
      thr_desc.grp_id_ = grp_id;
      thr_desc.thr_state_ = thr_state;
      thr_desc.task_ = task;

      this->current_count_++;
      return 0;
    }
}

// Return the thread descriptor (indexed by ACE_hthread_t).

int 
ACE_Thread_Manager::find_hthread (ACE_hthread_t h_id)
{
  ACE_TRACE ("ACE_Thread_Descriptor::find_hthread");  

  for (size_t i = 0; i < this->current_count_; i++)
    if (ACE_OS::thr_cmp (h_id, this->thr_table_[i].thr_handle_))
      return i;

  return -1;
}

// Locate the index in the table associated with <t_id>.  Must be
// called with the lock held.

int 
ACE_Thread_Manager::find_thread (ACE_thread_t t_id)
{
  ACE_TRACE ("ACE_Thread_Manager::find_thread");

  for (size_t i = 0; i < this->current_count_; i++)
    if (ACE_OS::thr_equal (t_id, this->thr_table_[i].thr_id_))
      return i;

  return -1;    
}

// Insert a thread into the pool (checks for duplicates and doesn't
// allow them to be inserted twice).

int
ACE_Thread_Manager::insert_thr (ACE_thread_t t_id, 
				ACE_hthread_t t_handle,
				int grp_id)
{
  ACE_TRACE ("ACE_Thread_Manager::insert_thr");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  // Check for duplicates and bail out if they're already
  // registered...
  if (this->find_thread (t_id) != -1)
    return -1;

  if (grp_id == -1)
    grp_id = this->grp_id_++;
    
  if (this->append_thr (t_id, t_handle, 
			ACE_THR_SPAWNED,
			grp_id) == -1)
    return -1;
  else
    return grp_id;
}

// Remove a thread from the pool.  Must be called with locks held.

void
ACE_Thread_Manager::remove_thr (int i)
{
  ACE_TRACE ("ACE_Thread_Manager::remove_thr");

  this->current_count_--;

  if (this->current_count_ > 0)
    // Compact the table by moving the last item into the slot vacated
    // by the index being removed (this is a structure assignment).
    this->thr_table_[i] = this->thr_table_[this->current_count_];

#if defined (ACE_HAS_THREADS)
  // Tell all waiters when there are no more threads left in the pool.
  if (this->current_count_ == 0)
    this->zero_cond_.broadcast ();
#endif /* ACE_HAS_THREADS */
}

// Factory out some common behavior to simplify the following methods.
#define ACE_THR_OP(OP,STATE) \
  int result = OP (this->thr_table_[i].thr_handle_); \
  if (result != 0) { \
    this->remove_thr (i); \
    errno = result; \
    return -1; \
  } \
  else { \
    this->thr_table_[i].thr_state_ = STATE; \
    return 0; \
  }

int
ACE_Thread_Manager::join_thr (int i)
{
  ACE_TRACE ("ACE_Thread_Manager::join_thr");

  ACE_THR_OP (ACE_Thread::join, ACE_THR_TERMINATED);
}

int
ACE_Thread_Manager::suspend_thr (int i)
{
  ACE_TRACE ("ACE_Thread_Manager::suspend_thr");

  ACE_THR_OP (ACE_Thread::suspend, ACE_THR_SUSPENDED);
}

int
ACE_Thread_Manager::resume_thr (int i)
{
  ACE_TRACE ("ACE_Thread_Manager::resume_thr");

  ACE_THR_OP (ACE_Thread::resume, ACE_THR_RUNNING);
}

int
ACE_Thread_Manager::cancel_thr (int i)
{
  ACE_TRACE ("ACE_Thread_Manager::cancel_thr");
  this->thr_table_[i].thr_state_ = ACE_THR_CANCELLED;
  return 0;
}

int
ACE_Thread_Manager::kill_thr (int i, int arg)
{
  ACE_TRACE ("ACE_Thread_Manager::kill_thr");

  int signum = (int) arg;

  int result = ACE_Thread::kill ((ACE_thread_t) this->thr_table_[i].thr_id_,
				 signum);

  if (result != 0)
    { 
      // We need to save this across calls to remove_thr() since that
      // call may reset errno.
      int error = errno;

      this->remove_thr (i); 
      errno = error; 
      return -1; 
    } 
  else 
    return 0; 
}

#define ACE_EXECUTE_OP(OP) \
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1)); \
  ACE_FIND (this->find_thread (t_id), index); \
  return OP (index);

// Suspend a single thread.

int 
ACE_Thread_Manager::suspend (ACE_thread_t t_id)
{
  ACE_TRACE ("ACE_Thread_Manager::suspend");
  ACE_EXECUTE_OP (this->suspend_thr);
}

// Resume a single thread.

int 
ACE_Thread_Manager::resume (ACE_thread_t t_id)
{
  ACE_TRACE ("ACE_Thread_Manager::resume");
  ACE_EXECUTE_OP (this->resume_thr);
}

// Cancel a single thread.

int 
ACE_Thread_Manager::cancel (ACE_thread_t t_id)
{
  ACE_TRACE ("ACE_Thread_Manager::cancel");
  ACE_EXECUTE_OP (this->cancel_thr);
}

// Send a signal to a single thread.

int 
ACE_Thread_Manager::kill (ACE_thread_t t_id, int signum)
{
  ACE_TRACE ("ACE_Thread_Manager::kill");
  return ACE_Thread::kill (t_id, signum);
}

int
ACE_Thread_Manager::check_state (ACE_Thread_State state, 
				 ACE_thread_t id) 
{
  ACE_TRACE ("ACE_Thread_Manager::check_state");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  ACE_Thread_State *thr_state = 0;
#if 0
  int self_check = ACE_OS::thr_equal (id, ACE_OS::thr_self ());

  // If we're checking the state of our thread, try to get the cached
  // value out of TSS to avoid lookup.
  if (self_check) 
    thr_state = ACE_LOG_MSG->thr_state ();

  // Wasn't in the cache, so we'll have to look it up.
  if (thr_state == 0) 
    { 
      ACE_FIND (this->find_thread (id), index);
      thr_state = &this->thr_table_[index].thr_state_;

      if (self_check) // Update the TSS cache. 
	ACE_LOG_MSG->thr_state (thr_state); 
    } 
#else
   // Turn off caching for the time being until we figure out 
   // how to do it correctly in the face of deletions...
  ACE_FIND (this->find_thread (id), index);
  thr_state = &this->thr_table_[index].thr_state_;
#endif /* 0 */
  return *thr_state == state;
}

// Test if a single thread is suspended.

int 
ACE_Thread_Manager::testsuspend (ACE_thread_t t_id)
{
  ACE_TRACE ("ACE_Thread_Manager::testsuspend");
  return this->check_state (ACE_THR_SUSPENDED, t_id);
}

// Test if a single thread is active (i.e., resumed).

int 
ACE_Thread_Manager::testresume (ACE_thread_t t_id)
{
  ACE_TRACE ("ACE_Thread_Manager::testresume");
  return this->check_state (ACE_THR_RUNNING, t_id);
}

// Test if a single thread is cancelled.

int 
ACE_Thread_Manager::testcancel (ACE_thread_t t_id)
{
  ACE_TRACE ("ACE_Thread_Manager::testcancel");
  return this->check_state (ACE_THR_CANCELLED, t_id);
}

// Get group ids for a particular thread id.

int 
ACE_Thread_Manager::get_grp (ACE_thread_t t_id, int &grp_id)
{
  ACE_TRACE ("ACE_Thread_Manager::get_grp");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  ACE_FIND (this->find_thread (t_id), index);
  grp_id = this->thr_table_[index].grp_id_;
  return 0;
}

// Set group ids for a particular thread id.

int 
ACE_Thread_Manager::set_grp (ACE_thread_t t_id, int grp_id)
{
  ACE_TRACE ("ACE_Thread_Manager::set_grp");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  ACE_FIND (this->find_thread (t_id), index);
  this->thr_table_[index].grp_id_ = grp_id;
  return 0;
}

// Suspend a group of threads.

int
ACE_Thread_Manager::apply_grp (int grp_id, 
			       THR_FUNC func,
			       int arg)
{
  ACE_TRACE ("ACE_Thread_Manager::apply_grp");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  int result = 0;

  for (size_t i = 0; i < this->current_count_; i++)
    if (this->thr_table_[i].grp_id_ == grp_id
	&& (this->*func)(i, arg) == -1)
      result = -1;

  return result;
}

int 
ACE_Thread_Manager::suspend_grp (int grp_id)
{
  ACE_TRACE ("ACE_Thread_Manager::suspend_grp");
  return this->apply_grp (grp_id, 
			  THR_FUNC (&ACE_Thread_Manager::suspend_thr));
}

// Resume a group of threads.

int 
ACE_Thread_Manager::resume_grp (int grp_id)
{
  ACE_TRACE ("ACE_Thread_Manager::resume_grp");
  return this->apply_grp (grp_id, 
			  THR_FUNC (&ACE_Thread_Manager::resume_thr));
}

// Kill a group of threads.

int 
ACE_Thread_Manager::kill_grp (int grp_id, int signum)
{
  ACE_TRACE ("ACE_Thread_Manager::kill_grp");
  return this->apply_grp (grp_id, 
			  THR_FUNC (&ACE_Thread_Manager::kill_thr), signum);
}

// Cancel a group of threads.

int 
ACE_Thread_Manager::cancel_grp (int grp_id)
{
  ACE_TRACE ("ACE_Thread_Manager::resume_grp");
  return this->apply_grp (grp_id, 
			  THR_FUNC (&ACE_Thread_Manager::cancel_thr));
}

int
ACE_Thread_Manager::apply_all (THR_FUNC func, int arg)
{
  ACE_TRACE ("ACE_Thread_Manager::apply_all");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  int result = 0;

  for (size_t i = 0; i < this->current_count_; i++)
    if ((this->*func)(i, arg) == -1)
      result = -1;

  return result;
}

// Resume all threads that are suspended.

int 
ACE_Thread_Manager::resume_all (void)
{
  ACE_TRACE ("ACE_Thread_Manager::resume_all");
  return this->apply_all (THR_FUNC (&ACE_Thread_Manager::resume_thr));
}

int 
ACE_Thread_Manager::suspend_all (void)
{
  ACE_TRACE ("ACE_Thread_Manager::suspend_all");
  return this->apply_all (THR_FUNC (&ACE_Thread_Manager::suspend_thr));
}

int 
ACE_Thread_Manager::kill_all (int sig)
{
  ACE_TRACE ("ACE_Thread_Manager::kill_all");
  return this->apply_all (&ACE_Thread_Manager::kill_thr, sig);
}

int 
ACE_Thread_Manager::cancel_all (void)
{
  ACE_TRACE ("ACE_Thread_Manager::cancel_all");
  return this->apply_all (THR_FUNC (&ACE_Thread_Manager::cancel_thr));
}

// Wait for group of threads

int 
ACE_Thread_Manager::wait_grp (int grp_id)
{
  ACE_TRACE ("ACE_Thread_Manager::wait_grp");

  int copy_count = 0;
  ACE_Thread_Descriptor *copy_table = 0;

  // We have to make sure that while we wait for these threads to
  // exit, we do not have the lock. Therefore we make a copy of all
  // interesting entries and let go of the lock.
  {
    ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

    copy_table = new ACE_Thread_Descriptor [this->current_count_];  

    for (size_t i = 0; i < this->current_count_; i++)
      if (this->thr_table_[i].grp_id_ == grp_id)
	{
	  copy_table[copy_count] = this->thr_table_[i];
	  copy_count++;
	}
  }
  
  // Now to do the actual work
  int result = 0;

  for (int i = 0;
       i < copy_count && result != -1;
       i++)
    if (ACE_Thread::join (copy_table[i].thr_handle_) == -1)
      result = -1;
  
  delete [] copy_table;

  return result;
}

// Must be called when thread goes out of scope to clean up its table
// slot.

void *
ACE_Thread_Manager::exit (void *status, int do_thr_exit)
{
  ACE_TRACE ("ACE_Thread_Manager::exit");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, 0));

  int i = this->find_thread (ACE_Thread::self ());

  // Locate thread id.
  if (i != -1)
    this->remove_thr (i);

  if (do_thr_exit)
    {
      // Note, destructor is never called, so we must manually release
      // the lock...  Note that once we release the lock, it marks it
      // as being "free" so that the Guard's destructor won't try to
      // release it again.

      ACE_MT (ace_mon.release ()); 

      ACE_Thread::exit (status);
      // On reasonable systems ACE_Thread::exit() should not return.
      // However, due to horrible semantics with Win32 thread-specific
      // storage this call can return (don't ask...).
    }
  return 0;
}

// Wait for all the threads to exit.

int
ACE_Thread_Manager::wait (const ACE_Time_Value *timeout)
{
  ACE_TRACE ("ACE_Thread_Manager::wait");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

#if defined (ACE_HAS_THREADS)
  while (this->current_count_ > 0)
    if (this->zero_cond_.wait (timeout) == -1)
      return -1;
#else
  ACE_UNUSED_ARG (timeout);
#endif /* ACE_HAS_THREADS */

  return 0;
}

int
ACE_Thread_Manager::apply_task (ACE_Task_Base *task, 
				THR_FUNC func,
				int arg)
{
  ACE_TRACE ("ACE_Thread_Manager::apply_task");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  int result = 0;

  for (size_t i = 0; i < this->current_count_; i++)
    if (this->thr_table_[i].task_ == task
	&& (this->*func)(i, arg) == -1)
      result = -1;

  return result;
}

// Wait for task  
int 
ACE_Thread_Manager::wait_task (ACE_Task_Base *task)
{
  int copy_count = 0;
  ACE_Thread_Descriptor *copy_table = 0;

  // We have to make sure that while we wait for these threads to
  // exit, we do not have the lock. Therefore we make a copy of all
  // interesting entries and let go of the lock.
  {
    ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

    ACE_NEW_RETURN (copy_table,
		    ACE_Thread_Descriptor [this->current_count_],
		    -1);

    for (size_t i = 0; i < this->current_count_; i++)
      if (this->thr_table_[i].task_ == task)
	{
	  copy_table[copy_count] = this->thr_table_[i];
	  copy_count++;
	}
  }
  
  // Now to do the actual work
  int result = 0;
  for (int i = 0; i < copy_count && result != -1; i++)
    if (ACE_Thread::join (copy_table[i].thr_handle_) == -1)
      result = -1;
  
  delete [] copy_table;

  return result;
}

// Suspend a task
int  
ACE_Thread_Manager::suspend_task (ACE_Task_Base *task)
{ 
  ACE_TRACE ("ACE_Thread_Manager::suspend_task");
  return this->apply_task (task, 
			   THR_FUNC (&ACE_Thread_Manager::suspend_thr));
}

// Resume a task.
int  
ACE_Thread_Manager::resume_task (ACE_Task_Base *task)
{
  ACE_TRACE ("ACE_Thread_Manager::resume_task");
  return this->apply_task (task, 
			   THR_FUNC (&ACE_Thread_Manager::resume_thr));
}

// Kill a task.
int  
ACE_Thread_Manager::kill_task (ACE_Task_Base *task, int /* signum */)
{
  ACE_TRACE ("ACE_Thread_Manager::kill_task");
  return this->apply_task (task, 
			   THR_FUNC (&ACE_Thread_Manager::kill_thr));
}

// Cancel a task.
int  
ACE_Thread_Manager::cancel_task (ACE_Task_Base *task)
{
  ACE_TRACE ("ACE_Thread_Manager::cancel_task");
  return this->apply_task (task, 
			   THR_FUNC (&ACE_Thread_Manager::cancel_thr));
}

// Locate the index in the table associated with <task> from the
// beginning of the table up to an index.  Must be called with the
// lock held.

int 
ACE_Thread_Manager::find_task (ACE_Task_Base *task, 
			       int index)
{
  ACE_TRACE ("ACE_Thread_Manager::find_task");

  if (index == -1)
      index = current_count_;

  for (int i = 0; i < index; i++)
    if (task == this->thr_table_[i].task_)
      return i;

  return -1;    
}

// Returns the number of ACE_Task in a group. 

int 
ACE_Thread_Manager::num_tasks_in_group (int grp_id)
{
  ACE_TRACE ("ACE_Thread_Manager::num_tasks_in_group");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  int tasks_count = 0;

  for (size_t i = 0; i < this->current_count_; i++)
    if (this->thr_table_[i].grp_id_ == grp_id
	&& this->find_task (thr_table_[i].task_, i) == -1)
      tasks_count++;

  return tasks_count;
}

// Returns the number of threads in an ACE_Task.

int 
ACE_Thread_Manager::num_threads_in_task (ACE_Task_Base *task)
{
  ACE_TRACE ("ACE_Thread_Manager::num_threads_in_task");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  int threads_count = 0;

  for (size_t i = 0; i < this->current_count_; i++)
    if (this->thr_table_[i].task_ == task)
      threads_count++;

  return threads_count;
}

// Returns in task_list a list of ACE_Tasks in a group.

int 
ACE_Thread_Manager::task_list (int grp_id, 
			       ACE_Task_Base *task_list[],
			       size_t n)
{
  ACE_TRACE ("ACE_Thread_Manager::task_list");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  ACE_Task_Base **task_list_iterator = task_list;
  size_t task_list_count = 0;

  for (size_t i = 0; i < this->current_count_; i++) 
    if (task_list_count < n
	&& this->thr_table_[i].grp_id_ == grp_id
	&& this->find_task (thr_table_[i].task_, i) == -1)
      {
	task_list_iterator[task_list_count] = this->thr_table_[i].task_;
	task_list_count++;
      }

  return 0;
}

// Returns in thread_list a list of thread ids in an ACE_Task.

int 
ACE_Thread_Manager::thread_list (ACE_Task_Base *task, 
				 ACE_thread_t thread_list[],
				 size_t n)
{
  ACE_TRACE ("ACE_Thread_Manager::thread_list");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  ACE_thread_t *thread_list_iterator = thread_list;
  size_t thread_list_count = 0;

  for (size_t i = 0; i < this->current_count_; i++)
    if (thread_list_count < n && this->thr_table_[i].task_ == task)
      {
	thread_list_iterator[thread_list_count] = this->thr_table_[i].thr_id_;
	thread_list_count++;
      }

  return 0;
}

// Returns in thread_list a list of thread handles in an ACE_Task.

int 
ACE_Thread_Manager::hthread_list (ACE_Task_Base *task, 
				  ACE_hthread_t hthread_list[],
				  size_t n)
{
  ACE_TRACE ("ACE_Thread_Manager::thread_list");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  ACE_hthread_t *hthread_list_iterator = hthread_list;
  size_t hthread_list_count = 0;

  for (size_t i = 0; i < this->current_count_; i++)
    if (this->thr_table_[i].task_ == task 
	&& hthread_list_count < n)
      {
	hthread_list_iterator[hthread_list_count] = 
	  this->thr_table_[i].thr_handle_;
	hthread_list_count++;
      }

  return 0;
}

int 
ACE_Thread_Manager::set_grp (ACE_Task_Base *task, int grp_id)
{
  ACE_TRACE ("ACE_Thread_Manager::set_grp");

  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  for (size_t i = 0; i < this->current_count_; i++)
    if (this->thr_table_[i].task_ == task)
      this->thr_table_[i].grp_id_ = grp_id;
  return 0;
}

int 
ACE_Thread_Manager::get_grp (ACE_Task_Base *task, int &grp_id)
{
  ACE_TRACE ("ACE_Thread_Manager::get_grp");

  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1));

  ACE_FIND (this->find_task (task), index);
  grp_id = this->thr_table_[index].grp_id_;
  return 0;
}

void
ACE_Thread_Control::dump (void) const
{
  ACE_TRACE ("ACE_Thread_Control::dump");
}

int
ACE_Thread_Control::insert (ACE_Thread_Manager *tm)
{
  ACE_TRACE ("ACE_Thread_Control::insert");
 
  ACE_hthread_t t_id;
  ACE_Thread::self (t_id);
  this->tm_ = tm;
 
  return this->tm_->insert_thr (ACE_Thread::self (), t_id);
}

// Initialize the thread controller.

ACE_Thread_Control::ACE_Thread_Control (ACE_Thread_Manager *t, 
					int insert)
  : tm_ (t),
    status_ (0)
{
  ACE_TRACE ("ACE_Thread_Control::ACE_Thread_Control");
  if (this->tm_ != 0 && insert)
    {
      ACE_hthread_t t_id;
      ACE_Thread::self (t_id);
      this->tm_->insert_thr (ACE_Thread::self (), t_id);
    }
}

// Automatically kill thread on exit.

ACE_Thread_Control::~ACE_Thread_Control (void)
{
  ACE_TRACE ("ACE_Thread_Control::~ACE_Thread_Control");
#if defined (ACE_HAS_RECURSIVE_THR_EXIT_SEMANTICS) || defined (ACE_HAS_TSS_EMULATION)
  this->exit (this->status_, 0);
#else
  this->exit (this->status_, 1);
#endif /* ACE_HAS_RECURSIVE_THR_EXIT_SEMANTICS */
}

// Exit from thread (but clean up first).

void *
ACE_Thread_Control::exit (void *exit_status, int do_thr_exit)
{
  ACE_TRACE ("ACE_Thread_Control::exit");

  if (this->tm_ != 0)
    return this->tm_->exit (exit_status, do_thr_exit);
  else
    {
#if !defined (ACE_HAS_TSS_EMULATION)
      // With ACE_HAS_TSS_EMULATION, we let ACE_Thread_Adapter::invoke ()
      // exit the thread after cleaning up TSS.
      ACE_Thread::exit (exit_status);
#endif /* ! ACE_HAS_TSS_EMULATION */
      return 0;
    }
}