Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / tests / Auto_IncDec_Test.cpp
blob8022519994bd5820efe7d6275ad7519feab9eea0
2 //=============================================================================
3 /**
4 * @file Auto_IncDec_Test.cpp
6 * This is a simple test of the Auto Increment/Decrement Class in
7 * ACE.
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;
30 static void *
31 worker (void *)
33 ACE_DEBUG ((LM_DEBUG,
34 ACE_TEXT (" (%t) worker starting\n")));
36 { // First section.
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 (),
42 0);
43 ACE_OS::sleep (pause);
46 { // Second section.
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
51 // section...
52 ACE_Time_Value pause (current_threads_in_first_section.value (),
53 0);
54 ACE_OS::sleep (pause);
57 ACE_DEBUG ((LM_DEBUG,
58 ACE_TEXT (" (%t) worker exiting\n")));
59 return 0;
62 #endif /* ACE_HAS_THREADS */
64 // Spawn off threads.
66 int
67 run_main (int, ACE_TCHAR *[])
69 ACE_START_TEST (ACE_TEXT ("Auto_IncDec_Test"));
71 #if defined (ACE_HAS_THREADS)
72 ACE_DEBUG ((LM_DEBUG,
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
79 (n_threads,
80 ACE_THR_FUNC (worker),
82 THR_NEW_LWP) == -1)
83 ACE_ERROR_RETURN ((LM_ERROR,
84 ACE_TEXT ("%p\n"),
85 ACE_TEXT ("spawn_n")),
86 -1);
87 // Make sure at least one thread is started...
88 ACE_Thread::yield ();
90 while (ACE_Thread_Manager::instance ()->count_threads ())
92 // wait according to the number of threads...
93 ACE_DEBUG ((LM_DEBUG,
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")));
110 #else
111 int counter = 0;
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 */
132 ACE_END_TEST;
133 return 0;