2 //=============================================================================
4 * @file Barrier_Test.cpp
6 * This program illustrates how the ACE barrier synchronization
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)
23 // These arguments are passed into each test thread.
25 Tester_Args (ACE_Barrier
&tb
, int i
)
26 : tester_barrier_ (tb
),
29 ACE_Barrier
&tester_barrier_
;
30 // Reference to the tester barrier. This controls each iteration
31 // of the tester function running in every thread.
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.
41 wait_tester (Tester_Args
*args
)
43 for (int iterations
= 1;
44 iterations
<= args
->n_iterations_
;
47 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) in iteration %d\n"),
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")));
59 // Wait on the barrier, expecting it to be shut down before completing
63 shut_tester (Tester_Args
*args
)
65 if (args
->tester_barrier_
.wait () == 0)
67 ACE_TEXT ("(%t) wait succeeded, should have shut down\n")));
68 else if (errno
!= ESHUTDOWN
)
70 ACE_TEXT ("(%t) wait failed, expecting ESHUTDOWN, %p\n"),
75 #endif /* ACE_HAS_THREADS */
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
;
94 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("starting iteration %d\n"),
97 if (ACE_Thread_Manager::instance ()->spawn_n
99 (ACE_THR_FUNC
) wait_tester
,
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
116 (ACE_THR_FUNC
) shut_tester
,
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")));
127 ACE_TEXT ("threads not supported on this platform\n")));
128 #endif /* ACE_HAS_THREADS */