Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Barrier_Test.cpp
bloba79a4d1daf37910f6ae9e32113d30b3b68783798
2 //=============================================================================
3 /**
4 * @file Barrier_Test.cpp
6 * This program illustrates how the ACE barrier synchronization
7 * mechanisms work.
9 * @author Prashant Jain <pjain@cs.wustl.edu> and Doug Schmidt <d.schmidt@vanderbilt.edu>
11 //=============================================================================
14 #include "test_config.h"
15 #include "ace/Barrier.h"
16 #include "ace/Thread_Manager.h"
19 #if defined (ACE_HAS_THREADS)
21 struct Tester_Args
22 // = TITLE
23 // These arguments are passed into each test thread.
25 Tester_Args (ACE_Barrier &tb, int i)
26 : tester_barrier_ (tb),
27 n_iterations_ (i) {}
29 ACE_Barrier &tester_barrier_;
30 // Reference to the tester barrier. This controls each iteration
31 // of the tester function running in every thread.
33 int n_iterations_;
34 // Number of iterations to run.
37 // Iterate <n_iterations> time printing off a message and "waiting"
38 // for all other threads to complete this iteration.
40 static void *
41 wait_tester (Tester_Args *args)
43 for (int iterations = 1;
44 iterations <= args->n_iterations_;
45 iterations++)
47 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) in iteration %d\n"),
48 iterations));
50 // Block until all other threads have waited, then continue.
51 if (args->tester_barrier_.wait () != 0)
52 ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"),
53 ACE_TEXT ("wait failed")));
56 return 0;
59 // Wait on the barrier, expecting it to be shut down before completing
60 // the wait.
62 static void *
63 shut_tester (Tester_Args *args)
65 if (args->tester_barrier_.wait () == 0)
66 ACE_ERROR ((LM_ERROR,
67 ACE_TEXT ("(%t) wait succeeded, should have shut down\n")));
68 else if (errno != ESHUTDOWN)
69 ACE_ERROR ((LM_ERROR,
70 ACE_TEXT ("(%t) wait failed, expecting ESHUTDOWN, %p\n"),
71 ACE_TEXT ("got")));
72 return 0;
75 #endif /* ACE_HAS_THREADS */
77 int
78 run_main (int, ACE_TCHAR *[])
80 ACE_START_TEST (ACE_TEXT ("Barrier_Test"));
82 #if defined (ACE_HAS_THREADS)
83 int n_threads = ACE_MAX_THREADS;
84 int n_iterations = ACE_MAX_ITERATIONS;
86 ACE_Barrier tester_barrier (n_threads);
88 Tester_Args args (tester_barrier, n_iterations);
90 for (size_t iteration_count = 0;
91 iteration_count < ACE_MAX_ITERATIONS;
92 iteration_count++)
94 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("starting iteration %d\n"),
95 iteration_count));
97 if (ACE_Thread_Manager::instance ()->spawn_n
98 (n_threads,
99 (ACE_THR_FUNC) wait_tester,
100 (void *) &args,
101 THR_NEW_LWP | THR_JOINABLE) == -1)
103 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
104 ACE_TEXT ("spawn_n")), 1);
106 ACE_Thread_Manager::instance ()->wait ();
109 // Now test ACE_Barrier shutdown. Set up a barrier for n_threads, and start
110 // n_threads - 1 threads to wait, then shut the barrier down.
111 ACE_Barrier shut_barrier (n_threads);
112 Tester_Args shut_args (shut_barrier, 1);
113 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Starting shutdown test threads\n")));
114 if (ACE_Thread_Manager::instance ()->spawn_n
115 (n_threads - 1,
116 (ACE_THR_FUNC) shut_tester,
117 (void *) &shut_args,
118 THR_NEW_LWP | THR_JOINABLE) == -1)
119 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn_n")), 1);
121 shut_barrier.shutdown ();
122 ACE_Thread_Manager::instance ()->wait ();
124 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("test done\n")));
125 #else
126 ACE_ERROR ((LM_INFO,
127 ACE_TEXT ("threads not supported on this platform\n")));
128 #endif /* ACE_HAS_THREADS */
129 ACE_END_TEST;
130 return 0;