Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Auto_IncDec_Test.cpp
blobdb61c42a77cbffab028a5d6990c709a1c529a646
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"
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;
31 static void *
32 worker (void *)
34 ACE_DEBUG ((LM_DEBUG,
35 ACE_TEXT (" (%t) worker starting\n")));
37 { // First section.
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 (),
43 0);
44 ACE_OS::sleep (pause);
47 { // Second section.
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
52 // section...
53 ACE_Time_Value pause (current_threads_in_first_section.value (),
54 0);
55 ACE_OS::sleep (pause);
58 ACE_DEBUG ((LM_DEBUG,
59 ACE_TEXT (" (%t) worker exiting\n")));
60 return 0;
63 #endif /* ACE_HAS_THREADS */
65 // Spawn off threads.
67 int
68 run_main (int, ACE_TCHAR *[])
70 ACE_START_TEST (ACE_TEXT ("Auto_IncDec_Test"));
72 #if defined (ACE_HAS_THREADS)
73 ACE_DEBUG ((LM_DEBUG,
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
80 (n_threads,
81 ACE_THR_FUNC (worker),
83 THR_NEW_LWP) == -1)
84 ACE_ERROR_RETURN ((LM_ERROR,
85 ACE_TEXT ("%p\n"),
86 ACE_TEXT ("spawn_n")),
87 -1);
88 // Make sure at least one thread is started...
89 ACE_Thread::yield ();
91 while (ACE_Thread_Manager::instance ()->count_threads ())
93 // wait according to the number of threads...
94 ACE_DEBUG ((LM_DEBUG,
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")));
111 #else
112 int counter = 0;
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 */
133 ACE_END_TEST;
134 return 0;