/
OS_NS_Thread.h
2004 lines (1657 loc) · 62.3 KB
/
OS_NS_Thread.h
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
// -*- C++ -*-
//=============================================================================
/**
* @file OS_NS_Thread.h
*
* $Id: OS_NS_Thread.h 91693 2010-09-09 12:57:54Z johnnyw $
*
* @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
* @author Jesper S. M|ller<stophph@diku.dk>
* @author and a cast of thousands...
*
* Originally in OS.h.
*/
//=============================================================================
#ifndef ACE_OS_NS_THREAD_H
# define ACE_OS_NS_THREAD_H
# include /**/ "ace/pre.h"
# include "ace/config-all.h"
# if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
# endif /* ACE_LACKS_PRAGMA_ONCE */
# include "ace/Global_Macros.h"
# include "ace/Basic_Types.h"
# include "ace/Default_Constants.h"
# include "ace/os_include/os_pthread.h"
# include "ace/os_include/os_sched.h"
# include "ace/Base_Thread_Adapter.h"
# include "ace/os_include/sys/os_sem.h"
# include "ace/os_include/os_semaphore.h"
# include "ace/OS_Memory.h"
# include "ace/OS_NS_signal.h"
# include "ace/ACE_export.h"
# include "ace/Object_Manager_Base.h"
# if defined (ACE_EXPORT_MACRO)
# undef ACE_EXPORT_MACRO
# endif
# define ACE_EXPORT_MACRO ACE_Export
# if defined (ACE_HAS_PRIOCNTL)
// Need to #include thread.h before #defining THR_BOUND, etc.,
// when building without threads on SunOS 5.x.
# if defined (sun)
# include /**/ <thread.h>
# endif /* sun */
// Need to #include these before #defining USYNC_PROCESS on SunOS 5.x.
# include /**/ <sys/rtpriocntl.h>
# include /**/ <sys/tspriocntl.h>
# endif /* ACE_HAS_PRIOCNTL */
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
# if defined (ACE_WIN32)
typedef DWORD ACE_thread_t;
typedef HANDLE ACE_hthread_t;
// Native TSS key type
typedef DWORD ACE_OS_thread_key_t;
// Application TSS key type (use this type except in TSS Emulation)
# if defined (ACE_HAS_TSS_EMULATION)
typedef u_int ACE_thread_key_t;
# else /* ! ACE_HAS_TSS_EMULATION */
typedef ACE_OS_thread_key_t ACE_thread_key_t;
# endif /* ! ACE_HAS_TSS_EMULATION */
# endif /* ACE_WIN32 */
ACE_END_VERSIONED_NAMESPACE_DECL
# if !defined (ACE_HAS_POSIX_SEM) && defined (ACE_USES_FIFO_SEM)
extern "C" {
typedef struct
{
char* name_;
ACE_HANDLE fd_[2];
} ACE_sema_t;
}
#endif /* !ACE_HAS_POSIX_SEM && ACE_USES_FIFO_SEM */
# if defined (ACE_HAS_THREADS)
# if defined (ACE_HAS_STHREADS)
# include /**/ <synch.h>
# include /**/ <thread.h>
# define ACE_SCOPE_PROCESS P_PID
# define ACE_SCOPE_LWP P_LWPID
# define ACE_SCOPE_THREAD (ACE_SCOPE_LWP + 1)
# else
# define ACE_SCOPE_PROCESS 0
# define ACE_SCOPE_LWP 1
# define ACE_SCOPE_THREAD 2
# endif /* ACE_HAS_STHREADS */
# if !defined (ACE_HAS_PTHREADS)
# define ACE_SCHED_OTHER 0
# define ACE_SCHED_FIFO 1
# define ACE_SCHED_RR 2
# endif /* ! ACE_HAS_PTHREADS */
# if defined (ACE_HAS_PTHREADS)
// moved to pthread.h
# elif defined (ACE_HAS_STHREADS)
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
// Solaris threads, without PTHREADS.
// Typedefs to help compatibility with Windows NT and Pthreads.
typedef thread_t ACE_thread_t;
// Native TSS key type (not for general use)
typedef thread_key_t ACE_OS_thread_key_t;
// Application TSS key type (use this type except in TSS Emulation)
# if defined (ACE_HAS_TSS_EMULATION)
typedef u_int ACE_thread_key_t;
# else /* ! ACE_HAS_TSS_EMULATION */
typedef ACE_OS_thread_key_t ACE_thread_key_t;
# endif /* ! ACE_HAS_TSS_EMULATION */
typedef mutex_t ACE_mutex_t;
# if !defined (ACE_LACKS_RWLOCK_T)
typedef rwlock_t ACE_rwlock_t;
# endif /* !ACE_LACKS_RWLOCK_T */
# if !defined (ACE_HAS_POSIX_SEM) && !defined (ACE_USES_FIFO_SEM)
typedef sema_t ACE_sema_t;
# endif /* !ACE_HAS_POSIX_SEM */
typedef cond_t ACE_cond_t;
struct ACE_Export ACE_condattr_t
{
int type;
};
struct ACE_Export ACE_mutexattr_t
{
int type;
};
typedef ACE_thread_t ACE_hthread_t;
typedef ACE_mutex_t ACE_thread_mutex_t;
ACE_END_VERSIONED_NAMESPACE_DECL
# define THR_CANCEL_DISABLE 0
# define THR_CANCEL_ENABLE 0
# define THR_CANCEL_DEFERRED 0
# define THR_CANCEL_ASYNCHRONOUS 0
# define THR_JOINABLE 0
# define THR_SCHED_FIFO 0
# define THR_SCHED_RR 0
# define THR_SCHED_DEFAULT 0
# define THR_INHERIT_SCHED 0
# define THR_SCOPE_PROCESS 0
# elif defined (ACE_VXWORKS)
# include /**/ <sysLib.h> // for sysClkRateGet()
# if !defined (__RTP__)
# include /**/ <taskLib.h>
# include /**/ <taskHookLib.h>
# endif
// make sure these are included for VXWORKS.
// @todo move these to a common place, perhaps the top of the file.
# include "ace/os_include/os_fcntl.h"
# include "ace/os_include/os_netdb.h"
# include "ace/os_include/os_semaphore.h"
# include "ace/os_include/os_signal.h"
# include "ace/os_include/os_stdio.h"
# include "ace/os_include/os_stdlib.h"
# include "ace/os_include/os_stropts.h"
# include "ace/os_include/os_unistd.h"
# include "ace/os_include/arpa/os_inet.h"
# include "ace/os_include/sys/os_select.h"
# include "ace/os_include/sys/os_socket.h"
// task options: the other options are either obsolete, internal, or for
// Fortran or Ada support
# define VX_UNBREAKABLE 0x0002 /* breakpoints ignored */
# if !defined (VX_FP_TASK)
# define VX_FP_TASK 0x0008 /* floating point coprocessor */
# endif
# define VX_PRIVATE_ENV 0x0080 /* private environment support */
# define VX_NO_STACK_FILL 0x0100 /* do not stack fill for
checkstack () */
# define THR_CANCEL_DISABLE 0
# define THR_CANCEL_ENABLE 0
# define THR_CANCEL_DEFERRED 0
# define THR_CANCEL_ASYNCHRONOUS 0
# define THR_BOUND 0
# define THR_NEW_LWP 0
# define THR_DETACHED 0
# define THR_SUSPENDED 0
# define THR_DAEMON 0
# define THR_JOINABLE 0
# define THR_SCHED_FIFO 0
# define THR_SCHED_RR 0
# define THR_SCHED_DEFAULT 0
# define THR_INHERIT_SCHED 0
# define THR_EXPLICIT_SCHED 0
# define THR_SCHED_IO 0
# define THR_SCOPE_SYSTEM 0
# define THR_SCOPE_PROCESS 0
# define USYNC_THREAD 0
# define USYNC_PROCESS 1 /* It's all global on VxWorks
(without MMU option). */
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
typedef SEM_ID ACE_mutex_t;
// Implement ACE_thread_mutex_t with ACE_mutex_t because there's just
// one process . . .
typedef ACE_mutex_t ACE_thread_mutex_t;
# if !defined (ACE_HAS_POSIX_SEM)
// Use VxWorks semaphores, wrapped ...
typedef struct
{
/// Semaphore handle. This is allocated by VxWorks.
SEM_ID sema_;
/// Name of the semaphore: always NULL with VxWorks.
char *name_;
} ACE_sema_t;
# endif /* !ACE_HAS_POSIX_SEM */
typedef int ACE_thread_t;
typedef int ACE_hthread_t;
// Key type: the ACE TSS emulation requires the key type be unsigned,
// for efficiency. (Current POSIX and Solaris TSS implementations also
// use u_int, so the ACE TSS emulation is compatible with them.)
// Native TSS key type
typedef u_int ACE_OS_thread_key_t;
// Application TSS key type (use this type except in TSS Emulation)
# if defined (ACE_HAS_TSS_EMULATION)
typedef u_int ACE_thread_key_t;
# else /* ! ACE_HAS_TSS_EMULATION */
typedef ACE_OS_thread_key_t ACE_thread_key_t;
# endif /* ! ACE_HAS_TSS_EMULATION */
ACE_END_VERSIONED_NAMESPACE_DECL
# elif defined (ACE_HAS_WTHREADS)
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
typedef CRITICAL_SECTION ACE_thread_mutex_t;
typedef struct
{
/// Either USYNC_THREAD or USYNC_PROCESS
int type_;
union
{
HANDLE proc_mutex_;
CRITICAL_SECTION thr_mutex_;
};
} ACE_mutex_t;
// Wrapper for NT Events.
typedef HANDLE ACE_event_t;
# if defined (ACE_WIN32)
//@@ ACE_USES_WINCE_SEMA_SIMULATION is used to debug
// semaphore simulation on WinNT. It should be
// changed to ACE_USES_HAS_WINCE at some later point.
# if !defined (ACE_USES_WINCE_SEMA_SIMULATION)
typedef HANDLE ACE_sema_t;
# else
/**
* @class ACE_sema_t
*
* @brief Semaphore simulation for Windows CE.
*/
class ACE_Export ACE_sema_t
{
public:
/// Serializes access to @c count_.
ACE_thread_mutex_t lock_;
/// This event is signaled whenever the count becomes non-zero.
ACE_event_t count_nonzero_;
/// Current count of the semaphore.
u_int count_;
};
# endif /* ACE_USES_WINCE_SEMA_SIMULATION */
# endif /* defined (ACE_WIN32) */
ACE_END_VERSIONED_NAMESPACE_DECL
// These need to be different values, neither of which can be 0...
# define USYNC_THREAD 1
# define USYNC_PROCESS 2
# define THR_CANCEL_DISABLE 0
# define THR_CANCEL_ENABLE 0
# define THR_CANCEL_DEFERRED 0
# define THR_CANCEL_ASYNCHRONOUS 0
# define THR_DETACHED 0x02000000 /* ignore in most places */
# define THR_BOUND 0 /* ignore in most places */
# define THR_NEW_LWP 0 /* ignore in most places */
# define THR_DAEMON 0 /* ignore in most places */
# define THR_JOINABLE 0 /* ignore in most places */
# define THR_SUSPENDED CREATE_SUSPENDED
# if !defined (STACK_SIZE_PARAM_IS_A_RESERVATION)
# define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
# endif /* STACK_SIZE_PARAM_IS_A_RESERVATION */
# define THR_USE_AFX 0x01000000
# define THR_SCHED_FIFO 0
# define THR_SCHED_RR 0
# define THR_SCHED_DEFAULT 0
# define THR_INHERIT_SCHED 0
# define THR_EXPLICIT_SCHED 0
# define THR_SCOPE_PROCESS 0
# define THR_SCOPE_SYSTEM 0
# endif /* ACE_HAS_PTHREADS / STHREADS / VXWORKS / WTHREADS **********/
# if defined (ACE_HAS_WTHREADS_CONDITION_VARIABLE)
typedef CONDITION_VARIABLE ACE_cond_t;
# elif defined (ACE_LACKS_COND_T)
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
/**
* @class ACE_cond_t
*
* @brief This structure is used to implement condition variables on
* platforms that lack it natively, such as VxWorks, and Win32.
*
* At the current time, this stuff only works for threads
* within the same process.
*/
class ACE_Export ACE_cond_t
{
public:
/// Returns the number of waiters.
long waiters (void) const;
//protected:
/// Number of waiting threads.
long waiters_;
/// Serialize access to the waiters count.
ACE_thread_mutex_t waiters_lock_;
/// Queue up threads waiting for the condition to become signaled.
ACE_sema_t sema_;
# if defined (ACE_VXWORKS)
/**
* A semaphore used by the broadcast/signal thread to wait for all
* the waiting thread(s) to wake up and be released from the
* semaphore.
*/
ACE_sema_t waiters_done_;
# elif defined (ACE_WIN32)
/**
* An auto reset event used by the broadcast/signal thread to wait
* for the waiting thread(s) to wake up and get a chance at the
* semaphore.
*/
HANDLE waiters_done_;
# else
# error "Please implement this feature or check your config.h file!"
# endif /* ACE_VXWORKS */
/// Keeps track of whether we were broadcasting or just signaling.
size_t was_broadcast_;
};
ACE_END_VERSIONED_NAMESPACE_DECL
# endif /* ACE_LACKS_COND_T */
# if defined (ACE_HAS_WTHREADS_CONDITION_VARIABLE) || defined (ACE_LACKS_COND_T)
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
struct ACE_Export ACE_condattr_t
{
int type;
};
struct ACE_Export ACE_mutexattr_t
{
int type;
};
ACE_END_VERSIONED_NAMESPACE_DECL
# endif /* ACE_HAS_WTHREADS_CONDITION_VARIABLE || ACE_LACKS_COND_T */
# if defined (ACE_LACKS_RWLOCK_T) && !defined (ACE_HAS_PTHREADS_UNIX98_EXT)
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
/**
* @class ACE_rwlock_t
*
* @brief This is used to implement readers/writer locks on NT,
* VxWorks, and POSIX pthreads.
*
* At the current time, this stuff only works for threads
* within the same process.
*/
struct ACE_Export ACE_rwlock_t
{
public:
//protected:
/// Serialize access to internal state.
ACE_mutex_t lock_;
/// Reader threads waiting to acquire the lock.
ACE_cond_t waiting_readers_;
/// Number of waiting readers.
int num_waiting_readers_;
/// Writer threads waiting to acquire the lock.
ACE_cond_t waiting_writers_;
/// Number of waiting writers.
int num_waiting_writers_;
/// Value is -1 if writer has the lock, else this keeps track of the
/// number of readers holding the lock.
int ref_count_;
/// Indicate that a reader is trying to upgrade
bool important_writer_;
/// Condition for the upgrading reader
ACE_cond_t waiting_important_writer_;
};
ACE_END_VERSIONED_NAMESPACE_DECL
# elif defined (ACE_HAS_PTHREADS_UNIX98_EXT)
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
typedef pthread_rwlock_t ACE_rwlock_t;
ACE_END_VERSIONED_NAMESPACE_DECL
# elif defined (ACE_HAS_STHREADS)
# include /**/ <synch.h>
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
typedef rwlock_t ACE_rwlock_t;
ACE_END_VERSIONED_NAMESPACE_DECL
# endif /* ACE_LACKS_RWLOCK_T */
// Define some default thread priorities on all threaded platforms, if
// not defined above or in the individual platform config file.
// ACE_THR_PRI_FIFO_DEF should be used by applications for default
// real-time thread priority. ACE_THR_PRI_OTHER_DEF should be used
// for non-real-time priority.
# if !defined(ACE_THR_PRI_FIFO_DEF)
# if defined (ACE_WTHREADS)
// It would be more in spirit to use THREAD_PRIORITY_NORMAL. But,
// using THREAD_PRIORITY_ABOVE_NORMAL should give preference to the
// threads in this process, even if the process is not in the
// REALTIME_PRIORITY_CLASS.
# define ACE_THR_PRI_FIFO_DEF THREAD_PRIORITY_ABOVE_NORMAL
# else /* ! ACE_WTHREADS */
# define ACE_THR_PRI_FIFO_DEF 0
# endif /* ! ACE_WTHREADS */
# endif /* ! ACE_THR_PRI_FIFO_DEF */
# if !defined(ACE_THR_PRI_OTHER_DEF)
# if defined (ACE_WTHREADS)
// It would be more in spirit to use THREAD_PRIORITY_NORMAL. But,
// using THREAD_PRIORITY_ABOVE_NORMAL should give preference to the
// threads in this process, even if the process is not in the
// REALTIME_PRIORITY_CLASS.
# define ACE_THR_PRI_OTHER_DEF THREAD_PRIORITY_NORMAL
# else /* ! ACE_WTHREADS */
# define ACE_THR_PRI_OTHER_DEF 0
# endif /* ! ACE_WTHREADS */
# endif /* ! ACE_THR_PRI_OTHER_DEF */
// Recursive mutex support.
//
// There are two parts to this:
// 1. The mutex type itself. This is based on whether or not the
// platform supports recursive mutexes natively or they're emulated.
// 2. Support for using the recursive mutex with a condition variable.
// When a thread waits on a condition variable, it has to relinquish
// the lock and wait atomically, then reacquire it after the condition
// variable is signaled. In non-recursive mutexes, the platform
// handles this automatically. But in recursive mutexes, especially
// when emulated, the recursion count needs to be maintained across
// the wait. Since another thread needs to be able to acquire the
// lock, it needs to appear free, even if the waiting thread had done
// multiple acquires. Thus, there's another structure to hold this
// information, and is used with the recursive_mutex_cond_unlock()
// and recursive_mutex_cond_relock() methods to maintain the expected
// state when the wait finishes.
# if defined (ACE_HAS_RECURSIVE_MUTEXES)
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
typedef ACE_thread_mutex_t ACE_recursive_thread_mutex_t;
# if defined (ACE_WIN32)
// Windows has recursive mutexes, but doesn't have condition variables,
// so there's no built-in support for this. Thus, the condition-related
// unlock/relock is augmented in ACE.
struct ACE_recursive_mutex_state
{
// On Windows the augmented processing is simply unlocking/relocking
// the recursive locks - the condition handles a single lock ok.
LONG relock_count_;
};
# else
// No need for special handling; just need a type for method signatures.
typedef int ACE_recursive_mutex_state;
# endif /* ACE_WIN32 */
ACE_END_VERSIONED_NAMESPACE_DECL
# else
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
/**
* @class ACE_recursive_thread_mutex_t
*
* @brief
* Implement a thin C++ wrapper that allows nested acquisition
* and release of a mutex that occurs in the same thread.
*
* This implementation is based on an algorithm sketched by Dave
* Butenhof <butenhof@zko.dec.com>. Naturally, I take the
* credit for any mistakes ;-)
*/
class ACE_recursive_thread_mutex_t
{
public:
/// Guards the state of the nesting level and thread id.
ACE_thread_mutex_t nesting_mutex_;
/// This condition variable suspends other waiting threads until the
/// mutex is available.
ACE_cond_t lock_available_;
/// Current nesting level of the recursion.
int nesting_level_;
/// Current owner of the lock.
ACE_thread_t owner_id_;
};
/// Since recursive mutex is emulated, the state saving needs to be handled
/// in ACE as well. These members save those from ACE_recursive_thread_mutex_t.
struct ACE_recursive_mutex_state
{
int nesting_level_;
ACE_thread_t owner_id_;
};
ACE_END_VERSIONED_NAMESPACE_DECL
# endif /* ACE_HAS_RECURSIVE_MUTEXES */
# else /* !ACE_HAS_THREADS, i.e., the OS/platform doesn't support threading. */
// Give these things some reasonable value...
# define ACE_SCOPE_PROCESS 0
# define ACE_SCOPE_LWP 1
# define ACE_SCOPE_THREAD 2
# define ACE_SCHED_OTHER 0
# define ACE_SCHED_FIFO 1
# define ACE_SCHED_RR 2
# if !defined (THR_CANCEL_DISABLE)
# define THR_CANCEL_DISABLE 0
# endif /* ! THR_CANCEL_DISABLE */
# if !defined (THR_CANCEL_ENABLE)
# define THR_CANCEL_ENABLE 0
# endif /* ! THR_CANCEL_ENABLE */
# if !defined (THR_CANCEL_DEFERRED)
# define THR_CANCEL_DEFERRED 0
# endif /* ! THR_CANCEL_DEFERRED */
# if !defined (THR_CANCEL_ASYNCHRONOUS)
# define THR_CANCEL_ASYNCHRONOUS 0
# endif /* ! THR_CANCEL_ASYNCHRONOUS */
# if !defined (THR_JOINABLE)
# define THR_JOINABLE 0 /* ignore in most places */
# endif /* ! THR_JOINABLE */
# if !defined (THR_DETACHED)
# define THR_DETACHED 0 /* ignore in most places */
# endif /* ! THR_DETACHED */
# if !defined (THR_DAEMON)
# define THR_DAEMON 0 /* ignore in most places */
# endif /* ! THR_DAEMON */
# if !defined (THR_BOUND)
# define THR_BOUND 0 /* ignore in most places */
# endif /* ! THR_BOUND */
# if !defined (THR_NEW_LWP)
# define THR_NEW_LWP 0 /* ignore in most places */
# endif /* ! THR_NEW_LWP */
# if !defined (THR_SUSPENDED)
# define THR_SUSPENDED 0 /* ignore in most places */
# endif /* ! THR_SUSPENDED */
# if !defined (THR_SCHED_FIFO)
# define THR_SCHED_FIFO 0
# endif /* ! THR_SCHED_FIFO */
# if !defined (THR_SCHED_RR)
# define THR_SCHED_RR 0
# endif /* ! THR_SCHED_RR */
# if !defined (THR_SCHED_DEFAULT)
# define THR_SCHED_DEFAULT 0
# endif /* ! THR_SCHED_DEFAULT */
# if !defined (THR_INHERIT_SCHED)
# define THR_INHERIT_SCHED 0
# endif /* ! THR_INHERIT_SCHED */
# if !defined (USYNC_THREAD)
# define USYNC_THREAD 0
# endif /* ! USYNC_THREAD */
# if !defined (USYNC_PROCESS)
# define USYNC_PROCESS 0
# endif /* ! USYNC_PROCESS */
# if !defined (THR_SCOPE_PROCESS)
# define THR_SCOPE_PROCESS 0
# endif /* ! THR_SCOPE_PROCESS */
# if !defined (THR_SCOPE_SYSTEM)
# define THR_SCOPE_SYSTEM 0
# endif /* ! THR_SCOPE_SYSTEM */
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
// These are dummies needed for class OS.h
typedef int ACE_cond_t;
struct ACE_Export ACE_condattr_t
{
int type;
};
struct ACE_Export ACE_mutexattr_t
{
int type;
};
typedef int ACE_mutex_t;
typedef int ACE_thread_mutex_t;
typedef int ACE_recursive_thread_mutex_t;
typedef int ACE_recursive_mutex_state;
# if !defined (ACE_HAS_POSIX_SEM) && !defined (ACE_USES_FIFO_SEM)
typedef int ACE_sema_t;
# endif /* !ACE_HAS_POSIX_SEM && !ACE_USES_FIFO_SEM */
typedef int ACE_rwlock_t;
typedef int ACE_thread_t;
typedef int ACE_hthread_t;
// Native TSS key type
typedef unsigned int ACE_OS_thread_key_t;
// Application TSS key type (use this type except in TSS Emulation)
# if defined (ACE_HAS_TSS_EMULATION)
typedef u_int ACE_thread_key_t;
# else /* ! ACE_HAS_TSS_EMULATION */
typedef ACE_OS_thread_key_t ACE_thread_key_t;
# endif /* ! ACE_HAS_TSS_EMULATION */
ACE_END_VERSIONED_NAMESPACE_DECL
// Ensure that ACE_THR_PRI_FIFO_DEF and ACE_THR_PRI_OTHER_DEF are
// defined on non-threaded platforms, to support application source
// code compatibility. ACE_THR_PRI_FIFO_DEF should be used by
// applications for default real-time thread priority.
// ACE_THR_PRI_OTHER_DEF should be used for non-real-time priority.
# if !defined(ACE_THR_PRI_FIFO_DEF)
# define ACE_THR_PRI_FIFO_DEF 0
# endif /* ! ACE_THR_PRI_FIFO_DEF */
# if !defined(ACE_THR_PRI_OTHER_DEF)
# define ACE_THR_PRI_OTHER_DEF 0
# endif /* ! ACE_THR_PRI_OTHER_DEF */
# endif /* ACE_HAS_THREADS ***********************************************/
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
/**
* @class ACE_Thread_ID
*
* @brief
* Defines a platform-independent thread ID class. Note that
* this class should be defined within the scope of a thread, rather
* than at global scope!
*/
class ACE_Export ACE_Thread_ID
{
public:
/// Initialize the object using the thread id and thread handle
/// passed as parameters.
ACE_Thread_ID (ACE_thread_t thr_id,
ACE_hthread_t thr_handle);
/// Initialize the object using calls to ACE_OS::thr_self().
ACE_Thread_ID (void);
/// Copy constructor.
ACE_Thread_ID (const ACE_Thread_ID &id);
/// Assignment operator
ACE_Thread_ID& operator= (const ACE_Thread_ID&id);
/// Get the thread id.
ACE_thread_t id (void) const;
/// Set the thread id.
void id (ACE_thread_t);
/// Get the thread handle.
ACE_hthread_t handle (void) const;
/// Set the thread handle.
void handle (ACE_hthread_t);
// Create a string representation of the thread id.
void to_string (char *thr_string) const;
/// Equality operator.
bool operator== (const ACE_Thread_ID &) const;
/// Inequality operator.
bool operator!= (const ACE_Thread_ID &) const;
private:
/// Identify the thread.
ACE_thread_t thread_id_;
/// Handle to the thread (typically used to "wait" on Win32).
ACE_hthread_t thread_handle_;
};
// = The ACE_Sched_Priority type should be used for platform-
// independent thread and process priorities, by convention.
// int should be used for OS-specific priorities.
typedef int ACE_Sched_Priority;
# if !defined (ACE_DEFAULT_SYNCH_TYPE)
# if defined (ACE_VXWORKS)
// Types include these options: SEM_Q_PRIORITY, SEM_Q_FIFO,
// SEM_DELETE_SAFE, and SEM_INVERSION_SAFE. SEM_Q_FIFO is
// used as the default because that is VxWorks' default.
# define ACE_DEFAULT_SYNCH_TYPE SEM_Q_FIFO
# else
# define ACE_DEFAULT_SYNCH_TYPE USYNC_THREAD
# endif /* ACE_VXWORKS */
#endif /* ! ACE_DEFAULT_SYNCH_TYPE */
// forward declaration
class ACE_Sched_Params;
class ACE_Time_Value;
#if defined (ACE_WIN32)
typedef int ACE_idtype_t;
typedef DWORD ACE_id_t;
typedef int ACE_pri_t;
# define ACE_SELF (0)
#else /* !defined (ACE_WIN32) */
# if defined (ACE_HAS_IDTYPE_T)
typedef idtype_t ACE_idtype_t;
# else
typedef int ACE_idtype_t;
# endif /* ACE_HAS_IDTYPE_T */
# if defined (ACE_HAS_STHREADS)
# if defined (ACE_LACKS_PRI_T)
typedef int pri_t;
# endif /* ACE_LACKS_PRI_T */
typedef id_t ACE_id_t;
# define ACE_SELF P_MYID
typedef pri_t ACE_pri_t;
# else /* ! ACE_HAS_STHREADS */
typedef long ACE_id_t;
# define ACE_SELF (-1)
typedef short ACE_pri_t;
# endif /* ! ACE_HAS_STHREADS */
#endif /* !defined (ACE_WIN32) */
# if defined (ACE_HAS_TSS_EMULATION)
// Allow config.h to set the default number of thread keys.
# if !defined (ACE_DEFAULT_THREAD_KEYS)
# define ACE_DEFAULT_THREAD_KEYS 64
# endif /* ! ACE_DEFAULT_THREAD_KEYS */
// forward declaration
class ACE_TSS_Keys;
/**
* @class ACE_TSS_Emulation
*
* @brief Thread-specific storage emulation.
*
* This provides a thread-specific storage implementation.
* It is intended for use on platforms that don't have a
* native TSS, or have a TSS with limitations such as the
* number of keys or lack of support for removing keys.
*/
class ACE_Export ACE_TSS_Emulation
{
public:
typedef void (*ACE_TSS_DESTRUCTOR)(void *value) /* throw () */;
/// Maximum number of TSS keys allowed over the life of the program.
enum { ACE_TSS_THREAD_KEYS_MAX = ACE_DEFAULT_THREAD_KEYS };
/// Returns the total number of keys allocated so far.
static u_int total_keys ();
/// Sets the argument to the next available key. Returns 0 on success,
/// -1 if no keys are available.
static int next_key (ACE_thread_key_t &key);
/// Release a key that was used. This way the key can be given out in a
/// new request. Returns 0 on success, 1 if the key was not reserved.
static int release_key (ACE_thread_key_t key);
/// Check a key for validity.
static int is_key (ACE_thread_key_t key);
/// Returns the exit hook associated with the key. Does _not_ check
/// for a valid key.
static ACE_TSS_DESTRUCTOR tss_destructor (const ACE_thread_key_t key);
/// Associates the TSS destructor with the key. Does _not_ check
/// for a valid key.
static void tss_destructor (const ACE_thread_key_t key,
ACE_TSS_DESTRUCTOR destructor);
/// Accesses the object referenced by key in the current thread's TSS array.
/// Does _not_ check for a valid key.
static void *&ts_object (const ACE_thread_key_t key);
/**
* Setup an array to be used for local TSS. Returns the array
* address on success. Returns 0 if local TSS had already been
* setup for this thread. There is no corresponding tss_close ()
* because it is not needed.
* @note tss_open () is called by ACE for threads that it spawns.
* If your application spawns threads without using ACE, and it uses
* ACE's TSS emulation, each of those threads should call tss_open
* (). See the ace_thread_adapter () implementation for an example.
*/
static void *tss_open (void *ts_storage[ACE_TSS_THREAD_KEYS_MAX]);
/// Shutdown TSS emulation. For use only by ACE_OS::cleanup_tss ().
static void tss_close ();
private:
// Global TSS structures.
/// Contains the possible value of the next key to be allocated. Which key
/// is actually allocated is based on the tss_keys_used
static u_int total_keys_;
/// Array of thread exit hooks (TSS destructors) that are called for each
/// key (that has one) when the thread exits.
static ACE_TSS_DESTRUCTOR tss_destructor_ [ACE_TSS_THREAD_KEYS_MAX];
/// TSS_Keys instance to administrate whether a specific key is in used
/// or not.
/// or not.
// Static construction in VxWorks 5.4 and later is slightly broken.
// If the static object is more complex than an integral type, static
// construction will occur twice. The tss_keys_used_ object is
// statically constructed and then modified by ACE_Log_Msg::instance()
// when two keys are created and TSS data is stored. However, at
// the end of static construction the tss_keys_used_ object is again
// initialized and therefore it will appear to next_key() that no
// TSS keys have been handed out. That is all true unless the
// tss_keys_used object is a static pointer instead of a static object.
static ACE_TSS_Keys* tss_keys_used_;
# if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
/// Location of current thread's TSS array.
static void **tss_base (void* ts_storage[] = 0, u_int *ts_created = 0);
# else /* ! ACE_HAS_THREAD_SPECIFIC_STORAGE */
/// Location of current thread's TSS array.
static void **&tss_base ();
# endif /* ! ACE_HAS_THREAD_SPECIFIC_STORAGE */
# if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
// Rely on native thread specific storage for the implementation,
// but just use one key.
static ACE_OS_thread_key_t native_tss_key_;
// Used to indicate if native tss key has been allocated
static bool key_created_;
# endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE */
};
# endif /* ACE_HAS_TSS_EMULATION */
// moved ACE_TSS_Ref, ACE_TSS_Info, and ACE_TSS_Keys class
// declarations from OS.cpp so they are visible to the single
// file of template instantiations.
# if defined (ACE_WIN32) || defined (ACE_HAS_TSS_EMULATION)
/**
* @class ACE_TSS_Ref
*
* @brief "Reference count" for thread-specific storage keys.
*
* Since the <ACE_Unbounded_Stack> doesn't allow duplicates, the
* "reference count" is the identify of the thread_id.
*/
class ACE_TSS_Ref
{
public:
/// Constructor
ACE_TSS_Ref (ACE_thread_t id);
/// Default constructor
ACE_TSS_Ref (void);
/// Check for equality.
bool operator== (const ACE_TSS_Ref &) const;
/// Check for inequality.
bool operator!= (const ACE_TSS_Ref &) const;
// private:
/// ID of thread using a specific key.
ACE_thread_t tid_;
};
/**
* @class ACE_TSS_Info
*
* @brief Thread Specific Key management.
*
* This class maps a key to a "destructor."
*/
class ACE_TSS_Info
{
public:
/// Declare pointer to function to destroy tss object.
typedef void (*Destructor)(void *);
/// Constructor
ACE_TSS_Info (ACE_thread_key_t key,
Destructor dest = 0);
/// Default constructor
ACE_TSS_Info (void);
/// Returns 1 if the key is in use, 0 if not.
int key_in_use (void) const { return thread_count_ != -1; }
/// Mark the key as being in use if the flag is non-zero, or
/// not in use if the flag is 0.
void key_in_use (int flag) { thread_count_ = flag == 0 ? -1 : 1; }
/// Check for equality.
bool operator== (const ACE_TSS_Info &) const;
/// Check for inequality.
bool operator!= (const ACE_TSS_Info &) const;
/// Dump the state.
void dump (void);
private:
/// Key to the thread-specific storage item.
ACE_thread_key_t key_;
/// "Destructor" that gets called when the item is finally released.
Destructor destructor_;
/// Count of threads that are using this key. Contains -1 when the
/// key is not in use.
int thread_count_;
friend class ACE_TSS_Cleanup;
};
/**
* @class ACE_TSS_Keys
*
* @brief Collection of in-use flags for a thread's TSS keys.
* For internal use only by ACE_TSS_Cleanup; it is public because
* some compilers can't use nested classes for template instantiation
* parameters.
*
* Wrapper around array of whether each key is in use. A simple
* typedef doesn't work with Sun C++ 4.2.
*/
class ACE_TSS_Keys
{
public:
/// Default constructor, to initialize all bits to zero (unused).
ACE_TSS_Keys (void);
/// Mark the specified key as being in use, if it was not already so marked.
/// Returns 1 if the had already been marked, 0 if not.
int test_and_set (const ACE_thread_key_t key);
/// Mark the specified key as not being in use, if it was not already so
/// cleared. Returns 1 if the key had already been cleared, 0 if not.
int test_and_clear (const ACE_thread_key_t key);
/// Return whether the specific key is marked as in use.
/// Returns 1 if the key is been marked, 0 if not.