2 //=============================================================================
4 * @file Auto_IncDec_Test.cpp
6 * This is a simple test of the Auto Increment/Decrement Class in
9 * @author Edan Ayal <EdanA@cti2.com>
11 //=============================================================================
14 #include "test_config.h"
15 #include "ace/Auto_IncDec_T.h"
16 #include "ace/Thread_Manager.h"
17 #include "ace/Atomic_Op.h"
18 #include "ace/OS_NS_unistd.h"
21 #if defined (ACE_HAS_THREADS)
23 // Default number of threads.
24 static size_t n_threads
= 15;
26 using INTERLOCKED_INT
= ACE_Atomic_Op
<ACE_Thread_Mutex
, int>;
27 static INTERLOCKED_INT current_threads_in_first_section
;
28 static INTERLOCKED_INT current_threads_in_second_section
;
34 ACE_TEXT (" (%t) worker starting\n")));
37 ACE_Auto_IncDec
<INTERLOCKED_INT
> threads_in_section_auto_inc_dec
38 (current_threads_in_first_section
);
40 // Wait according to the number of threads...
41 ACE_Time_Value
pause (current_threads_in_first_section
.value (),
43 ACE_OS::sleep (pause
);
47 ACE_Auto_IncDec
<INTERLOCKED_INT
> threads_in_section_auto_inc_dec
48 (current_threads_in_second_section
);
50 // Wait according to the number of threads inside the previous
52 ACE_Time_Value
pause (current_threads_in_first_section
.value (),
54 ACE_OS::sleep (pause
);
58 ACE_TEXT (" (%t) worker exiting\n")));
62 #endif /* ACE_HAS_THREADS */
67 run_main (int, ACE_TCHAR
*[])
69 ACE_START_TEST (ACE_TEXT ("Auto_IncDec_Test"));
71 #if defined (ACE_HAS_THREADS)
73 ACE_TEXT (" (%t) main thread starting\n")));
75 current_threads_in_first_section
= 0;
76 current_threads_in_second_section
= 0;
78 if (ACE_Thread_Manager::instance ()->spawn_n
80 ACE_THR_FUNC (worker
),
83 ACE_ERROR_RETURN ((LM_ERROR
,
85 ACE_TEXT ("spawn_n")),
87 // Make sure at least one thread is started...
90 while (ACE_Thread_Manager::instance ()->count_threads ())
92 // wait according to the number of threads...
94 ACE_TEXT (" %d in first section, %d in second section, %d total\n"),
95 current_threads_in_first_section
.value (),
96 current_threads_in_second_section
.value (),
97 ACE_Thread_Manager::instance ()->count_threads ()));
99 ACE_Time_Value
pause (1, 0);
100 ACE_OS::sleep (pause
);
103 ACE_Thread_Manager::instance ()->wait ();
105 ACE_TEST_ASSERT (current_threads_in_first_section
.value () == 0
106 && current_threads_in_second_section
.value () == 0);
108 ACE_DEBUG ((LM_DEBUG
,
109 ACE_TEXT (" (%t) exiting main thread\n")));
113 ACE_Auto_IncDec
<int> Auto_IncDec1 (counter
);
114 ACE_TEST_ASSERT (counter
== 1);
116 ACE_Auto_IncDec
<int> Auto_IncDec2 (counter
);
117 ACE_TEST_ASSERT (counter
== 2);
120 ACE_TEST_ASSERT (counter
== 2);
121 ACE_Auto_IncDec
<int> Auto_IncDec3 (counter
);
122 ACE_TEST_ASSERT (counter
== 3);
125 ACE_TEST_ASSERT (counter
== 2);
128 ACE_TEST_ASSERT (counter
== 0);
130 #endif /* ACE_HAS_THREADS */