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"
22 #if defined (ACE_HAS_THREADS)
24 // Default number of threads.
25 static size_t n_threads
= 15;
27 typedef ACE_Atomic_Op
<ACE_Thread_Mutex
, int> INTERLOCKED_INT
;
28 static INTERLOCKED_INT current_threads_in_first_section
;
29 static INTERLOCKED_INT current_threads_in_second_section
;
35 ACE_TEXT (" (%t) worker starting\n")));
38 ACE_Auto_IncDec
<INTERLOCKED_INT
> threads_in_section_auto_inc_dec
39 (current_threads_in_first_section
);
41 // Wait according to the number of threads...
42 ACE_Time_Value
pause (current_threads_in_first_section
.value (),
44 ACE_OS::sleep (pause
);
48 ACE_Auto_IncDec
<INTERLOCKED_INT
> threads_in_section_auto_inc_dec
49 (current_threads_in_second_section
);
51 // Wait according to the number of threads inside the previous
53 ACE_Time_Value
pause (current_threads_in_first_section
.value (),
55 ACE_OS::sleep (pause
);
59 ACE_TEXT (" (%t) worker exiting\n")));
63 #endif /* ACE_HAS_THREADS */
68 run_main (int, ACE_TCHAR
*[])
70 ACE_START_TEST (ACE_TEXT ("Auto_IncDec_Test"));
72 #if defined (ACE_HAS_THREADS)
74 ACE_TEXT (" (%t) main thread starting\n")));
76 current_threads_in_first_section
= 0;
77 current_threads_in_second_section
= 0;
79 if (ACE_Thread_Manager::instance ()->spawn_n
81 ACE_THR_FUNC (worker
),
84 ACE_ERROR_RETURN ((LM_ERROR
,
86 ACE_TEXT ("spawn_n")),
88 // Make sure at least one thread is started...
91 while (ACE_Thread_Manager::instance ()->count_threads ())
93 // wait according to the number of threads...
95 ACE_TEXT (" %d in first section, %d in second section, %d total\n"),
96 current_threads_in_first_section
.value (),
97 current_threads_in_second_section
.value (),
98 ACE_Thread_Manager::instance ()->count_threads ()));
100 ACE_Time_Value
pause (1, 0);
101 ACE_OS::sleep (pause
);
104 ACE_Thread_Manager::instance ()->wait ();
106 ACE_TEST_ASSERT (current_threads_in_first_section
.value () == 0
107 && current_threads_in_second_section
.value () == 0);
109 ACE_DEBUG ((LM_DEBUG
,
110 ACE_TEXT (" (%t) exiting main thread\n")));
114 ACE_Auto_IncDec
<int> Auto_IncDec1 (counter
);
115 ACE_TEST_ASSERT (counter
== 1);
117 ACE_Auto_IncDec
<int> Auto_IncDec2 (counter
);
118 ACE_TEST_ASSERT (counter
== 2);
121 ACE_TEST_ASSERT (counter
== 2);
122 ACE_Auto_IncDec
<int> Auto_IncDec3 (counter
);
123 ACE_TEST_ASSERT (counter
== 3);
126 ACE_TEST_ASSERT (counter
== 2);
129 ACE_TEST_ASSERT (counter
== 0);
131 #endif /* ACE_HAS_THREADS */